Skip to content

Structuring typing.NewType classes #173

Closed

Description

@Savlik
  • 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.

Activity

  1. Tinche commented on Feb 7, 2022

    @Tinche
    Member

    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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions