https://github.com/python/cpython/commit/564385612cdf72c2fa8e629a68225fb2cd3b3d99
commit: 564385612cdf72c2fa8e629a68225fb2cd3b3d99
branch: main
author: dave-shawley <[email protected]>
committer: AlexWaygood <[email protected]>
date: 2024-02-09T22:11:37Z
summary:
gh-115165: Fix `typing.Annotated` for immutable types (#115213)
The return value from an annotated callable can raise any exception from
__setattr__ for the `__orig_class__` property.
files:
A Misc/NEWS.d/next/Library/2024-02-09-07-20-16.gh-issue-115165.yfJLXA.rst
M Lib/test/test_typing.py
M Lib/typing.py
diff --git a/Lib/test/test_typing.py b/Lib/test/test_typing.py
index 58566c4bfc821c..c3a092f3af3009 100644
--- a/Lib/test/test_typing.py
+++ b/Lib/test/test_typing.py
@@ -4323,6 +4323,16 @@ class C(B[int]):
c.bar = 'abc'
self.assertEqual(c.__dict__, {'bar': 'abc'})
+ def test_setattr_exceptions(self):
+ class Immutable[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]):
@@ -8561,6 +8571,17 @@ 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"]
+ # 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):
diff --git a/Lib/typing.py b/Lib/typing.py
index 347373f00956c7..914ddeaf504cd0 100644
--- a/Lib/typing.py
+++ b/Lib/typing.py
@@ -1127,7 +1127,9 @@ def __call__(self, *args, **kwargs):
result = self.__origin__(*args, **kwargs)
try:
result.__orig_class__ = self
- except AttributeError:
+ # Some objects raise TypeError (or something even more exotic)
+ # if you try to set attributes on them; we guard against that here
+ except Exception:
pass
return result
diff --git
a/Misc/NEWS.d/next/Library/2024-02-09-07-20-16.gh-issue-115165.yfJLXA.rst
b/Misc/NEWS.d/next/Library/2024-02-09-07-20-16.gh-issue-115165.yfJLXA.rst
new file mode 100644
index 00000000000000..73d3d001f07f3f
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2024-02-09-07-20-16.gh-issue-115165.yfJLXA.rst
@@ -0,0 +1,4 @@
+Most exceptions are now ignored when attempting to set the ``__orig_class__``
+attribute on objects returned when calling :mod:`typing` generic aliases
+(including generic aliases created using :data:`typing.Annotated`).
+Previously only :exc:`AttributeError`` was ignored. Patch by Dave Shawley.
_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-checkins.python.org/
Member address: [email protected]