Skip to content

Commit

Permalink
Fix TypeError on nested Annotated types where the inner type has …
Browse files Browse the repository at this point in the history
…unhashable metadata (#417)
  • Loading branch information
AlexWaygood authored Jun 1, 2024
1 parent d76f591 commit 8dfcf3c
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 2 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

- Preliminary changes for compatibility with the draft implementation
of PEP 649 in Python 3.14.
- Fix regression in v4.12.0 where nested `Annotated` types would cause
`TypeError` to be raised if the nested `Annotated` type had unhashable
metadata.

# Release 4.12.0 (May 23, 2024)

Expand Down
8 changes: 8 additions & 0 deletions src/test_typing_extensions.py
Original file line number Diff line number Diff line change
Expand Up @@ -4769,6 +4769,14 @@ def test_annotated_in_other_types(self):
X = List[Annotated[T, 5]]
self.assertEqual(X[int], List[Annotated[int, 5]])

def test_nested_annotated_with_unhashable_metadata(self):
X = Annotated[
List[Annotated[str, {"unhashable_metadata"}]],
"metadata"
]
self.assertEqual(X.__origin__, List[Annotated[str, {"unhashable_metadata"}]])
self.assertEqual(X.__metadata__, ("metadata",))


class GetTypeHintsTests(BaseTestCase):
def test_get_type_hints(self):
Expand Down
4 changes: 2 additions & 2 deletions src/typing_extensions.py
Original file line number Diff line number Diff line change
Expand Up @@ -2958,9 +2958,9 @@ def _has_generic_or_protocol_as_origin() -> bool:
except AttributeError:
return False # err on the side of leniency
else:
return frame.f_locals.get("origin") in {
return frame.f_locals.get("origin") in (
typing.Generic, Protocol, typing.Protocol
}
)


_TYPEVARTUPLE_TYPES = {TypeVarTuple, getattr(typing, "TypeVarTuple", None)}
Expand Down

0 comments on commit 8dfcf3c

Please sign in to comment.