-
-
Notifications
You must be signed in to change notification settings - Fork 484
Type from settings not correctly loaded in a model #312
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
Can you please post the result of |
I posted it in the initial report:
|
Oh, sorry, I have missed it. Yes, looks like a bug! |
This comment has been minimized.
This comment has been minimized.
@lfrodrigues could you verify if this bug still occurs? I wasn't able to reproduce it at all. I guess that someone fixed this in the meantime, or maybe your |
I just tested it again. If I just have a simple settings.py file like the one created when django starts a project there is no problem. The problem is that my settings import different sub modules for settings depending on the country that was configured. Here's a demo repository: from django.conf import settings
def view():
a = settings.MY_SETTINGS['item']['subitem']
|
@lfrodrigues Thank you for the tips, I'll try to fix this as soon as I have enough time. |
I think I have a related issue # settings.py
SOME_DICT = {
'a': 'whatever',
'b': 'whatever',
'c': 'whatever',
} # myproject/mymodule.py
from django.conf import settings
whatever = settings.SOME_DICT["b"] and mypy gives the following error:
The Adding an annotation to the settings like this does not help: # settings.py
from typing import Dict
SOME_DICT: Dict[str, str] = {
'a': 'whatever',
'b': 'whatever',
'c': 'whatever',
}
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: # myproject/mymodule.py
from typing import Dict
from django.conf import settings
_some_dict: Dict[str, str] = settings.SOME_DICT
whatever = _some_dict["b"] (and no annotation in |
this looks the same #352 |
I am experiencing the same bug with django-stubs 1.8.0 and django-stubs-ext 0.2.0. Defining dictionaries in a settings file and than importing them via
becomes
|
@kszmigiel, are you still intending to fix this or should it be unassigned? |
@RJPercival feel free to take this over! 👍 |
Hello, I have the same issue, I tried to understand why it happens. In this function, plugin cant extract type from django-stubs/mypy_django_plugin/transformers/settings.py Lines 23 to 49 in 9a95b98
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#L788 class dict(MutableMapping[_KT, _VT], Generic[_KT, _VT]): I think, the plugin needs to check if its dict and return dict type from mypy module, like Dict[Any, Any] If it's the good solution I will make PR. |
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. |
Here’s a complete minimal reproducible test case in ~20 lines. Note that
from django.conf import settings
def test() -> None:
reveal_type(settings.MY_LIST)
from django.conf import settings
def test() -> None:
reveal_type(settings.MY_LIST)
# empty
from django.db import models
import bad
class MyUser(models.Model):
pass
from other import other
AUTH_USER_MODEL = "my_app.MyUser"
INSTALLED_APPS = ["my_app"]
MY_LIST = [1]
from django_stubs_ext import ValuesQuerySet
other = ()
[tool.django-stubs]
django_settings_module = "my_settings"
[tool.mypy]
plugins = ["mypy_django_plugin.main"] Reproduction: $ pip install Django \
'git+https://github.com/typeddjango/django-stubs.git@6f9afd3c#egg=django-stubs[compatible-mypy]' \
'git+https://github.com/typeddjango/django-stubs.git@6f9afd3c#subdirectory=django_stubs_ext'
$ pip freeze
asgiref==3.5.2
Django==4.1.1
django-stubs @ git+https://github.com/typeddjango/django-stubs.git@6f9afd3cc9202a3d1147f78e53ef31ced2f03452
django-stubs-ext==0.6.0
mypy==0.971
mypy-extensions==0.4.3
sqlparse==0.4.2
tomli==2.0.1
types-pytz==2022.2.1.0
types-PyYAML==6.0.11
typing_extensions==4.3.0
$ mypy .
bad.py:4: note: Revealed type is "builtins.list"
good.py:4: note: Revealed type is "builtins.list[builtins.int]"
Success: no issues found in 6 source files Notes:
|
In my test case, during the first run of
so
so It seems to me that falling back to the runtime type isn’t how a static type checker should ever behave. I don’t know much about the mypy API, but if the static type hasn’t been inferred yet, surely there must be a way to trigger mypy to finish inferring the type and/or delay until it has done so. |
The reproduction seems plausible, but I'm not yet able to reproduce it in the pytest environment. I guess this is very specific to how the modules are set up and how mypy is invoked. There is a |
Signed-off-by: Anders Kaseorg <[email protected]>
Deferring type analysis for an attribute seems to be tricky, as I can't find direct support for that. The only relevant plugin API for deferring is only supported for |
Actually, this might have more to do with the |
Tracking the debugging process in #1162. We need to come up with a proper fix for this issue. |
This fallback to value.__class__ seems to be doing more harm than good; see typeddjango#312 and typeddjango#1162. Replace it with a clear error message that suggests a way to fix the problem rather than papering over it. Signed-off-by: Anders Kaseorg <[email protected]>
This fallback to value.__class__ seems to be doing more harm than good; see typeddjango#312 and typeddjango#1162. Replace it with a clear error message that suggests a way to fix the problem rather than incompletely papering over it. Signed-off-by: Anders Kaseorg <[email protected]>
This fallback to value.__class__ seems to be doing more harm than good; see typeddjango#312 and typeddjango#1162. Replace it with a clear error message that suggests a way to fix the problem rather than incompletely papering over it. Signed-off-by: Anders Kaseorg <[email protected]>
This fallback to value.__class__ seems to be doing more harm than good; see typeddjango#312 and typeddjango#1162. Replace it with a clear error message that suggests a way to fix the problem rather than incompletely papering over it. Signed-off-by: Anders Kaseorg <[email protected]>
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 a way to fix the problem rather than incompletely papering over it. Signed-off-by: Anders Kaseorg <[email protected]> Signed-off-by: Anders Kaseorg <[email protected]>
Bug report
I have this in settings:
and this in my model:
What's wrong
mypy fails with:
If I place a reveal in settings and another in models.py I get
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
python
version: 3.6django
version: 2.2mypy
version: 0.761django-stubs
version: 1.4.0The text was updated successfully, but these errors were encountered: