-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
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
type signature in generic subclass #5664
Labels
Comments
ilevkivskyi
added
bug
mypy got something wrong
topic-type-variables
priority-1-normal
labels
Sep 24, 2018
This is a known issue, see #3645, but I don't want to close this because I want to collect all the use cases for type variables in class methods, so that we can understand what is actually desired and test our solution properly. |
Thanks for your reply! I have tried the PR and found a code may get wrong checking result. If it's ok to put my repro in the PR? |
ilevkivskyi
added a commit
that referenced
this issue
Feb 24, 2019
Fixes #3645 Fixes #1337 Fixes #5664 The fix is straightforward, I just add/propagate the bound type variable values by mapping to supertype. I didn't find any corner cases with class methods, and essentially follow the same logic as when we generate the callable from `__init__` for generic classes in calls like `C()` or `C[int]()`. For class attributes there are two things I fixed. First we used to prohibit ambiguous access: ```python class C(Generic[T]): x: T C.x # Error! C[int].x # Error! ``` but the type variables were leaking after an error, now they are erased to `Any`. Second, I now make an exception and allow accessing attributes on `Type[C]`, this is very similar to how we allow instantiation of `Type[C]` even if it is abstract (because we expect concrete subclasses there), plus this allows accessing variables on `cls` (first argument in class methods), for example: ```python class C(Generic[T]): x: T def get(cls) -> T: return cls.x # OK ``` (I also added a bunch of more detailed comments in this part of code.)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
output of mypy is
I was expecting that mypy will find type mismatch to call
IteratorBox.wrap(g)
, which it didn't.And
reveal_type(IteratorBox.wrap)
just return the type of the method in base class.The text was updated successfully, but these errors were encountered: