In csfield, force cs.Default to appear as init
Default[Construct] always has flagbuildnone as True, and therefore Default won't be available in the dataclass init.
This behaviour makes sense for Const and many other flagbuildnone, as then there is no need to provide a value, but with Default the current implementation removes the possibility of providing a overriding value.
Short example:
```py
import dataclasses
from construct_typed import DataclassMixin, csfield
@dataclasses.dataclass
class BandSystemTime(DataclassMixin):
Year: int = csfield(Default(Int16ul, 0))
Month: int = csfield(Default(Int16ul, 0))
DayOfWeek: int = csfield(Default(Int16ul, 0))
Day: int = csfield(Default(Int16ul, 0))
Hour: int = csfield(Default(Int16ul, 0))
Minute: int = csfield(Default(Int16ul, 0))
Second: int = csfield(Default(Int16ul, 0))
Milliseconds: int = csfield(Default(Int16ul, 0))```
```py
# Current behaviour
>>> BandSystemTime(Hour=12)
TypeError: __init__() got an unexpected keyword argument 'Hour'
>>> BandSystemTime.__init__
<function <...>.__create_fn__.<locals>.__init__(self) -> None>```
```py
# Desired behaviour
>>> BandSystemTime(Hour=12)
BandSystemTime(Year=0, Month=10, DayOfWeek=1, Day=0, Hour=2, Minute=0, Second=0, Milliseconds=0)
>>> BandSystemTime.__init__
<function <...>.__create_fn__.<locals>.__init__(self, Year: int = 0, Month: int = 0, DayOfWeek: int = 0, Day: int = 0, Hour: int = 0, Minute: int = 0, Second: int = 0, Milliseconds: int = 0) -> None>```
This commit is contained in:
parent
834d244eda
commit
14a10ebfc5
1 changed files with 2 additions and 1 deletions
|
|
@ -107,6 +107,7 @@ def csfield(
|
|||
if callable(default_subcon.value):
|
||||
default = None # context lambda is only defined at parsing/building
|
||||
else:
|
||||
init = True
|
||||
default = default_subcon.value
|
||||
|
||||
return t.cast(
|
||||
|
|
@ -269,4 +270,4 @@ TBitStruct = DataclassBitStruct
|
|||
TContainerMixin = DataclassMixin
|
||||
TContainerBase = DataclassMixin
|
||||
TStructField = csfield
|
||||
sfield = csfield
|
||||
sfield = csfield
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue