-
-
Notifications
You must be signed in to change notification settings - Fork 485
Requiring field annotations even though type can be inferred from field type #68
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
Did you set |
Yup (and other Django type hints seem to be working). Here are my two config files:
[mypy]
plugins =
mypy_django_plugin.main
[mypy_django_plugin]
django_settings = penny.settings
ignore_missing_settings = True
ignore_missing_model_attributes = True |
Bump, have any ideas? Is this expected behavior? If not, where can we look to solve it? Thanks! |
I'm also seeing this issue with
Has anyone found a workaround for this? |
We just added manual type annotation comments to all the fields, never found a solution unfortunately. |
@pirate have you tried |
Yup, still happening on
|
What IDE are you using? If you invoke mypy from the command line, does it work then? If you manage to run it correctly and types are revealed with reveal_type in the cli, then it's your IDE setup that isn't working. |
This issue occurs for me outside of my IDE, running |
Confirmed here as well, it's not limited to the IDE. All other django stubs are working fine too, it's just the model Fields that are having this problem. |
I've run into this as well. When I ran into it, I noticed that it was occurring anywhere I was using
@pirate @ncvc either of you using |
Got the same error at 1.0.2 |
We do happen to be using We have a different custom base model that we use for everything |
@ntietz' solution seems to work for those using django_extensions. |
Just confirming that this didn't have anything to do with |
It would be good to have this check every class defined in a typical |
For me this actually results in a type error with the |
So, like,
look at |
You only have to make sure the last class passed to extend from in that example, at some point, extends models.Model. In Python, the last class passed takes precedence over the rest which should only be mixins. |
I've added processing of Field instantiation for mixin classes in #167, but after more thorough reading of the issue, it doesn't seem to be all that needed to be done. |
Nearly all my errors go away when i don't inherit from |
I never worked with |
thanks @mkurnikov let me know if i can help |
@mkurnikov that fix in #177 looks GIS-specific, I don't think it will fix the general case of |
That PR doesn't solve the issue for those using django_extension classes like TimestampedModel. This issue should be kept open. |
I think the idea is like that: if you have a base model which is type annotated (mypy knows that it's a If it's not, then mypy will infer UPD.
should solve the issue. On runtime this clause will never be executed, but mypy will know that it's a |
Having the same problem using another ModelBase (MPTTModel from django-mptt), I can confirm this: if TYPE_CHECKING:
reveal_type(MPTTModel)
class Resource(MPTTModel):
author = models.ForeignKey(User, on_delete=models.SET_NULL, null=True)
short_description = models.CharField(max_length=255)
long_description = models.TextField(default="")
dependencies = models.ManyToManyField("self")
metadata = models.ForeignKey(Metadata, on_delete=models.PROTECT)
file = models.PositiveIntegerField(default=file_auto_increment, db_index=True)
creation = models.DateTimeField(auto_now_add=True)
previous = TreeForeignKey("self", on_delete=models.PROTECT, null=True, related_name=") I got : note: Revealed type is 'Any'
error: Need type annotation for 'author'
error: Need type annotation for 'short_description'
error: Need type annotation for 'long_description'
error: Need type annotation for 'dependencies'
error: Need type annotation for 'metadata'
error: Need type annotation for 'file'
error: Need type annotation for 'creation' And in my case, if TYPE_CHECKING:
class MPTTModel(MPTTModel, models.Model):
pass Did not solve the problem: Name 'MPTTModel' already defined (possibly by an import)
error: Need type annotation for 'author'
error: Need type annotation for 'short_description'
error: Need type annotation for 'long_description'
error: Need type annotation for 'dependencies'
error: Need type annotation for 'metadata'
error: Need type annotation for 'file'
error: Need type annotation for 'creation' Using: if TYPE_CHECKING:
class MPTTModel(models.Model):
pass
else:
from mptt.models import MPTTModel Solved the problem of attribute annotation, but I obviously got complaints about method declared in MPTTModel : error: "Resource" has no attribute "get_level"
error: "Resource" has no attribute "get_ancestors"; maybe "ancestors"?
error: "Resource" has no attribute "version"
[...] |
Would this work?
(just tried it locally, seems fine) |
This one did work for me, thanks ! |
if TYPE_CHECKING:
from third_party.models import BaseModel as _BaseModel
class BaseModel(_BaseModel, models.Model):
pass
else:
from third_party.models import BaseModel This is a bit of a gnarly a hack to require adding at the top of many files all over a large codebase. Is there no generic solution to fix this for all base models? >>> from core.models import Version # our model that inherits from a custom BaseModel
>>> from django.db import models
>>> issubclass(Version, models.Model)
True Given that they're still |
What's a result of
in your case? |
|
For anyone following this, @mkurnikov's new solution worked for me! #182 It looks like it's properly checking all of the model base classes in the inheritance chain now, and I no longer get Thanks for the quick turnaround time on that fix over the last week @mkurnikov! |
Will that PR get merged? Is there going to be a release soon? |
@Tatsh if you need it asap you can install it from the branch: pip install -e 'git+https://github.com/mkurnikov/django-stubs@custom-model-base#egg=django-stubs'
# or
pipenv install ... |
@Tatsh Release is planned a week or two after 0.730 is released. As for merge, I don't know, I'll try to make it till the end of the week. |
Right now mypy is complaining that my fields aren't annotated with types.
Is this expected behavior? If so, why? Cant the type be inferred from the field value itself?
Thanks!
The text was updated successfully, but these errors were encountered: