-
-
Notifications
You must be signed in to change notification settings - Fork 454
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
Fix regression in '.bulk_update()' annotations for Django 3.2 #1114
Open
javulticat
wants to merge
1
commit into
typeddjango:master
Choose a base branch
from
javulticat:issue-1113
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
By changing to
Optional[int]
we've gone from having a correct return type for more recent versions to have an incorrect return type for all versions (don't thinkbulk_update
has ever returnedOptional[int]
).Could it work to wrap the
bulk_update
definition in an if-statement that branches on the django version instead?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.
That's a great idea, and actually what I attempted first, but I could not get it to work, and I'm having a hard time finding any examples on the internet that show that conditional type hinting logic based on the installed Django version is indeed possible. For example, I don't see any other example of that kind of logic occurring in this codebase, and, from what I understand, that has historically been the reason why each version of
django-stubs
has only ever claimed/attempted to support a single version of Django - because, AFAIK,mypy
is specifically only able to handle conditional logic in a few limited cases:sys.version_info
sys.platform
typing.TYPE_CHECKING
MYPY
(Source: https://mypy.readthedocs.io/en/stable/common_issues.html#python-version-and-system-platform-checks)
But if you want it to handle conditional logic based on anything else, such as the installed Django version, you're out of luck (from what I understand).
For example,
mypy
is fine with this (which isn't what we want, but just showing an example of how it can handle conditional logic based on one of the above thingsmypy
explicitly says it can handle):Results in no
mypy
error.But, if instead we do what we actually want to do, which is have a conditional on the Django version, not the Python version,
mypy
won't accept it:We get this
mypy
error:error: All conditional function variants must have identical signatures
I've tried all sorts of combinations with the things
mypy
allows conditional logic on with no luck:if sys.version_info and django.VERSION < (4,):
if sys.platform and django.VERSION < (4,):
if typing.TYPE_CHECKING and django.VERSION < (4,):
All result in the same
mypy
error:error: All conditional function variants must have identical signatures
I've also tried nesting the conditionals for each of the above cases too, in the hopes that it would somehow allow
mypy
to process everything under the conditional - for example:But, again, same error each time:
error: All conditional function variants must have identical signatures
I'm not having much luck in finding any information about this elsewhere (StackOverflow, Google, etc.) where someone has used
mypy
successfully in handling conditional logic outside of the 4 special cases it specifically supports, unfortunately. Hopefully someone more knowledgeable than me can come along and show me what I'm missing on how this can be possible 🙃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.
I see. This feels somewhat of a limitation that could be resolved through mypy. But I'm not at all sure.
What I'm thinking is that if mypy exposes some sort of hook for plugins to declare these sort of constants by themselves.
Until we can find support for declaring 2 versions of the
bulk_update
signature I'd lean towards havingdjango-stubs
exporting types for newer versions of Django. But perhaps that's just me.