Closed idc9 closed 6 months ago
Hi, appreciate the succinct example script!
To me this looks like it's working as designed: the default value of a_default
, A()
, is mutable. This is unsafe.
There's a reasonable question about whether safety here is any of tyro
's business, but either way I'd recommend either marking your dataclass as frozen or using default_factory
. Note that your example will throw an error starting in Python 3.11:
Traceback (most recent call last):
File "/Users/brentyi/Documents/code/idc9.py", line 8, in <module>
@dataclass
^^^^^^^^^
File "/Users/brentyi/miniconda3/envs/py311/lib/python3.11/dataclasses.py", line 1223, in dataclass
return wrap(cls)
^^^^^^^^^
File "/Users/brentyi/miniconda3/envs/py311/lib/python3.11/dataclasses.py", line 1213, in wrap
return _process_class(cls, init, repr, eq, order, unsafe_hash,
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/brentyi/miniconda3/envs/py311/lib/python3.11/dataclasses.py", line 958, in _process_class
cls_fields.append(_get_field(cls, name, type, kw_only))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/brentyi/miniconda3/envs/py311/lib/python3.11/dataclasses.py", line 815, in _get_field
raise ValueError(f'mutable default {type(f.default)} for field '
ValueError: mutable default <class '__main__.A'> for field a_default is not allowed: use default_factory
Ah ok I misunderstood how the default
argument works with field... I agree with how tyro works in this case. Tyro 1, Iain 0.
Tyro gives a warning for setting mutable dataclass defaults when you use
field
withdefault
(incorrect behavior). It correctly does not give a warning when you usefield
withdefault_factory
.Here is a simple script
And it outputs the following warning (notice the warning is for
a_default
and there is no warning fora_factory
.I'm using tyro version 0.7.2 and python version 3.8.18