-
Notifications
You must be signed in to change notification settings - Fork 1.7k
isinstance(instance, SubClass)
loses generic type instead of narrowing
#817
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
This is the correct behavior. The assert statement validates that If you change your code as follows, it should work as you expect:
|
If we change the last lines to: def f(a: A[T]) -> B[T]:
assert isinstance(a, B[T])
return a
f(B()) the code will produce TypeError at runtime:
As per documentation this construction is not valid Python code since |
Also, if this helps, I think this is the analogous bug in MyPy: python/mypy#949 |
Yes, you are correct. My proposed workaround unfortunately won't work. It might seem straightforward to simply assume that
Let's assume in this case that a value of type Here are some additional workarounds (none of which are very elegant):
|
Thanks for the clarification. On a related note I think it would be nice if pyright warned about the usage of |
That's a good suggestion. |
Your suggestion is now implemented in Pyright 1.1.52, which I just published. It will be included in the next published version of Pylance. |
Describe the bug
When performing an
isinstance
check on object with generic type, the type information of variable type is lost.To Reproduce
Standard pyright configuration with strict mode enabled.
Expected behavior
No errors. After assert type of
a
isB[T]
.Actual behavior
17:12 - error: Return type, "B[Unknown]", is partially unknown
After assert type of
a
isB[Unknown]
The text was updated successfully, but these errors were encountered: