-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
isinstance
Does not Narrow Generic Parameters
#9141
Comments
Typechecking issue, transferring to Pyright. |
I Initially marked this as "Bug" because the inference is correct for |
…es where the filter type (the second argument) is a child of the un-narrowed type and the child has a type parameter that is used multiple times in its base class (e.g. `Child[T](Parent[T, T])`). This addresses #9141.
Pyright does try to retain type arguments when an class Parent[T, U]:
def __init__(self, value: T, other: U) -> None: ...
class Sibling[T, U](Parent[T, U]): ...
class Child[T](Parent[T, T]):
def meth[U](self, other: Parent[T, U]) -> None:
if isinstance(other, Sibling):
reveal_type(other) # Pyright reveals Sibling[T, U], mypy reveals Sibling[Any, Any]
if isinstance(other, Child):
reveal_type(other) # Pyright reveals Child[Unknown], mypy reveals Child[Any] The challenge with the I've enhanced the algorithm so it can handle this condition. This will be included in the next release. |
Directed to new issue from microsoft/pylance-release#2128
Environment data
Code Snippet
Repro Steps
Expected behavior
in the
if isinstance(other, Child)
the type ofother
should be narrowed toChild[T] / Parent[T, T]
andU
should be infered toT
.From my understanding, unless there is some slip in my logic I'm not catching, this is the only valid inference that matches both
other: Parent[T, U]
andother: Child[Unknown]
Actual behavior
Inferences left inline above in the comments, I wrote out Pylance's inferences.
Particularly, again in that if block,
other: Child[Unknown]
instead ofother: Child[T]
Logs
The "Python" log looks uninteresting, and the "Python Language Server" log blew up with a ton of stuff. Not sure what I would be looking for in here, but I think it's a bad idea to just dump it in here.
The text was updated successfully, but these errors were encountered: