Skip to content
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
Next Next commit
gh-115165: Fix Annotated for immutable types
The return value from an annotated callable can raise any exception from
__setattr__ for the `__orig_class__` property.
  • Loading branch information
dave-shawley committed Feb 9, 2024
commit b87995b94dd1ca6ff19963174fcbfcf6e7c15e94
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)

Comment thread
AlexWaygood marked this conversation as resolved.
Outdated

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:
Comment thread
AlexWaygood marked this conversation as resolved.
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__``
Comment thread
AlexWaygood marked this conversation as resolved.
Outdated
attribute on objects returned from a callable. Previously only ``TypeError``
was ignored.
Comment thread
AlexWaygood marked this conversation as resolved.
Outdated