Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pydantic AttributeError: '' object has no attribute '__fields_set__'

from pydantic import BaseModel

class A(BaseModel):
    date = ''

class B(A):
    person: float

    def __init__(self):
        self.person = 0
    
B()

tried to initiate class B but raised error AttributeError: 'B' object has no attribute '__fields_set__', why is it?

like image 506
Jace Yang Avatar asked May 26 '26 23:05

Jace Yang


1 Answers

It's because you override the __init__ and do not call super there so Pydantic cannot do it's magic with setting proper fields.

With pydantic it's rare you need to implement your __init__ most cases can be solved different way:

from pydantic import BaseModel

class A(BaseModel):
    date = ""

class B(A):
    person: float = 0

B()
like image 175
kosciej16 Avatar answered May 28 '26 13:05

kosciej16



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!