-
-
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
QuerySet.annotate improvements #398
Merged
Merged
Changes from all commits
Commits
Show all changes
26 commits
Select commit
Hold shift + click to select a range
808ecd4
QuerySet.annotate returns self-type. Attribute access falls back to Any.
syastrov d4278f7
Fix .annotate so it reuses existing annotated types. Fixes error in t…
syastrov 922ed03
Fix self-typecheck error
syastrov c31a0a6
Fix flake8
syastrov dad53c2
Fix case of .values/.values_list before .annotate.
syastrov 63fcc8a
Extra ignores for Django 2.2 tests (false positives due to tests assu…
syastrov 2a319a8
More tests + more precise typing in case annotate called before value…
syastrov 33372f6
Test and fix annotate in combination with values/values_list with no …
syastrov db604d6
Remove line that does nothing :)
syastrov ebf9500
Formatting fixes
syastrov 6118634
Merge branch 'master' into queryset-annotate
syastrov a0d8e38
Address code review
syastrov 741a2d3
Fix quoting in tests after mypy changed things
syastrov 968d90b
Use Final
syastrov 7cc814d
Use typing_extensions.Final
syastrov a06ed52
Merge branch 'master' into queryset-annotate
syastrov eddce50
Fixes after ValuesQuerySet -> _ValuesQuerySet refactor. Still not pas…
syastrov 4d4dcf5
Fix inheritance of _ValuesQuerySet and remove unneeded type ignores.
syastrov 53ad2db
Merge branch 'master' into queryset-annotate
syastrov b8debd2
Make it possible to annotate user code with "annotated models", using…
syastrov 751ae7f
Add docs
syastrov 7108a0c
Make QuerySet[_T] an external alias to _QuerySet[_T, _T].
syastrov 51f0448
Support passing TypedDicts to WithAnnotations
syastrov 72c1dfb
Add an example of an error to README regarding WithAnnotations + Type…
syastrov a6a47da
Fix runtime behavior of ValuesQuerySet alias (you can't extend Any, f…
syastrov d3ea945
Fix issue when using from_queryset in some cases when having an argum…
syastrov 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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,6 @@ | ||
from .aliases import ValuesQuerySet as ValuesQuerySet | ||
from .annotations import Annotations as Annotations | ||
from .annotations import WithAnnotations as WithAnnotations | ||
from .patch import monkeypatch as monkeypatch | ||
|
||
__all__ = ["monkeypatch", "ValuesQuerySet"] | ||
__all__ = ["monkeypatch", "ValuesQuerySet", "WithAnnotations", "Annotations"] |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,10 @@ | ||
import typing | ||
|
||
if typing.TYPE_CHECKING: | ||
from django.db.models.query import _T, _Row, _ValuesQuerySet | ||
from django.db.models.query import _T, _QuerySet, _Row | ||
|
||
ValuesQuerySet = _ValuesQuerySet[_T, _Row] | ||
ValuesQuerySet = _QuerySet[_T, _Row] | ||
else: | ||
ValuesQuerySet = typing.Any | ||
from django.db.models.query import QuerySet | ||
|
||
ValuesQuerySet = QuerySet |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
from typing import Any, Generic, Mapping, TypeVar | ||
|
||
from django.db.models.base import Model | ||
from typing_extensions import Annotated | ||
|
||
# Really, we would like to use TypedDict as a bound, but it's not possible | ||
_Annotations = TypeVar("_Annotations", covariant=True, bound=Mapping[str, Any]) | ||
|
||
|
||
class Annotations(Generic[_Annotations]): | ||
"""Use as `Annotations[MyTypedDict]`""" | ||
|
||
pass | ||
|
||
|
||
_T = TypeVar("_T", bound=Model) | ||
|
||
WithAnnotations = Annotated[_T, Annotations[_Annotations]] | ||
"""Alias to make it easy to annotate the model `_T` as having annotations `_Annotations` (a `TypedDict` or `Any` if not provided). | ||
|
||
Use as `WithAnnotations[MyModel]` or `WithAnnotations[MyModel, MyTypedDict]`. | ||
""" |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
from typing import Any | ||
|
||
from django_stubs_ext import ValuesQuerySet | ||
|
||
|
||
def test_extends_values_queryset() -> None: | ||
class MyQS(ValuesQuerySet[Any, Any]): | ||
pass |
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.
Will it be possible for us to later add something like
WithAnnotations[Model, TypedDict]
and not break things for users? Double checking 🙂