Skip to content

Constrained TypeVar fails to narrow on left hand side of conditional expression #4134

Description

@alanhdu

When I run mypy --python-version 3.6 on the following code:

from typing import AnyStr
def f(x: AnyStr) -> bytes:
    return x.encode("ascii") if isinstance(x, str) else x

I get:

test.py:3: error: "bytes" has no attribute "encode"; maybe "decode"?

But as far as I can tell, this is backwards! x.encode("ascii") is only called when isinstance(x, str), not in the else case (where x is a bytes object).

This is with the latest mypy==0.530

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions