construct-typing icon indicating copy to clipboard operation
construct-typing copied to clipboard

Unable to set Default field

Open brherger opened this issue 1 year ago • 1 comments

Based on the unit test test_dataclass_const_default, it does not appear we can set the default fields.

I would expect that is possible per construct.Default documentation.

>>> ConstDefaultTest(default_int=5)
Traceback (most recent call last):
  File "C:\Program Files\JetBrains\PyCharm 2022.3\plugins\python\helpers\pydev\_pydevd_bundle\pydevd_exec2.py", line 3, in Exec
    exec(exp, global_vars, local_vars)
  File "<input>", line 1, in <module>
TypeError: ConstDefaultTest.__init__() got an unexpected keyword argument 'default_int'

Python 3.11.4 construct==2.10.68 construct-typing==0.5.5

brherger avatar Jun 29 '23 12:06 brherger

That's correct, currently construct.Default fields are initialized to there default value and have to be change after calling __init__. That's because dataclasses.dataclass is used in the background to create the __init__ method. I did not found an solution to implement your expected behavior. If you have an idea how, feel free to create an PR :)

timrid avatar Jun 29 '23 19:06 timrid