2
@dataclass
class base:
    data_dir: Path = Path(__file__).parents[1] / 'data'
    models_dir: Path = data_dir / 'models'

@dataclass
class field(base):
    id: str
    data_source: str  # can be a csv file or a database
    df: pd.DataFrame  # dataframe containing field information

    def get_field(self):
        pass

In the code above, I get this error: TypeError: non-default argument 'id' follows default argument

How can I fix this?

martineau
  • 119,623
  • 25
  • 170
  • 301
user308827
  • 21,227
  • 87
  • 254
  • 417
  • 1
    By moving the definition of the argument with a default value to before all those that do not. – martineau Oct 09 '20 at 18:33
  • 3
    The documentation ("TypeError will be raised if a field without a default value follows a field with a default value. **This is true either when this occurs in a single class, or as a result of class inheritance.**") implies you simply cannot define non-default fields in a subclass of a field with default-valued fields. – chepner Oct 09 '20 at 18:38
  • 1
    @martineau, but how is that done here? - ah, \@chepner said it. That seems kinda bizarre to me. So he has to give all of his fields default values then, eh? – CryptoFool Oct 09 '20 at 18:39
  • Sorry, I was mistaken and based my comment strictly on the error message. – martineau Oct 09 '20 at 18:42

0 Answers0