You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I am trying to use mypy on an existing django project. In Django models can have a special sub-class Meta and the Models can inherit from each other. Therefore I have a situation like this one:
Hm, interesting. There is an ongoing project writing stubs for django (https://github.com/machinalis/mypy-django) -- maybe that has a solution already, or maybe someone on that project can help you if you file an issue there?
This one is actually quite straightforward to fix. The symbol table nodes for nested classes are created in the first pass (when little is known) so that their types are set to None. They are however never updated in the second pass. I would propose to update the types to either Type[Nested] or CallableType[<signature of __init__>, Nested]. Both versions have their pluses and minuses, so I am not sure here.
This is closed by #5926 that emits a better error message, and accepts safe use cases (e.g. when one nested class is a subclass of another nested class).
Note that mypy still emits an error on the original example, because it is technically unsafe. Note however that @mkurnikov works on a mypy plugin for Django that will suppress this error for Django specific use cases.
Also for those who don't want to use the Django mypy plugin, or if you want to silence this error for other reason you can either just put # type: ignore on the subclass definition, or use this pattern:
Unsafe: Any=objectclassA:
classSub(Unsafe):
passclassB:
classSub(Unsafe):
passclassC(A, B): # no complains from mypypass
Note however it will disable many checks for A.Sub and B.Sub.
Reacted by Sacha, Vishes, nefi, Nick Presta, Mateusz Mikołajczyk and Dhruv Baldawa
Activity
gvanrossum commentedon Aug 21, 2017
Hm, interesting. There is an ongoing project writing stubs for django (https://github.com/machinalis/mypy-django) -- maybe that has a solution already, or maybe someone on that project can help you if you file an issue there?
ostcar commentedon Aug 21, 2017
Thank you for the tip. I opened an issue on there project.
But I think this problem is not django specific. I don't know if classes in classes is a good style, but another projects may also use them.
gvanrossum commentedon Aug 21, 2017
It's pretty uncommon outside Django's patterns.
ilevkivskyi commentedon Aug 21, 2017
This one is actually quite straightforward to fix. The symbol table nodes for nested classes are created in the first pass (when little is known) so that their types are set to
None. They are however never updated in the second pass. I would propose to update the types to eitherType[Nested]orCallableType[<signature of __init__>, Nested]. Both versions have their pluses and minuses, so I am not sure here.syenchuk commentedon Nov 8, 2018
FYI, similar issues #2871 #4956
ilevkivskyi commentedon Jan 9, 2019
This is closed by #5926 that emits a better error message, and accepts safe use cases (e.g. when one nested class is a subclass of another nested class).
Note that mypy still emits an error on the original example, because it is technically unsafe. Note however that @mkurnikov works on a mypy plugin for Django that will suppress this error for Django specific use cases.
ilevkivskyi commentedon Jan 9, 2019
Also for those who don't want to use the Django mypy plugin, or if you want to silence this error for other reason you can either just put
# type: ignoreon the subclass definition, or use this pattern:Note however it will disable many checks for
A.SubandB.Sub.