Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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
AlexWaygood merged 9 commits intopython:mainfromsobolevn:issue-112618
Dec 2, 2023
Merged
Show file tree
Hide file tree
Changes from1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
NextNext commit
gh-112618: MakeAnnotated cache typed
  • Loading branch information
@sobolevn
sobolevn committedDec 2, 2023
commitbcb274c9d47525db7e5d69c282ff970dffe51282
27 changes: 27 additions & 0 deletionsLib/test/test_typing.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -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)


class TypeAliasTests(BaseTestCase):
def test_canonical_usage_with_variable_annotation(self):
Expand Down
14 changes: 11 additions & 3 deletionsLib/typing.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -497,6 +497,13 @@ def __getitem__(self, parameters):
return self._getitem(self, *parameters)


class _AnnotatedSpecialForm(_SpecialForm, _root=True):
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:
Expand DownExpand Up@@ -2005,8 +2012,9 @@ def __mro_entries__(self, bases):
return (self.__origin__,)


@_SpecialForm
def Annotated(self, params):
@_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
Expand DownExpand Up@@ -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.
"""
ifnot isinstance(params, tuple) orlen(params) < 2:
if len(params) < 2:
raise TypeError("Annotated[...] should be used "
"with at least two arguments (a type and an "
"annotation).")
Expand Down
View file
Open in desktop
Original file line numberDiff line numberDiff 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.

[8]ページ先頭

©2009-2025 Movatter.jp