question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

mypy support of translated fields

See original GitHub issue

Well, 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:closed
  • Created 2 years ago
  • Comments:5

github_iconTop GitHub Comments

1reaction
last-partizancommented, Jan 25, 2022

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 like translation.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.

0reactions
ppeczekcommented, Jan 25, 2022

I agree. Therefore I am closing this issue. Thanks!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Mypy Documentation
Mypy is a static type checker for Python. Type checkers help ensure that you're using variables and functions in your code correctly.
Read more >
Generic inference regression from #5699 · Issue #5738 - GitHub
Since #5699, mypy now rejects the following program (minimized from a few failures at dropbox): from typing import TypeVar, Callable, ...
Read more >
mypy reports errors on custom defined builtins - Stack Overflow
I'm using the gettext package to perform translations in the Python application. There's a custom Translation class ...
Read more >
mypy-zope - PyPI
Plugin for mypy to support zope interfaces. ... In context of an interface, some known zope.schema field types are automatically translated to python...
Read more >
django-extensions Changelog - pyup.io
Improvement: fix translation interpolation in prospective arabic translations ... Fix: sqldiff, improve support of GIS fields by using Django introspection
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found