Skip to content

Forward Reference Validation #14

@mlamby

Description

@mlamby

Thanks for creating this great library~

I have the following code that uses a forward reference per PEP484.

@dataclass_validate
@dataclass
class DC4:
    member1: str
    # member2 can be either a string or a DC4 instance
    member2: Union[str, "DC4"]

def test_forwardReference():
    DC4("Hello", "World")
    DC4("Hello", DC4("World", "Too"))

    # This should raise a validation error but doesn't
    with pytest.raises(TypeValidationError) as e:
        DC4("Hello", 1) 

At the moment I can set member2 to any value and no TypeValidationError is raised.

I suspect this is because the forward reference is not resolved by the dataclass library. If I print out the type of each field of DC4 I get the following:

member1: <class 'str'>
member2: typing.Union[str, ForwardRef('DC4')]

Is this a use case that you have considered?

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