Replies: 2 comments 3 replies
-
Yes, this is expected behavior. It's unsafe to narrow the type in the negative (else) case because you're using the def func(a: list[int] | int):
if type(a) == list:
print("Got a list!")
else:
print("Got an int?")
class MyList(list[int]):
...
func(MyList()) # Prints "Got an int?" Not surprisingly, mypy produces the same type narrowing results as pyright in this case. |
Beta Was this translation helpful? Give feedback.
3 replies
-
Apologies for reviving a necrothread, but here's a method that works for type variables (like def is_type_foo(type_) -> TypeGuard[type[Foo]]:
return type_ is Foo or issubclass(type_, Foo) |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
For the following code:
Somehow pyright can infer that
a
is indeedlist[int]
in the true branch, but in the else brancha
is inferred aslist[int] | int
still. Is this expected behavior?Beta Was this translation helpful? Give feedback.
All reactions