Skip to content

Variable incorrectly typed as None, misleading error message #1691

Closed
@dmoisset

Description

@dmoisset

While debugging a more complicated issue, I reduced it to this:

from typing import Tuple, Union

Pair = Tuple[int, int]
Variant = Union[int, Pair]


def tuplify(v: Variant) -> None:
    if not isinstance(v, tuple):
        v = (v, v)
    print(v[0], v[1])

mypy running on this example detects an error on the print line saying:

foo.py:10: error: Value of type None is not indexable

For some reason it says that the value of v has type None. I understand why the checker may not be able to follow the negative isinstance check, but I expected the error to say something like Value of type Union[...] is not indexable.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrong

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions