Skip to content
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

'reveal_type' is not defined when using mypy_django_plugin #590

Closed
blueyed opened this issue Apr 13, 2021 · 11 comments · Fixed by #591
Closed

'reveal_type' is not defined when using mypy_django_plugin #590

blueyed opened this issue Apr 13, 2021 · 11 comments · Fixed by #591
Labels
bug Something isn't working

Comments

@blueyed
Copy link
Contributor

blueyed commented Apr 13, 2021

Using reveal_type in a module fails when using plugins = project.mypy_django_plugin (via [mypy] section in setup.cfg):

% mypy project/app/admin.py
Error constructing plugin instance of NewSemanalDjangoPlugin

Traceback (most recent call last):
  File "…/project/.venv/bin/mypy", line 8, in <module>
    sys.exit(console_entry())
  File "…/project/.venv/lib/python3.9/site-packages/mypy/__main__.py", line 11, in console_entry
    main(None, sys.stdout, sys.stderr)
  File "mypy/main.py", line 90, in main
  File "mypy/build.py", line 179, in build
  File "mypy/build.py", line 228, in _build
  File "mypy/build.py", line 472, in load_plugins
  File "mypy/build.py", line 450, in load_plugins_from_config
  File "…/project/.venv/lib/python3.9/site-packages/mypy_django_plugin/main.py", line 104, in __init__
    self.django_context = DjangoContext(django_settings_module)
  File "…/project/.venv/lib/python3.9/site-packages/mypy_django_plugin/django/context.py", line 88, in __init__
    apps, settings = initialize_django(self.django_settings_module)
  File "…/project/.venv/lib/python3.9/site-packages/mypy_django_plugin/django/context.py", line 72, in initialize_django
    apps.populate(settings.INSTALLED_APPS)
  File "…/project/.venv/lib/python3.9/site-packages/django/apps/registry.py", line 122, in populate
    app_config.ready()
  File "…/project/.venv/lib/python3.9/site-packages/django/contrib/admin/apps.py", line 24, in ready
    self.module.autodiscover()
  File "…/project/.venv/lib/python3.9/site-packages/django/contrib/admin/__init__.py", line 24, in autodiscover
    autodiscover_modules('admin', register_to=site)
  File "…/project/.venv/lib/python3.9/site-packages/django/utils/module_loading.py", line 47, in autodiscover_modules
    import_module('%s.%s' % (app_config.name, module_to_search))
  File "/usr/lib/python3.9/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1030, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
  File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 790, in exec_module
  File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
  File "…/project/app/operations/admin.py", line 928, in <module>
    reveal_type(FooAdmin)
NameError: name 'reveal_type' is not defined

System information

  • OS:
  • python version: Python 3.9.2
  • django version: Django 3.1.8
  • mypy version: mypy 0.812
  • django-stubs version: f003968 (current master)
@blueyed blueyed added the bug Something isn't working label Apr 13, 2021
@sobolevn
Copy link
Member

sobolevn commented Apr 13, 2021

Yes, this happens because we first run django.setup() and reveal_type is not defined in runtime.

I suggest to use this hack:

from typing import TYPE_CHECKING

if not TYPE_CHECKING:
   reveal_type = print

@blueyed
Copy link
Contributor Author

blueyed commented Apr 13, 2021

Yes, this happens because we first run django.setup() and reveal_type is not defined in runtime.

Thanks for the explanation and the workaround.
I think this warrants an answer/mention in the README/FAQ probably, doesn't it? (I've wondered not having found an issue for it already).

But I'd also like to clarify if there isn't something that could be done to fix/improve this in general, i.e. in case plugins are not really meant to import modules / run code already (what (the "custom") django.setup() does (

if not settings.configured:
settings._setup()
apps.populate(settings.INSTALLED_APPS)
)); maybe there is a later (mypy) plugin hook that should/could be used?

As for the hack the following appears to be more feasible (as an editor snippet/shortcut then):

from typing import TYPE_CHECKING

if TYPE_CHECKING:
    reveal_type(expr)

And django-stubs itself could maybe add something like the following (while running "django.setup()")?

        from typing import TYPE_CHECKING

        if not TYPE_CHECKING:
            import builtins

            builtins.reveal_type = lambda _: None
            builtins.reveal_locals = lambda: None

@sobolevn
Copy link
Member

Hm, probably we can add builtins patch into https://github.com/typeddjango/django-stubs/tree/master/django_stubs_ext 🤔

Do you want to work on this?

@sobolevn sobolevn reopened this Apr 13, 2021
@blueyed
Copy link
Contributor Author

blueyed commented Apr 13, 2021

Hm, probably we can add builtins patch into https://github.com/typeddjango/django-stubs/tree/master/django_stubs_ext thinking

Sounds good!

Do you want to work on this?

=> #591

blueyed added a commit to blueyed/django-stubs that referenced this issue Apr 13, 2021
sobolevn added a commit that referenced this issue Apr 14, 2021
…ns (#591)

* WIP: django_stubs_ext: monkeypatch `reveal_{type,locals}` into builtins

Fixes #590

* fixup! WIP: django_stubs_ext: monkeypatch `reveal_{type,locals}` into builtins

* fixup! fixup! WIP: django_stubs_ext: monkeypatch `reveal_{type,locals}` into builtins

* Update patch.py

Co-authored-by: Nikita Sobolev <mail@sobolevn.me>
@blueyed
Copy link
Contributor Author

blueyed commented Apr 14, 2021

@sobolevn
After doing #591 I've thought that it might be better to only do this patching during the "django.setup()" - like initially suggested here.

I think it a) should not require to use the patch() method, and b) should not be in place during runtime in general (after the (apparently necessary/unavoidable) "setup").

@sobolevn
Copy link
Member

Yes, looks like it is a good idea indeed. But, 0.2.0 is already released, we can rework how it is done in the 0.3.0.

@sebastian-philipp
Copy link
Contributor

facing the same issue right now

@sobolevn
Copy link
Member

You have reveal_type somewhere in your code, don't forget to use from typing import reveal_type

@sebastian-philipp
Copy link
Contributor

sebastian-philipp commented Oct 16, 2023

sure about this?

$ python -m venv venv                                      
$ . ./venv/bin/activate          
$ pip install mypy > /dev/null
$ echo "x: int = 0" > file.py
$ echo "reveal_type(x)" >> file.py
$ mypy file.py 
file.py:2: note: Revealed type is "builtins.int"
$

@sobolevn
Copy link
Member

Yes, I am sure :)

» echo "x: int = 0" > file.py                                                   
» echo "reveal_type(x)" >> file.py                                                                                                        
» python file.py
Traceback (most recent call last):
  File "/Users/sobolev/Desktop/mypy/file.py", line 2, in <module>
    reveal_type(x)
    ^^^^^^^^^^^
NameError: name 'reveal_type' is not defined

With import:

» echo "x: int = 0" > file.py                                                                                                       
» echo "from typing_extensions import reveal_type" >> file.py                          
» echo "reveal_type(x)" >> file.py                                                                              
» python file.py
Runtime type is 'int'

@intgr
Copy link
Collaborator

intgr commented Oct 16, 2023

Mypy doesn't require reveal_type import, but Python itself does require it to be imported.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Development

Successfully merging a pull request may close this issue.

4 participants