Skip to content

Validating subclasses #807

@timonbimon

Description

@timonbimon

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 :)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions