summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>2023-12-03 00:58:13 (GMT)
committerGitHub <noreply@github.com>2023-12-03 00:58:13 (GMT)
commitdb537702e4ff6801699204a72ec635e39d885735 (patch)
treea44da074438b242eec91346f078146980fccd1c1
parent8d8efe7e7148303b4ecffbe54109356c8ef4ea77 (diff)
downloadcpython-db537702e4ff6801699204a72ec635e39d885735.zip
cpython-db537702e4ff6801699204a72ec635e39d885735.tar.gz
cpython-db537702e4ff6801699204a72ec635e39d885735.tar.bz2
[3.11] [3.12] gh-112618: Make Annotated cache typed (GH-112619) (GH-112628) (#112633)
[3.12] gh-112618: Make Annotated cache typed (GH-112619) (GH-112628) (cherry picked from commit 2a378ca2efcb91db4b04fb4e052424fd610ffbc9) Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
-rw-r--r--Lib/test/test_typing.py34
-rw-r--r--Lib/typing.py9
-rw-r--r--Misc/NEWS.d/next/Library/2023-12-02-12-55-17.gh-issue-112618.7_FT8-.rst2
3 files changed, 43 insertions, 2 deletions
diff --git a/Lib/test/test_typing.py b/Lib/test/test_typing.py
index f663fbd..b0e3ca9 100644
--- a/Lib/test/test_typing.py
+++ b/Lib/test/test_typing.py
@@ -7627,6 +7627,40 @@ class AnnotatedTests(BaseTestCase):
self.assertEqual(X.__mro__, (X, int, object),
"Annotated should be transparent.")
+ def test_annotated_cached_with_types(self):
+ class A(str): ...
+ class B(str): ...
+
+ field_a1 = Annotated[str, A("X")]
+ field_a2 = Annotated[str, B("X")]
+ a1_metadata = field_a1.__metadata__[0]
+ a2_metadata = field_a2.__metadata__[0]
+
+ self.assertIs(type(a1_metadata), A)
+ self.assertEqual(a1_metadata, A("X"))
+ self.assertIs(type(a2_metadata), B)
+ self.assertEqual(a2_metadata, B("X"))
+ self.assertIsNot(type(a1_metadata), type(a2_metadata))
+
+ field_b1 = Annotated[str, A("Y")]
+ field_b2 = Annotated[str, B("Y")]
+ b1_metadata = field_b1.__metadata__[0]
+ b2_metadata = field_b2.__metadata__[0]
+
+ self.assertIs(type(b1_metadata), A)
+ self.assertEqual(b1_metadata, A("Y"))
+ self.assertIs(type(b2_metadata), B)
+ self.assertEqual(b2_metadata, B("Y"))
+ self.assertIsNot(type(b1_metadata), type(b2_metadata))
+
+ field_c1 = Annotated[int, 1]
+ field_c2 = Annotated[int, 1.0]
+ field_c3 = Annotated[int, True]
+
+ self.assertIs(type(field_c1.__metadata__[0]), int)
+ self.assertIs(type(field_c2.__metadata__[0]), float)
+ self.assertIs(type(field_c3.__metadata__[0]), bool)
+
class TypeAliasTests(BaseTestCase):
def test_canonical_usage_with_variable_annotation(self):
diff --git a/Lib/typing.py b/Lib/typing.py
index 95f855b..85166df 100644
--- a/Lib/typing.py
+++ b/Lib/typing.py
@@ -2214,9 +2214,14 @@ class Annotated:
def __new__(cls, *args, **kwargs):
raise TypeError("Type Annotated cannot be instantiated.")
- @_tp_cache
def __class_getitem__(cls, params):
- if not isinstance(params, tuple) or len(params) < 2:
+ if not isinstance(params, tuple):
+ params = (params,)
+ return cls._class_getitem_inner(cls, *params)
+
+ @_tp_cache(typed=True)
+ def _class_getitem_inner(cls, *params):
+ if len(params) < 2:
raise TypeError("Annotated[...] should be used "
"with at least two arguments (a type and an "
"annotation).")
diff --git a/Misc/NEWS.d/next/Library/2023-12-02-12-55-17.gh-issue-112618.7_FT8-.rst b/Misc/NEWS.d/next/Library/2023-12-02-12-55-17.gh-issue-112618.7_FT8-.rst
new file mode 100644
index 0000000..c732de1
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2023-12-02-12-55-17.gh-issue-112618.7_FT8-.rst
@@ -0,0 +1,2 @@
+Fix a caching bug relating to :data:`typing.Annotated`.
+``Annotated[str, True]`` is no longer identical to ``Annotated[str, 1]``.