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

Forward Reference Validation #14

Open
mlamby opened this issue Oct 10, 2021 · 0 comments
Open

Forward Reference Validation #14

mlamby opened this issue Oct 10, 2021 · 0 comments

Comments

@mlamby
Copy link
Contributor

mlamby commented Oct 10, 2021

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?

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

1 participant