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

Validating subclasses #807

Closed
timonbimon opened this issue Sep 14, 2019 · 2 comments · Fixed by #808
Closed

Validating subclasses #807

timonbimon opened this issue Sep 14, 2019 · 2 comments · Fixed by #808
Labels

Comments

@timonbimon
Copy link
Contributor

@timonbimon timonbimon commented Sep 14, 2019

Question

I was wondering what the best way to validate subclasses is?
Something along the sorts of

import pydantic
class MyClass:
    pass
class MySubClass(MyClass):
    pass
class MyOtherClass:
    pass

class Foo(BaseModel):
    a_class_not_an_object: Type[MyClass]

where I would expect both

Foo(a_class_not_an_object=MyClass)
Foo(a_class_not_an_object=MySubClass)

to work, but

Foo(a_class_not_an_object=MyOtherClass)

to throw an error.
Is the way to go to implement a custom type or is there a way pydantic supports this out-of-the-box?

Have a good weekend :)

@timonbimon timonbimon added the question label Sep 14, 2019
@samuelcolvin

This comment has been minimized.

Copy link
Owner

@samuelcolvin samuelcolvin commented Sep 14, 2019

Might be possible to achieve this now using validator decorators. Please let us know whether or not you get it to work.

I'd be happy to accept a PR to implement it out of the box if it's something multiple people want.

@timonbimon

This comment has been minimized.

Copy link
Contributor Author

@timonbimon timonbimon commented Sep 14, 2019

Alright, I've been freeriding long enough. 😉
Just made a MR that should resolve the issue. :)
Thanks for making an awesome library. pydantic rocks 💯

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants
You can’t perform that action at this time.