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
{{ message }}
Repository navigation
Cannot determine type of attribute class in base class #4956
scratch.py:11: error: Cannot determine type of 'Thing' in base class 'Foo'
Adding object doesn't help, nor #type: ignore as shown above.
However, Bar.Thingdoes type check; so there seems only to be a problem typing literal class-valued class attributes, when inherited. (Aside: many thanks if you can tell me the proper terminology for these Bar.Thing and Foo.Things - it's not easy to search for!)
Note Foo.Thing is not a problem until the inheriting class is introduced on L11.
This is not a bug but how mypy works. The error message can be made clearer however. The point is that inner classes are treated as types, not variables (i.e. as statically known), they cannot be re-assigned, but can be used in type annotations. On the contrary, an instance variable with type Type[Thing] (this is what is created by the assignment in your example) can be re-assigned, but can't be used in type annotations.
@ilevkivskyi Many thanks for your reply. I think it might help to explain why this is causing me a problem.
My Foo and Bar are really Django model 'mixins' (in quotes because they must inherit from Django's Model in order to provide extra ORM fields) and the inner class Thing is really Django's Meta, (not an actual metaclass) necessary to specify Meta.abstract = True in order not to create extra database tables for the 'mixin' itself.
The problem then is that, with Meta as inner class, mypy cannot determine its type; with Meta as instance variable, it conflicts with multiple 'mixins' (inheritance):
Support of Django is a separate project and will require writing a plugin, see #3539 (there are too much magic to typecheck normally).
Also it looks like we already have a very similar issue to yours #3855 with a similar motivation. So I think we can close this, feel free to re-open if you think this is a separate issue.
Is there any way to workaround this though? At this point I would be quite happy with # type: ignore, or similar, but that doesn't work. Or is it simply not possible to use mypy in a Django project with this pattern?
Activity
ilevkivskyi commentedon Apr 24, 2018
This is not a bug but how mypy works. The error message can be made clearer however. The point is that inner classes are treated as types, not variables (i.e. as statically known), they cannot be re-assigned, but can be used in type annotations. On the contrary, an instance variable with type
Type[Thing](this is what is created by the assignment in your example) can be re-assigned, but can't be used in type annotations.OJFord commentedon Apr 24, 2018
@ilevkivskyi Many thanks for your reply. I think it might help to explain why this is causing me a problem.
My
FooandBarare really Django model 'mixins' (in quotes because they must inherit from Django'sModelin order to provide extra ORM fields) and the inner classThingis really Django'sMeta, (not an actual metaclass) necessary to specifyMeta.abstract = Truein order not to create extra database tables for the 'mixin' itself.The problem then is that, with
Metaas inner class, mypy cannot determine its type; withMetaas instance variable, it conflicts with multiple 'mixins' (inheritance):ilevkivskyi commentedon Apr 24, 2018
Support of Django is a separate project and will require writing a plugin, see #3539 (there are too much magic to typecheck normally).
Also it looks like we already have a very similar issue to yours #3855 with a similar motivation. So I think we can close this, feel free to re-open if you think this is a separate issue.
OJFord commentedon Apr 24, 2018
Thanks; apologies - it's certainly a duplicate.
Is there any way to workaround this though? At this point I would be quite happy with
# type: ignore, or similar, but that doesn't work. Or is it simply not possible to use mypy in a Django project with this pattern?ilevkivskyi commentedon Apr 24, 2018
I think
# type: ignoreat the point of use (i.e. at line 11 in your example) should work.OJFord commentedon Apr 24, 2018
Nope, it complains before then, i.e. myRealModelis in a different file, but running mypy on just the file with the 'mixins' fails with:# type: ignoreon theMetainner class has no impact; nor does putting it on the outer 'mixin' modelFoo.ilevkivskyi commentedon Apr 24, 2018
Can you put
# type: ignoreon the line where it is reported? (This is how ignores are supposed to work, they just silence errors.)OJFord commentedon Apr 24, 2018
Oh no - sorry - I'm not sure how I screwed that up, that does work. Many thanks!
syenchuk commentedon Nov 8, 2018
FYI, similar issues #2871 #3855