-
-
Notifications
You must be signed in to change notification settings - Fork 553
Use Model instead of Self in parms and add AltersData inheritance
#3228
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
Open
emmanuel-ferdman
wants to merge
3
commits into
typeddjango:master
Choose a base branch
from
emmanuel-ferdman:fix-self-contravariant
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.
+44
−4
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
051a546
Use `Model` instead of `Self` in parameters and add `AltersData` inhe…
emmanuel-ferdman 33ed24e
Use `Model` instead of `Self` in parameters and add `AltersData` inhe…
emmanuel-ferdman 173f747
Use `Model` instead of `Self` in parameters and add `AltersData` inhe…
emmanuel-ferdman 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 hidden or 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 hidden or 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 hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| from collections.abc import Iterable | ||
|
|
||
| from django.db import models | ||
| from django.db.models.query import QuerySet | ||
| from typing_extensions import assert_type, override | ||
|
|
||
|
|
||
| class MyModel(models.Model): | ||
| class Meta: | ||
| app_label = "myapp" | ||
|
|
||
|
|
||
| class OtherModel(models.Model): | ||
| class Meta: | ||
| app_label = "myapp" | ||
|
|
||
|
|
||
| # Override with QuerySet[Model] — valid | ||
| class OverrideWithModel(models.Model): | ||
| @override | ||
| def refresh_from_db( | ||
| self, | ||
| using: str | None = None, | ||
| fields: Iterable[str] | None = None, | ||
| from_queryset: QuerySet[models.Model] | None = None, | ||
| ) -> None: ... | ||
|
|
||
| class Meta: | ||
| app_label = "myapp" | ||
|
|
||
|
|
||
| def test_correct_queryset(m: MyModel, qs: QuerySet[MyModel]) -> None: | ||
| m.refresh_from_db(from_queryset=qs) | ||
| assert_type(m.refresh_from_db(), None) | ||
|
|
||
|
|
||
| def test_wrong_queryset(m: MyModel, qs: QuerySet[OtherModel]) -> None: | ||
| m.refresh_from_db(from_queryset=qs) # type: ignore[arg-type] # pyright: ignore[reportArgumentType] # pyrefly: ignore[bad-argument-type] | ||
Oops, something went wrong.
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.
MyModelwon't work here, right?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.
Right,
QuerySet[MyModel]errors on both mypy and pyright since it narrows the parameter type. Should we add another test case for it?Uh oh!
There was an error while loading. Please reload this page.
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.
Yes, please. But, it seems kinda right to have it typed as
QuerySet[MyModel]?Because passing
from_queryset=QuerySet[SomeOther]with no error does seem like a false negative to me.Do you have any ideas on how we can make this right?
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.
We could try
self: _SelfwithQuerySet[_Self]- this ties the queryset to the actual model type. Souser.refresh_from_db(from_queryset=article_qs)would correctly error. It's stricter than runtime sinceQuerySet[Model]gets rejected for typed subclass instances, but I think passing a wrong model's queryset is a bug waiting to happen anyway :)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.
Let's try this :)
Uh oh!
There was an error while loading. Please reload this page.
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.
Code updated :)