ericvsmith / dataclasses

Apache License 2.0
587 stars 53 forks source link

Ordering and lack of defaults? #96

Closed JimJJewett closed 6 years ago

JimJJewett commented 6 years ago

What happens if a field without a default value follows one with a default value? (This may happen directly, or as the result of inheritance). e.g. from the PEP example, if quantity had been defined before unit_price?

@dataclass class InventoryItem: '''Class for keeping track of an item in inventory.''' name: str quantity_on_hand: int = 0 unit_price: float

If the answer is raising an Exception, that is fine ... but this should be explicit, and should also warn about the way it can arise from inheritance. e.g.,

@dataclass class A: a: int

@dataclass class B(A): b: int=4

ericvsmith commented 6 years ago

NB: It's @dataclass, not @DataClass.

I've added a note to the PEP. I'll also add some tests about an inheritance case.

I think your example is backward, with respect to which field has a default and which doesn't.