Skip to content
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

TypeError from inspect.type_info with unhashable Annotated metadata #565

Closed
peterschutt opened this issue Oct 11, 2023 · 0 comments · Fixed by #566
Closed

TypeError from inspect.type_info with unhashable Annotated metadata #565

peterschutt opened this issue Oct 11, 2023 · 0 comments · Fixed by #566

Comments

@peterschutt
Copy link
Contributor

Description

MCVE

from typing import Annotated

import msgspec


class Test(msgspec.Struct):
    a: Annotated[int, {"unhashable": "metadata"}]


t = Test(a=1)

print(t.a)  # 1 (no runtime issues)

msgspec.inspect.type_info(Test)  # TypeError: unhashable type: 'dict'
 

Error

 Traceback (most recent call last):
  File "/home/peter/PycharmProjects/msgspec-mcve/main.py", line 14, in <module>
    msgspec.inspect.type_info(Test)
  File "/home/peter/PycharmProjects/msgspec-mcve/venv/lib/python3.10/site-packages/msgspec/inspect.py", line 614, in type_info
    return multi_type_info([type])[0]
  File "/home/peter/PycharmProjects/msgspec-mcve/venv/lib/python3.10/site-packages/msgspec/inspect.py", line 580, in multi_type_info
    return _Translator(types).run()
  File "/home/peter/PycharmProjects/msgspec-mcve/venv/lib/python3.10/site-packages/msgspec/inspect.py", line 721, in run
    return tuple(self.translate(t) for t in self.types)
  File "/home/peter/PycharmProjects/msgspec-mcve/venv/lib/python3.10/site-packages/msgspec/inspect.py", line 721, in <genexpr>
    return tuple(self.translate(t) for t in self.types)
  File "/home/peter/PycharmProjects/msgspec-mcve/venv/lib/python3.10/site-packages/msgspec/inspect.py", line 755, in translate
    out = self._translate_inner(t, args, **constrs)
  File "/home/peter/PycharmProjects/msgspec-mcve/venv/lib/python3.10/site-packages/msgspec/inspect.py", line 903, in _translate_inner
    type=self.translate(hints[name]),
  File "/home/peter/PycharmProjects/msgspec-mcve/venv/lib/python3.10/site-packages/msgspec/inspect.py", line 724, in translate
    t, args, metadata = _origin_args_metadata(typ)
  File "/home/peter/PycharmProjects/msgspec-mcve/venv/lib/python3.10/site-packages/msgspec/inspect.py", line 622, in _origin_args_metadata
    origin = _CONCRETE_TYPES.get(t)
  File "/usr/lib/python3.10/typing.py", line 1651, in __hash__
    return hash((self.__origin__, self.__metadata__))
TypeError: unhashable type: 'dict'

There doesn't seem to be any hard requirement that type metadata via Annotated must be hashable (searching for "hash" in pep 593 doesn't yield anything), however, the implementation of _AnnotatedAlias.__hash__() does hash any metadata as well as the origin type (ref):

    def __hash__(self):
        return hash((self.__origin__, self.__metadata__))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant