mirror of
https://github.com/python/cpython.git
synced 2025-08-04 08:59:19 +00:00
[3.11] gh-115165: Fix typing.Annotated
for immutable types (GH-115213) (#115228)
gh-115165: Fix `typing.Annotated` for immutable types (GH-115213)
The return value from an annotated callable can raise any exception from
__setattr__ for the `__orig_class__` property.
(cherry picked from commit 564385612c
)
Co-authored-by: dave-shawley <daveshawley@gmail.com>
This commit is contained in:
parent
ca3eca1584
commit
7542210a95
3 changed files with 29 additions and 1 deletions
|
@ -3665,6 +3665,17 @@ class GenericTests(BaseTestCase):
|
|||
c.bar = 'abc'
|
||||
self.assertEqual(c.__dict__, {'bar': 'abc'})
|
||||
|
||||
def test_setattr_exceptions(self):
|
||||
T = TypeVar("T")
|
||||
class Immutable(Generic[T]):
|
||||
def __setattr__(self, key, value):
|
||||
raise RuntimeError("immutable")
|
||||
|
||||
# gh-115165: This used to cause RuntimeError to be raised
|
||||
# when we tried to set `__orig_class__` on the `Immutable` instance
|
||||
# returned by the `Immutable[int]()` call
|
||||
self.assertIsInstance(Immutable[int](), Immutable)
|
||||
|
||||
def test_subscripted_generics_as_proxies(self):
|
||||
T = TypeVar('T')
|
||||
class C(Generic[T]):
|
||||
|
@ -7397,6 +7408,17 @@ class AnnotatedTests(BaseTestCase):
|
|||
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"]
|
||||
# gh-115165: This used to cause RuntimeError to be raised
|
||||
# when we tried to set `__orig_class__` on the `C` instance
|
||||
# returned by the `A()` call
|
||||
self.assertIsInstance(A(), C)
|
||||
|
||||
def test_cannot_instantiate_forward(self):
|
||||
A = Annotated["int", (5, 6)]
|
||||
with self.assertRaises(TypeError):
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue