Skip to content

FR: Type narrowing for x is C #7642

Open
@jbwdevries

Description

@jbwdevries

This is a feature request.

Example unit test:

[case testIsNarrowAny]
from typing import Any, Type, Union

def narrow_any_to_str_then_reassign_to_int() -> None:
    v = 1 # type: Union[Type[Any], int]

    if v is Any:
        reveal_type(v)  # N: Revealed type is 'Type[Any]'
        v = 2
        reveal_type(v)  # N: Revealed type is 'int'

[builtins fixtures/isinstance.pyi]

For a project I'm working on, it would be help readability if I could write code like the above. However, mypy does not perform type narrowing in this case, so that results in a lot of type warnings.

Note: This is different from type(x) is C. In this case, x is the type instance itself, so no subclasses are involved.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions