chalk-diagrams / chalk

A declarative drawing API in Python
MIT License
282 stars 13 forks source link

ValueError: mutable default <class 'chalk.style.Style'> for field head_style is not allowed: use default_factory #121

Closed cblp closed 1 year ago

cblp commented 1 year ago
pip3 install git+https://github.com/chalk-diagrams/chalk/
ipython3
In [2]: from chalk import *
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Cell In[2], line 1
----> 1 from chalk import *

File /Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/chalk/__init__.py:12
     10 import chalk.align as align
     11 from chalk.align import *  # noqa: F403
---> 12 from chalk.arrow import ArrowOpts, arrow_at, arrow_between, arrow_v
     13 from chalk.combinators import *  # noqa: F403
     14 from chalk.core import empty, set_svg_draw_height, set_svg_height

File /Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/chalk/arrow.py:17
     13 black = Color("black")
     14 # arrow heads
---> 17 @dataclass
     18 class ArrowOpts:
     19     head_style: Style = Style()
     20     head_pad: float = 0.0

File /Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/dataclasses.py:1220, in dataclass(cls, init, repr, eq, order, unsafe_hash, frozen, match_args, kw_only, slots, weakref_slot)
   1217     return wrap
   1219 # We're called as @dataclass without parens.
-> 1220 return wrap(cls)

File /Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/dataclasses.py:1210, in dataclass.<locals>.wrap(cls)
   1209 def wrap(cls):
-> 1210     return _process_class(cls, init, repr, eq, order, unsafe_hash,
   1211                           frozen, match_args, kw_only, slots,
   1212                           weakref_slot)

File /Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/dataclasses.py:958, in _process_class(cls, init, repr, eq, order, unsafe_hash, frozen, match_args, kw_only, slots, weakref_slot)
    955         kw_only = True
    956     else:
    957         # Otherwise it's a field of some type.
--> 958         cls_fields.append(_get_field(cls, name, type, kw_only))
    960 for f in cls_fields:
    961     fields[f.name] = f

File /Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/dataclasses.py:815, in _get_field(cls, a_name, a_type, default_kw_only)
    811 # For real fields, disallow mutable defaults.  Use unhashable as a proxy
    812 # indicator for mutability.  Read the __hash__ attribute from the class,
    813 # not the instance.
    814 if f._field_type is _FIELD and f.default.__class__.__hash__ is None:
--> 815     raise ValueError(f'mutable default {type(f.default)} for field '
    816                      f'{f.name} is not allowed: use default_factory')
    818 return f

ValueError: mutable default <class 'chalk.style.Style'> for field head_style is not allowed: use default_factory
danoneata commented 1 year ago

Thanks for the report, Yuriy! I've made a change that hopefully fixes this error.

cblp commented 1 year ago

Yes, it works now, thanks!