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
10 changes: 10 additions & 0 deletions Lib/test/test_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -8756,6 +8756,16 @@ 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
2 changes: 1 addition & 1 deletion Lib/typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -1127,7 +1127,7 @@ def __call__(self, *args, **kwargs):
result = self.__origin__(*args, **kwargs)
try:
result.__orig_class__ = self
except AttributeError:
except Exception:
pass
return result

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
``typing.Annotated`` ignores exceptions when setting the ``__orig_class__``
attribute on objects returned from a callable. Previously only ``TypeError``
was ignored.
0