Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork33.7k
gh-112618: MakeAnnotated cache typed#112619
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Merged
Uh oh!
There was an error while loading.Please reload this page.
Merged
Changes from1 commit
Commits
Show all changes
9 commits Select commitHold shift + click to select a range
bcb274c gh-112618: Make `Annotated` cache typed
sobolevn3b57302 Update 2023-12-02-12-55-17.gh-issue-112618.7_FT8-.rst
sobolevn70895b4 Update Misc/NEWS.d/next/Library/2023-12-02-12-55-17.gh-issue-112618.7…
sobolevn03b43b3 Rename type
sobolevnb9a0c33 Address review
sobolevn13eb192 Address review
sobolevn0a842a0 Update 2023-12-02-12-55-17.gh-issue-112618.7_FT8-.rst
sobolevnb83154b Apply suggestions from code review
sobolevn5025f1c fix syntaxerror
AlexWaygoodFile filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
NextNext commit
gh-112618: Make
Annotated cache typed- Loading branch information
Uh oh!
There was an error while loading.Please reload this page.
commitbcb274c9d47525db7e5d69c282ff970dffe51282
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -8675,6 +8675,33 @@ class X(Annotated[int, (1, 10)]): ... | ||
| self.assertEqual(X.__mro__, (X, int, object), | ||
| "Annotated should be transparent.") | ||
| def test_annotated_cached_with_types(self): | ||
| class A(str): ... | ||
| class B(str): ... | ||
| class Message: | ||
| field_a: Annotated[str, A("X")] | ||
| field_b: Annotated[str, A("Y")] | ||
| field_c: Annotated[int, 1] | ||
| class Form: | ||
| box_a: Annotated[str, B("X")] | ||
| box_b: Annotated[str, B("Y")] | ||
| box_c: Annotated[int, 1.0] | ||
| hints = get_type_hints(Message, include_extras=True) | ||
| self.assertEqual(type(hints["field_a"].__metadata__[0]), A) | ||
| self.assertEqual(hints["field_a"].__metadata__[0], A("X")) | ||
| self.assertEqual(type(hints["field_b"].__metadata__[0]), A) | ||
| self.assertEqual(hints["field_b"].__metadata__[0], A("Y")) | ||
| self.assertEqual(type(hints["field_c"].__metadata__[0]), int) | ||
| hints = get_type_hints(Form, include_extras=True) | ||
| self.assertEqual(type(hints["box_a"].__metadata__[0]), B) | ||
| self.assertEqual(hints["box_a"].__metadata__[0], B("X")) | ||
| self.assertEqual(type(hints["box_b"].__metadata__[0]), B) | ||
| self.assertEqual(hints["box_b"].__metadata__[0], B("Y")) | ||
| self.assertEqual(type(hints["box_c"].__metadata__[0]), float) | ||
sobolevn marked this conversation as resolved. OutdatedShow resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| class TypeAliasTests(BaseTestCase): | ||
| def test_canonical_usage_with_variable_annotation(self): | ||
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 |
|---|---|---|
| @@ -497,6 +497,13 @@ def __getitem__(self, parameters): | ||
| return self._getitem(self, *parameters) | ||
| class _AnnotatedSpecialForm(_SpecialForm, _root=True): | ||
AlexWaygood marked this conversation as resolved. OutdatedShow resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| def __getitem__(self, parameters): | ||
| if not isinstance(parameters, tuple): | ||
| parameters = (parameters,) | ||
| return self._getitem(self, *parameters) | ||
| class _AnyMeta(type): | ||
| def __instancecheck__(self, obj): | ||
| if self is Any: | ||
| @@ -2005,8 +2012,9 @@ def __mro_entries__(self, bases): | ||
| return (self.__origin__,) | ||
| @_AnnotatedSpecialForm | ||
| @_tp_cache(typed=True) | ||
| def Annotated(self, *params): | ||
| """Add context-specific metadata to a type. | ||
| Example: Annotated[int, runtime_check.Unsigned] indicates to the | ||
| @@ -2053,7 +2061,7 @@ def Annotated(self, params): | ||
| where T1, T2 etc. are TypeVars, which would be invalid, because | ||
| only one type should be passed to Annotated. | ||
| """ | ||
| if len(params) < 2: | ||
| raise TypeError("Annotated[...] should be used " | ||
| "with at least two arguments (a type and an " | ||
| "annotation).") | ||
2 changes: 2 additions & 0 deletionsMisc/NEWS.d/next/Library/2023-12-02-12-55-17.gh-issue-112618.7_FT8-.rst
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,2 @@ | ||
| Make ``typing.Annotated`` cache work different for different types. Now | ||
| ``Annoated[int, 1]`` and ``Annotated[int, 1.0]`` will be different. |
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.