Now that I have finally dropped support for Python 2, I am migrating from attrs
to Python 3 dataclasses
and there is one issue I am particularly struggling with.
Let's say I have a frozen and hashable class MyClass
with one field my_field
of type tuple
.
Thanks to attrs
converters, I was able to provide a flexible API, with clients able to instantiate my_field
with a variety of types like list
, set
or dict_keys
. They would all be automatically converted to a tuple
before the class creation.
Can I preserve this API with dataclasses
?
As requested, a small code sample:
@attr.s(frozen=True, hash=True)
class MyClass:
my_field = attr.ib(default=tuple(), converter=tuple)
print(MyClass([1, 2, 3]))