8000 ForwardRef and GenConverter do not seem to work together · Issue #201 · python-attrs/cattrs · GitHub
[go: up one dir, main page]

Skip to content
ForwardRef and GenConverter do not seem to work together #201
@aha79

Description

@aha79
  • cattrs version: 1.8.0 (attrs 21.2.0)
  • Python version: 3.9.7
  • Operating System: Windows 10

Description

When using recursive types (see example below), structure with GenConverter raises a StructureHandlerNotFoundError with the message "Unsupported type: ForwardRef('Y'). Register a structure hook for it"
.
The error does not happen with PEP 563 enabled. That means when we add 'from future import annotations' to the program and
replace y: typing.Optional['Y'] with y: typing.Optional[Y] everything works as expected.

What I Did

import typing
import cattr
import attr

@attr.define(auto_attribs=True)
class X:
    a: int
    b: float
    y: typing.Optional['Y']   #with PEP563 enabled:   y: typing.Optional[Y]   (that does not raise an exception) 

@attr.define(auto_a
5744
ttribs=True)
class Y:
    a: typing.List[int]
    x: X


converter = cattr.GenConverter()

data = { 'a': [1,2,3], 'x': {'a':1, 'b':2.000001, 'y': {'a': [], 'x': {'a': 5, 'b': 3.1, 'y': None}}} }
d = converter.structure( data, Y )  # <---- raises StructureHandlerNotFoundError    Unsupported type: ForwardRef('Y'). Register a structure hook for it

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

      0