Skip to content

type of Union containing Any is incorrectly inferred after an isinstance check #1720

Closed
@sharmaeklavya2

Description

@sharmaeklavya2

union_any.py:

from typing import Any, Union

def func(v):
    # type: (Union[int, Any]) -> str
    if isinstance(v, int):
        s = hex(v)
    else:
        s = str(v).lower()
    return "key:" + s

mypy union_any.py:

union_any.py: note: In function "func":
union_any.py:8: error: No overload variant of "str" matches argument types [Union[<ERROR>, void]]

mypy --py2 union_any.py:

union_any.py: note: In function "func":
union_any.py:8: error: Argument 1 to "str" has incompatible type "Union[object, None]"; expected "object"

I discovered this when I used a class which was defined in some library which did not have stubs and I was using --silent-imports.

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