8000 gh-115165: Fix Annotated for immutable types by dave-shawley · Pull Request #115213 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-115165: Fix Annotated for immutable types #115213

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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Feb 9, 2024
Merged
Show file tree
Hide file tree
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
Small tweaks
  • Loading branch information
AlexWaygood committed Feb 9, 2024
commit 702ba2ca8d699ce0cb917081f19c077371fe1bf1
10 changes: 8 additions & 2 deletions Lib/test/test_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -4323,12 +4323,15 @@ class C(B[int]):
c.bar = 'abc'
self.assertEqual(c.__dict__, {'bar': 'abc'})

def test_getattr_exceptions(self):
def test_setattr_exceptions(self):
class Immutable[T]:
def __setattr__(self, key, value):
raise RuntimeError("immutable")

Immutable[int]()
# 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')
Expand Down Expand Up @@ -8505,6 +8508,9 @@ 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):
Expand Down
2 changes: 1 addition & 1 deletion Lib/typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -1128,7 +1128,7 @@ def __call__(self, *args, **kwargs):
try:
result.__orig_class__ = self
# Some objects raise TypeError (or something even more exotic)
# if you try to set attributes on them; guarding against that here
# if you try to set attributes on them; we guard against that here
except Exception:
pass
return result
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
``typing.Annotated`` now ignores most exceptions when attempting to set the
``__orig_class__`` attribute on objects returned when calling a generic alias.
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.
0