django-stubs: Type from settings not correctly loaded in a model
Bug report
I have this in settings:
BANK_ENTITIES = [('a', 'A Bank'), ('b', 'B Bank'),]
and this in my model:
entity = models.CharField(max_length=20, choices=settings.BANK_ENTITIES, blank=True)
What’s wrong
mypy fails with:
models.py:247: error: Argument "choices" to "CharField" has incompatible type "tuple"; expected "Optional[Iterable[Union[Tuple[Any, Any], Tuple[str, Iterable[Tuple[Any, Any]]]]]]"
If I place a reveal in settings and another in models.py I get
settingsbase.py:90: note: Revealed type is 'builtins.list[Tuple[builtins.str, builtins.str]]'
models.py:214: note: Revealed type is 'builtins.list'
How is that should be
mypy should be able to load the full type from settings, not only that it’s a list
System information
- OS: Ubuntu 18.04
pythonversion: 3.6djangoversion: 2.2mypyversion: 0.761django-stubsversion: 1.4.0
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Reactions: 6
- Comments: 22 (15 by maintainers)
Commits related to this issue
- Add failing test case to illustrate #312 Signed-off-by: Anders Kaseorg <andersk@mit.edu> — committed to andersk/django-stubs by andersk 2 years ago
- Disable fallback to runtime types for Django settings This fallback to value.__class__ seems to be doing more harm than good; see #312 and #1162. Replace it with a clear error message that suggests ... — committed to andersk/django-stubs by andersk 2 years ago
- Disable fallback to runtime types for Django settings This fallback to value.__class__ seems to be doing more harm than good; see #312 and #1162. Replace it with a clear error message that suggests ... — committed to andersk/django-stubs by andersk 2 years ago
- Disable fallback to runtime types for Django settings This fallback to value.__class__ seems to be doing more harm than good; see #312 and #1162. Replace it with a clear error message that suggests ... — committed to andersk/django-stubs by andersk 2 years ago
- Disable fallback to runtime types for Django settings This fallback to value.__class__ seems to be doing more harm than good; see #312 and #1162. Replace it with a clear error message that suggests ... — committed to andersk/django-stubs by andersk 2 years ago
- Disable fallback to runtime types for Django settings (#1163) This fallback to value.__class__ seems to be doing more harm than good; see #312 and #1162. Replace it with a clear error message that ... — committed to typeddjango/django-stubs by andersk 2 years ago
Any chance to move this issue forward? Configuration keeps producing quite a lot of warnings and it is a bit difficult to work around it.
Hello, I have the same issue, I tried to understand why it happens. In this function, plugin cant extract type from
symvariable, because it’s dict variable? after that, the plugin tries to understand what`s type, based on the variable value. https://github.com/typeddjango/django-stubs/blob/9a95b983981173d86b55c3ede97f637d94cfde9e/mypy_django_plugin/transformers/settings.py#L23-L49 In the case with dict value, the plugin gets type from builtins module. https://github.com/python/mypy/blob/master/mypy/typeshed/stdlib/builtins.pyi#L788I think, the plugin needs to check if its dict and return dict type from mypy module, like
If it’s the good solution I will make PR.
I think I have a related issue
and mypy gives the following error:
The
_KTTypeVar appears to be defined in mypy typeshed in relation toMappingcontainer, i.e. for dictsAdding an annotation to the settings like this does not help:
we also have similar situation with conditional import of sub-module settings, maybe is relevant
(my example code above is simplified so might not reproduce the issue by itself)
I am able to make mypy happy with the following workaround:
(and no annotation in
settings.py)