Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Structure failed with typing_extensions.Any #488

Closed
WEGFan opened this issue Jan 16, 2024 · 3 comments · Fixed by #490
Closed

Structure failed with typing_extensions.Any #488

WEGFan opened this issue Jan 16, 2024 · 3 comments · Fixed by #490
Milestone

Comments

@WEGFan
Copy link

WEGFan commented Jan 16, 2024

  • cattrs version: 23.2.3
  • Python version: 3.9.18, 3.10.13
  • Operating System: WSL-Ubuntu-22.04

Description

Structure failed and raises cattrs.errors.StructureHandlerNotFoundError: Unsupported type: typing_extensions.Any. Register a structure hook for it.. It succeeds if I use typing.Any.

typing_extension version is 4.9.0

What I Did

>>> import cattrs
>>> import typing as t
>>> import typing_extensions as te
>>> cattrs.structure(1, te.Any)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python3.10/site-packages/cattrs/converters.py", line 332, in structure
    return self._structure_func.dispatch(cl)(obj, cl)
  File "/usr/local/lib/python3.10/site-packages/cattrs/fns.py", line 17, in raise_error
    raise StructureHandlerNotFoundError(msg, type_=cl)
cattrs.errors.StructureHandlerNotFoundError: Unsupported type: typing_extensions.Any. Register a structure hook for it.
>>> cattrs.structure(1, t.Any)
1
>>> cattrs.structure([1], list[te.Any])
  + Exception Group Traceback (most recent call last):
  |   File "<stdin>", line 1, in <module>
  |   File "/usr/local/lib/python3.10/site-packages/cattrs/converters.py", line 332, in structure
  |     return self._structure_func.dispatch(cl)(obj, cl)
  |   File "/usr/local/lib/python3.10/site-packages/cattrs/converters.py", line 531, in _structure_list
  |     raise IterableValidationError(
  | cattrs.errors.IterableValidationError: While structuring list[typing_extensions.Any] (1 sub-exception)
  +-+---------------- 1 ----------------
    | Traceback (most recent call last):
    |   File "/usr/local/lib/python3.10/site-packages/cattrs/converters.py", line 521, in _structure_list
    |     res.append(handler(e, elem_type))
    |   File "/usr/local/lib/python3.10/site-packages/cattrs/fns.py", line 17, in raise_error
    |     raise StructureHandlerNotFoundError(msg, type_=cl)
    | cattrs.errors.StructureHandlerNotFoundError: Unsupported type: typing_extensions.Any. Register a structure hook for it.
    | Structuring list[typing_extensions.Any] @ index 0
    +------------------------------------
>>> cattrs.structure([1], list[t.Any])
[1]
@Tinche
Copy link
Member

Tinche commented Jan 16, 2024

Ugh, why does typing_extensions have their own Any type now?

In the mean time, you can do this to enable support:

from typing_extensions import Any

from cattrs import Converter

c = Converter()

c.register_structure_hook_func(lambda t: t is Any, lambda v, _: v)

@Tinche Tinche added this to the 24.1 milestone Jan 16, 2024
@WEGFan
Copy link
Author

WEGFan commented Jan 16, 2024

@Tinche
Copy link
Member

Tinche commented Jan 16, 2024

I guess because it's possible to subclass Any now. I can add support for it in the next version.

@Tinche Tinche linked a pull request Jan 21, 2024 that will close this issue
# for free to join this conversation on GitHub. Already have an account? # to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants