Skip to content

Error importing plugin 'mypy_django_plugin.main' #166

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

Closed
zacchiro opened this issue Sep 17, 2019 · 14 comments
Closed

Error importing plugin 'mypy_django_plugin.main' #166

zacchiro opened this issue Sep 17, 2019 · 14 comments
Labels
bug Something isn't working mypy-plugin Issues specific to mypy_django_plugin stale Pull requests that have been out of date or unmergeable for a long time.

Comments

@zacchiro
Copy link

(As per #160, I'm forking a dedicated issue for this.)

I'm using mypy 0.720 with django-stubs 1.1.0 (from PyPI today):

$ mypy --version
mypy 0.720
$ pip show django-stubs
Name: django-stubs
Version: 1.1.0

With plugins = mypy_django_plugin.main in mypy.ini, mypy fails as follows:

mypy -p foo.bar
../mypy.ini:12: error: Error importing plugin 'mypy_django_plugin.main'

even though mypy_django_plugin loads just fine.

I've tried dropping .main from the mypy.ini and that gives:

../mypy.ini:12: error: Plugin 'mypy_django_plugin' does not define entry point function "plugin"

I've also tried replacing .main with :main (which seems to be the current recommended way to specify entry points in mypy.ini) and that gives:

../mypy.ini:12: error: Plugin 'mypy_django_plugin' does not define entry point function "main"
@zacchiro zacchiro added the bug Something isn't working label Sep 17, 2019
@blueyed
Copy link
Contributor

blueyed commented Sep 17, 2019

../mypy.ini:12: error: Error importing plugin 'mypy_django_plugin.main'

Please check what the exact error is (need to patch/debug mypy; see python/mypy#7512).

@zacchiro
Copy link
Author

zacchiro commented Sep 17, 2019 via email

@blueyed
Copy link
Contributor

blueyed commented Sep 17, 2019

Try exporting DJANGO_SETTINGS_MODULE additionally.
I would print the whole traceback based on my above PR (you might leave a comment that it (having a traceback) would be useful over there then) (__import__("traceback").print_stack()), or use a debugger there - to see where it is coming from.

the module in question is already used for django testing by pytest,

Are you running mypy directly or through some pytest plugin?

@zacchiro
Copy link
Author

zacchiro commented Sep 17, 2019 via email

@blueyed
Copy link
Contributor

blueyed commented Sep 17, 2019

It should not be necessary to export it. (#153 is a way to only use the exported env.)

So it looks something is setting up Django during import time likely for you.
The traceback would be useful therefore.

@zacchiro
Copy link
Author

zacchiro commented Sep 17, 2019 via email

@blueyed
Copy link
Contributor

blueyed commented Sep 17, 2019

@zacchiro please fix the formatting.

@blueyed
Copy link
Contributor

blueyed commented Sep 17, 2019

Appears to be triggered via:

  File "/my/home/dir/.virtualenvs/swh/lib/python3.7/site-packages/mypy_django_plugin/main.py", line 14, in <module>
    from mypy_django_plugin.django.context import DjangoContext

Without investigating it looks like the plugin's setting up of Django should happen before this already then.

@zacchiro
What Django version is this?

@zacchiro
Copy link
Author

zacchiro commented Sep 17, 2019 via email

@ibraraslam
Copy link

was facing the same issue, but placing [mypy.plugins.django-stubs] before setting plugins in setup.cfg fixed it for me.

@zacchiro
Copy link
Author

zacchiro commented Nov 1, 2019

was facing the same issue, but placing [mypy.plugins.django-stubs] before setting plugins in setup.cfg fixed it for me.

I think that just make you not load the plugin at all, because then the plugins= key is no longer in the top-level stanza of mypy.ini

@benjamin-kirkbride
Copy link

I am having the same issue, and adding DJANGO_SETTINGS_MODULE to my .profile did not make a difference.

Not sure how to proceed.

@sklarsa
Copy link

sklarsa commented Dec 19, 2019

I'm able to repro this in Django v2 as well.

This happens at module import time, before the DjangoContext gets initialized (and sets up django). A hacky fix is to move the ArrayField import block inside the DjangoContext.__init__ func after the initialize_django call. This works because the class is only referenced inside an instantiated version of the class. Although I admit, this is not a viable long-term solution.

An example of this is here: master...sklarsa:issue-166

@flaeppe
Copy link
Member

flaeppe commented Mar 11, 2023

This issue is quite outdated. Gonna close it, feel free to reopen if it's still a relevant problem

@flaeppe flaeppe closed this as not planned Won't fix, can't repro, duplicate, stale Mar 11, 2023
@intgr intgr added mypy-plugin Issues specific to mypy_django_plugin stale Pull requests that have been out of date or unmergeable for a long time. labels Mar 11, 2023
voidus pushed a commit to voidus/django-stubs that referenced this issue Apr 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working mypy-plugin Issues specific to mypy_django_plugin stale Pull requests that have been out of date or unmergeable for a long time.
Development

No branches or pull requests

7 participants