Skip to content
Merged
Changes from 1 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
Prev Previous commit
Next Next commit
fixup! gh-115165: Fix Annotated for immutable types
Renamed test_annotated_callable_returning_immutable to
test_instantiate_immutable and moved it near the other instantiation
tests.
  • Loading branch information
dave-shawley committed Feb 9, 2024
commit 7289af40d46f34c92a3159de2a357b2a68c5269b
18 changes: 8 additions & 10 deletions Lib/test/test_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -8492,6 +8492,14 @@ def test_instantiate_generic(self):
self.assertEqual(MyCount([4, 4, 5]), {4: 2, 5: 1})
self.assertEqual(MyCount[int]([4, 4, 5]), {4: 2, 5: 1})

def test_instantiate_immutable(self):
class C:
def __setattr__(self, key, value):
raise Exception("should be ignored")

A = Annotated[C, "a decoration"]
self.assertIsInstance(A(), C)

def test_cannot_instantiate_forward(self):
A = Annotated["int", (5, 6)]
with self.assertRaises(TypeError):
Expand Down Expand Up @@ -8756,16 +8764,6 @@ class B(str): ...
self.assertIs(type(field_c2.__metadata__[0]), float)
self.assertIs(type(field_c3.__metadata__[0]), bool)

def test_annotated_callable_returning_immutable(self):
class C:
def __setattr__(self, name, value):
raise Exception('should be ignored')

class A(str): ...

annotated = Annotated[C, A("A")]
self.assertIsInstance(annotated(), C)


class TypeAliasTests(BaseTestCase):
def test_canonical_usage_with_variable_annotation(self):
Expand Down