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:
‮nerB‭‮ taraM‭ 2022-04-30 22:04:54 +01:00 committed by GitHub
parent 834d244eda
commit 14a10ebfc5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -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