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
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:
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 theelsebranch thinking that42is astr.I think you simply cannot narrow the negative case for
isinstanceif its second argument is of typetype[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