From 14a10ebfc500a305bfe1585d51900bb4681263e8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=AEnerB=E2=80=AD=E2=80=AE=20taraM=E2=80=AD?= Date: Sat, 30 Apr 2022 22:04:54 +0100 Subject: [PATCH] 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__ .__create_fn__..__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__ .__create_fn__..__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>``` --- construct_typed/dataclass_struct.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/construct_typed/dataclass_struct.py b/construct_typed/dataclass_struct.py index 78d2e59..d13f017 100644 --- a/construct_typed/dataclass_struct.py +++ b/construct_typed/dataclass_struct.py @@ -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 \ No newline at end of file +sfield = csfield