question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[FEATURE] Inheritance and Polymorphism support

See original GitHub issue

Actually, there is no support for Inheritance and Polymorphism as I know in fastapi : https://swagger.io/docs/specification/data-models/inheritance-and-polymorphism/

Maybe annotations like that will do the job :

class Item(BaseModel):
    type: str

class CarItem(Item):
    type: str

class PlaneItem(Item):
    type: str
    size: str


@app.post("/items/", response_model=Item, one_of=[CarItem, PlaneItem], discriminator="type")
async def create_item(*, item: Item):
    return item

For now, we need to define an any response_model since defining an Item response_model will remove the extra attributes during serialization.

I’ll try to hack using the based starlette api : https://www.starlette.io/schemas/ but comment are unused by fastapi (except for description)

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:1
  • Comments:22 (12 by maintainers)

github_iconTop GitHub Comments

9reactions
rapiz1commented, Apr 10, 2020

I think real inheritance and polymorphism support would be very useful. Comments above show that using typing.Union forces users to carefully put the class in order and is not a generic solution.

4reactions
sm-Fifteencommented, May 21, 2020

@mcauto: Ah, that’s a common mistake people make. Pydantic can’t tell what data structure your JSON is supposed to be aside from matching it against one of your possible models. As it happens, since Woman adds no new fields to Human and Man does, there’s no way for Pydantic to tell whether the payload you sent it just a Woman object (essentially a Human object) with extra fields or a Man object, since both match.

If you really want the absence of something to define whether or not your Human subtype is Man or Woman, I believe you can just have Woman forbid the presence of extra fields, like so:

class Woman(Human):
    """ woman """
    class Config:
        extra = 'forbid'

(I have not tested it myself, though, so apologies if this doesn’t work)

Read more comments on GitHub >

github_iconTop Results From Across the Web

5.4: Difference between Inheritance and Polymorphism
Inheritance : Inheritance is one in which a new class is created that inherits the properties of the already exist class. It supports...
Read more >
Understanding Java Inheritance and Polymorphism - Section.io
In inheritance, we create new classes that inherit features of the superclass while polymorphism decides what form of method to execute.
Read more >
Chapter 13. Inheritance and Polymorphism
Inheritance is a language construct that supports the sharing of features amongst different objects. Consider the domain of vehicles, which includes bicycles, ...
Read more >
Important Facts About Inheritance and Polymorphism
Two of the benefits of OT are code reusability and extensibility, and inheritance allows the implementation of both of these features. When new...
Read more >
Polymorphism vs. Inheritance: Difference Between ... - upGrad
Inheritance is essentially making a class, and then having other classes in your program get their feature form the already existing base class....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found