-
-
Notifications
You must be signed in to change notification settings - Fork 444
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support passing TypedDicts to WithAnnotations
- Loading branch information
Showing
8 changed files
with
255 additions
and
65 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,22 @@ | ||
from typing import Any, Type, TypeVar | ||
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: | ||
def __init__(self, **kwargs: Type[Any]): | ||
pass | ||
|
||
class Annotations(Generic[_Annotations]): | ||
"""Use as `Annotations[MyTypedDict]`""" | ||
|
||
pass | ||
|
||
|
||
_T = TypeVar("_T", bound=Model) | ||
|
||
WithAnnotations = Annotated[_T, Annotations()] | ||
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
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
Oops, something went wrong.