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

Structuring typing.NewType classes #173

Closed
Savlik opened this issue Oct 6, 2021 · 1 comment
Closed

Structuring typing.NewType classes #173

Savlik opened this issue Oct 6, 2021 · 1 comment

Comments

@Savlik
Copy link

Savlik commented Oct 6, 2021

  • cattrs version: 1.8.0
  • Python version: 3.8.10
  • Operating System: Linux

Description

I want to use typing.NewType in my code, but structuring these variables throws an exception.

What I Did

from typing import  NewType
from cattr import GenConverter

c = GenConverter()

MyType = NewType("MyType", str)
initial = MyType("test")

raw = c.unstructure(initial, MyType)
new = c.structure(raw, MyType)
assert initial == new
Traceback (most recent call last):
  File ".../test.py", line 10, in <module>
    new = c.structure(raw, MyType)
  File ".../lib/python3.8/site-packages/cattr/converters.py", line 294, in structure
    return self._structure_func.dispatch(cl)(obj, cl)
  File ".../lib/python3.8/site-packages/cattr/converters.py", line 360, in _structure_error
    raise StructureHandlerNotFoundError(msg, type_=cl)
cattr.errors.StructureHandlerNotFoundError: Unsupported type: <function NewType.<locals>.new_type at 0x7fb67c77a0d0>. Register a structure hook for it.

Of course, I can explicitly register structure hook for it, but it would be nice if it worked out of the box, if possible.

@Tinche
Copy link
Member

Tinche commented Feb 7, 2022

Dupe of #94 ? Might look into this soon though.

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

No branches or pull requests

2 participants