mypy support of translated fields
See original GitHub issueWell, I’m probably doing something wrong, but I integrated django-modeltranslation
into my project long ago and could have missed something.
When I run today mypy .
I get error: "Tournament" has no attribute "name_en"
, because I am accessing the self.name_en
field: self.slug = slugify(self.name_en)
. Well, I do not define translated fields by myself in models, I supposed that those fields are added magically into the models.
If this page is actual:
https://django-modeltranslation.readthedocs.io/en/latest/usage.html#accessing-translated-and-translation-fields
and if I understand correctly then every translated field should be appended to models after python manage.py update_translation_fields
, but I only get some logs:
Using default language: en
Working on models: ...
Updating data of model '<class '...'>'
Updating data of model '<class '...'>'
Updating data of model '<class '...'>'
Updating data of model '<class '...'>'
and there are no changes in the models.
After manual adding fields with postfix _{lang}
I get the error: ValueError: Error adding translation field. Model '...' already contains a field named 'name_en'.
and when I’m getting into the source code, then it seems like those lines shouldn’t be appended into my model, but should be “created” on the run. If so, then the docs are out of date and I suppose mypy should always fail.
BTW thank you for your work. You’ve done a great job!
Issue Analytics
- State:
- Created 2 years ago
- Comments:5
Top GitHub Comments
Fields like
name_en
meant to be dynamic, and not supposed to live in user code.If you want mypy to correctly recognize it, we need to write something like this: https://github.com/typeddjango/django-stubs/tree/master/mypy_django_plugin
But, i actually think it’s better to treat those fields as
private
and not use them in actual application code. And prefer solutions liketranslation.override
. This way - all complexity stays at translation level, which is handled by django and django-modeltranslation. And it compatible with any of typecheckers, like pyright, not only with mypy.I agree. Therefore I am closing this issue. Thanks!