KEMBAR78
If isinstance target type is Any, narrow down to Any · Issue #3474 · python/mypy · GitHub
Skip to content

If isinstance target type is Any, narrow down to Any #3474

@JukkaL

Description

@JukkaL

Currently if the second argument to isinstance has type Any, we don't narrow the type down. This doesn't work as expected with ignored imports. Example (assume that foo has a missing stub):

from typing import Union

from foo import A  # type: ignore

def f(x: Union[A, str]) -> None:
    if isinstance(x, A):
        x.method_only_in_a()  # Item "str" of "Union[Any, str]" has no attribute "method_only_in_a"

I'd expect the type of x to be 'narrowed' (actually widened) to Any, and mypy should probably not generate an error.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions