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

Please show me the error you're getting when trying to import my settings #1346

Closed
colons opened this issue Jan 26, 2023 · 7 comments
Closed
Labels
bug Something isn't working mypy-plugin Issues specific to mypy_django_plugin

Comments

@colons
Copy link

colons commented Jan 26, 2023

Bug report

What's wrong

django-stubs is complaining about an import cycle, but I don't get to see it and the project otherwise runs fine.

Specifically, I get errors whenever anything accesses a settings attribute with a message like error: Import cycle from Django settings module prevents type inference for 'LOGGING' [misc].

Since I do not know what the error actually is, and there is no obvious place around the place that message is sent (transformers/settings.py) to retrieve it, I have no idea how to start fixing this.

How is that should be

I would like for there to be an option to see the full traceback of the error being alluded to here. It'd be cool if that option could be mentioned in the message about it, too.

I may be completely misunderstanding what this message means. If that's the case, I feel like it should probably be rephrased, because I'm struggling to come up with an interpretation other than "we got an ImportError accessing your settings". If that's not true, or if it's possible the error could be something other than an import cycle, the message should be changed. Either way, any additional detail about the nature of the problem would be very helpful.

If there is another bug here, I'll try to make a smaller reproducible case once I'm able to see the actual error.

System information

  • OS: linux (various)
  • python version: 3.10
  • django version: 3.2.16
  • mypy version: 0.991
  • django-stubs version: i've seen this on 1.13.2 and 1.14.0
  • django-stubs-ext version: 0.7.0
@colons colons added the bug Something isn't working label Jan 26, 2023
@intgr
Copy link
Collaborator

intgr commented Jan 26, 2023

Related:

Maybe @ngnpope knows how to debug this?

@intgr intgr added the mypy-plugin Issues specific to mypy_django_plugin label Jan 26, 2023
@flaeppe
Copy link
Member

flaeppe commented Jan 26, 2023

Check if this works: #1163 (comment)

Also, I think the import cycle error is emitted by mypy. So it's out of our control changing the message.

@flaeppe
Copy link
Member

flaeppe commented Jan 26, 2023

My interpretation of the message is that; when mypy tries to infer(basically figure out for itself) the type for the 'LOGGING' variable. It runs in to an import cycle, due to Django settings module.

But what the cycle is only mypy knows. And I agree this could probably get a much more informative message

@colons
Copy link
Author

colons commented Jan 26, 2023

Also, I think the import cycle error is emitted by mypy. So it's out of our control changing the message.

The text of the error message I'm talking about comes from code in this project.

@colons
Copy link
Author

colons commented Jan 26, 2023

Check if this works: #1163 (comment)

Adding explicit annotations to the settings module used for type checking does seem to be helping, thank you.

It feels weird to have to do this at all, since all the settings are already defined here, they're just defined as imports from other settings modules, all of which mypy correctly resolves when I use reveal_type() on them within the settings file being used by django-stubs. I don't have nearly the knowledge to understand what adding explicit annotations is even changing here, as far as mypy is concerned. It's certainly not fixing an import cycle, though. It doesn't even seem to have anything to do with imports at all? I feel like this error message directed me in almost entirely the wrong direction to fix this. I spent a decent amount of time trying to fix import-related issues that I assumed were happening but I couldn't see.

@mschoettle
Copy link
Contributor

mschoettle commented Feb 8, 2023

I just ran into this issue but only by pre-commit invoking mypy, not when manually running mypy.

  • Python: 3.10.9
  • Django: 3.2.17
  • mypy: 0.991
  • django-stubs: 1.14.0

It happens in a models.py where settings.LANGUAGES is accessed at the module level.

Update: The suggested solution by @flaeppe referenced above solved this issue. Thanks!

@flaeppe
Copy link
Member

flaeppe commented May 6, 2024

Since the issue here seems to be fixed with #1163 (comment) and there's an ongoing discussion if we can find an improvement in #2097. I'll close this for now

@flaeppe flaeppe closed this as completed May 6, 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
Development

No branches or pull requests

4 participants