-
-
Notifications
You must be signed in to change notification settings - Fork 458
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
Disable fallback to runtime types for Django settings #1163
Conversation
c30c4b9
to
486607a
Compare
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]>
486607a
to
9a0e146
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Am I right that we should either merge this or #1162 ?
I think @PIG208 concluded that #1162 doesn’t really solve the problem. It happens to make my reduced test case pass, but doesn’t work on our actual large code base. And it probably regresses the type checking of My proposal is to close #1162 and merge this, unless someone comes up with a reliable way to make the circular dependencies work. (In which case we should still merge this, since the fallback would then switch from unreliable to unnecessary.) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This feels like a safer solution to me.
So, let's remove the fallback for now.
Any other PRs that can solve the whole problem is welcome!
By the way, thanks for the exceptional responsiveness to our PRs. With this merged, I think we have everything we need to enable django-stubs in Zulip (zulip/zulip#18777)! |
I am glad that you are happy :) It is much easier than any other project I work on. |
With this change we're now getting the |
@ljodal if you could find a minimal reproducer - we will be glad to fix it! |
It seems to be happening for a random selection of our settings, from simple strings to |
Ok, let's consider this a release blocker. Something that we need to investigate before releasing our next version. |
@ljodal My guess would be that your settings module imports something that transitively imports (You might find a command like |
Thanks! You are very much correct, both With regards to blocking a release I think maybe tweaking the error message a bit or adding something about this in the release notes should be enough. The official sentry docs say to set it up in settings, so I assume this might hit a few users |
Yeah, I had the same issue with |
Maybe we could add a section to the readme about this and link to that from the error message? I would assume a lot of people will end up having this issue once they upgrade. The error message pops up in kinda unrelated places, so I think it's hard to backtrack from that to the underlaying cause here. |
I dug a bit further and in addition to |
If there is a way for us to find out the dependency cycle through mypy's API, we can provide a more informative error message. We should probably also note that the cyclic imports are not the only source of dependencies cycle for our plugin, since we inject some extra dependencies for certain modules exposed only to mypy. |
The I note that Django itself avoids importing def gettext_noop(s: str) -> str:
return s
LANGUAGES = [
("de", gettext_noop("German")),
("en", gettext_noop("English")),
] This will mark the strings for translation without actually translating them, which should be fine because Django itself will translate them anyway. |
Yeah, in our case I think I can easily work around this. That said this is a gotcha, so if possible it would be great if the error message could be a bit more helpful to point people in the right direction. At least I did not intuitively understand how my local settings file would cause an import cycle if other projects import |
I have quite a tricky project on my hands that has the same "cyclic imports" issue (#1025 (comment)). I've untangled the following ways how settings depend on settings so far:
Maybe, it's possible to untangle it all, but I see it as a pretty challenging task. I'm interested in finding a way for django-stubs to get around cyclic imports rather than trying to rewrite the project for the sake of django-stubs. Is there a way to monkey-patch |
You can use different |
That's a great idea! Also quite hard for this particular project, though, there are like 10k lines of spaghetti 👀 Still, I might autogenerate fake settings, maybe. |
Can confirm this create a lot of false positives I've tried the Thanks to you all for your contributions ! |
I am getting cyclic errors since 9bd8aed.
I'm mystified 😆 I'm grateful for the effort and the time - 🙏. Can I ask you make a second PR with tests, documentation, and migration notes? |
Oof. Apparently this continues to be problematic for users, please open an issue for discussion about possible solutions. |
@intgr you can take the initiative on that, why not. Looks like you have a sample code of when it goes wrong, that would be a great discussion starter. |
Nope, this is the first time I heard of this issue. I have no sample code. |
I've not got a minimal reproduction, but I followed the advice above to track down this issue to sentry_sdk, but not |
@jgillard As per above, moving your |
OK. I was wondering if another change was due for this issue, as it's not obvious that this is the suggested solution without reading this thread. So the hope is that the sentry-docs suggestion gets merged then. |
Revisiting this I realised that whenever one has explicit settings declared in a
It was possible to get rid of it by explicitly annotate the setting variable instead of having mypy figure it out by itself ...
SOME_SETTING: int = func_returning_int()
... |
Oh my, it works! Thank you! |
…jango#1163)" This reverts commit 9bd8aed.
Created a Discussion along with new category "Common issues and solutions" |
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.