Home > Back-end >  Python dataclass with inheritance: __init__() missing 1 required positional argument
Python dataclass with inheritance: __init__() missing 1 required positional argument

Time:12-12

Trying my luck with inheritance with data classes (Python 3.9.13).

Given the following:

from dataclasses import dataclass
from datetime import datetime


@dataclass()
class BookMetadata():
    '''Parent class.'''
    
    isbn: str 
    title: str 
    author: str
    publisher: str
    date_published: int
    

    def __post_init__(self):
        '''Change attributes after assignment.'''
            
        # Change date from UNIX to YYYY-MM-DD HH:MM:SS
        self.date_published = datetime.fromtimestamp(int(str(self.date_published))).strftime('%Y-%m-%d %H:%M:%S')
      

@dataclass()
class RetailPrice(BookMetadata):
    '''Child class.'''
    
    def __init__(self, 
                 isbn, title, author, publisher, date_published,
                 price_usd, price_aud, price_eur, price_gbp) -> None:
        
        BookMetadata.__init__(isbn, title, author, publisher, date_published)
        
        self.price_usd: float = price_usd
        self.price_aud: float = price_aud
        self.price_eur: float = price_eur
        self.price_gbp: float = price_gbp


    def __post_init__(self):
        self.price_usd = str(self.price_usd)

and the values assigned as such:

book1 = RetailPrice(isbn='1234-5678-9000', 
                    title='My book', 
                    author='Name Surname', 
                    publisher='My publisher',
                    date_published=1670536799, 
                    price_usd=17.99, 
                    price_aud=23.99, 
                    price_eur=15.99, 
                    price_gbp=16.99)

I get a TypeError:: TypeError: __init__() missing 1 required positional argument: 'date_published', but this was provided in the assignment.

Is this due to the fact that the parent class has no __init__?

PS: this is my attempt at reproducing line 21 in the image below, having to work with data classes instead of regular classes: enter image description here

CodePudding user response:

Since you are using a subclass of BookMetadata you can just initialise the superclass from child class using super() -


@dataclass()
class RetailPrice(BookMetadata):
    '''Child class.'''
    
    def __init__(self, 
                 isbn, title, author, publisher, date_published,
                 price_usd, price_aud, price_eur, price_gbp) -> None:
        super().__init__(isbn, title, author, publisher, date_published)
        self.price_usd: float = price_usd
        self.price_aud: float = price_aud
        self.price_eur: float = price_eur
        self.price_gbp: float = price_gbp

The reason for the error in your original code is because of the missing self argument to BookMetadata.__init__(), since you don't have an instance of BookMetadata class, so one argument was missing(self 5 additional arguments, so it was giving error on the last missing argument date_published)

  • Related