Skip to content

Narrowing for isinstance and type[T] is unsound #713

@hurryabit

Description

@hurryabit

Describe the Bug

I came across something during my own work on pytype2 and thought I'd check whether pyrefly has the same problem. Turns out it does:

def f(cls: type[int], x: int | str) -> None:
    if isinstance(x, cls):
        reveal_type(x)  # reveals int - ok
    else:
        reveal_type(x)  # reveals str - too narrow

The first revealed type is fine but the second revealed type is too narrow. Someone could call f(bool, 42) and would end up in the else branch thinking that 42 is a str.

I think you simply cannot narrow the negative case for isinstance if its second argument is of type type[T] but not the literal name of a class.

Sandbox Link

https://pyrefly.org/sandbox/?code=GYJw9gtgBALgngBwJYDsDmUkQWEMoBUAUEQCYCmwUwAFAMYA2AzgFyyLkDaqMAugDRQAHmx5QAPlCYwQASigBaAHxQAcmBTkWRKLsxUkTVNICGKOuRpDBjJrO17HUEOQBu5EwwD68BJaGyOnrkzFpBTs5uHt6+-oFAA

(Only applicable for extension issues) IDE Information

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions