Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

include_subclasses does not support dataclasses #426

Closed
eltoder opened this issue Sep 17, 2023 · 2 comments · Fixed by #477
Closed

include_subclasses does not support dataclasses #426

eltoder opened this issue Sep 17, 2023 · 2 comments · Fixed by #477
Milestone

Comments

@eltoder
Copy link

eltoder commented Sep 17, 2023

  • cattrs version: 23.1.2
  • Python version: 3.9
  • Operating System: Linux

Description

According to the documentation, include_subclasses() supports both attrs and dataclasses. However, when I change the example to use dataclasses, I get an error:

from dataclasses import dataclass
from cattrs.strategies import include_subclasses
from cattrs import Converter

@dataclass
class Parent:
    a: int

@dataclass
class Child(Parent):
    b: str

converter = Converter()
include_subclasses(Parent, converter)

produces

Traceback (most recent call last):
  File "/home/eltoder/dev/scratch/cattrs_dc.py", line 14, in <module>
    include_subclasses(Parent, converter)
  File "/home/eltoder/.local/share/ext-python/python-3.9.16.146/lib/python3.9/site-packages/cattrs/strategies/_subclasses.py", line 73, in include_subclasses
    _include_subclasses_without_union_strategy(
  File "/home/eltoder/.local/share/ext-python/python-3.9.16.146/lib/python3.9/site-packages/cattrs/strategies/_subclasses.py", line 111, in _include_subclasses_without_union_strategy
    dis_fn = converter._get_dis_func(subclass_union)
  File "/home/eltoder/.local/share/ext-python/python-3.9.16.146/lib/python3.9/site-packages/cattrs/converters.py", line 756, in _get_dis_func
    return create_uniq_field_dis_func(*union_types)
  File "/home/eltoder/.local/share/ext-python/python-3.9.16.146/lib/python3.9/site-packages/cattrs/disambiguators.py", line 20, in create_uniq_field_dis_func
    cls_and_attrs = [
  File "/home/eltoder/.local/share/ext-python/python-3.9.16.146/lib/python3.9/site-packages/cattrs/disambiguators.py", line 21, in <listcomp>
    (cl, set(at.name for at in fields(get_origin(cl) or cl))) for cl in classes
  File "/home/eltoder/.local/share/ext-python/python-3.9.16.146/lib/python3.9/site-packages/attr/_make.py", line 1944, in fields
    raise NotAnAttrsClassError(f"{cls!r} is not an attrs-decorated class.")
attr.exceptions.NotAnAttrsClassError: <class '__main__.Child'> is not an attrs-decorated class.

It does work if I use union_strategy:

from cattrs.strategies import configure_tagged_union
include_subclasses(Parent, converter, union_strategy=configure_tagged_union)

Additionally, when union_strategy=configure_tagged_union is used, it would be nice to avoid using __subclasses__ so that new classes can be added dynamically. Let me know if you want me to open a separate issue for this.

@aha79
Copy link

aha79 commented Sep 22, 2023

I guess this is because the automatic union disambiguation (based on unique fields) is exclusively for attrs classes (see https://catt.rs/en/stable/structuring.html#automatic-disambiguation).

@Tinche
Copy link
Member

Tinche commented Sep 22, 2023

Ah, yeah. The easiest fix here would be to just support dataclasses for the default disambiguator. I think it's an easy and useful change. But not in the next release, since it's getting bloated already.

As for the other issue, feel free to open a new ticket and we can triage it.

@Tinche Tinche added this to the 24.1 milestone Nov 19, 2023
@Tinche Tinche linked a pull request Dec 29, 2023 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants