python-attrs / python-attrs/cattrs

Error with adapted_fields when subclassing attr class

Open
#257 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
1.1k
Forks
159
Avg merge
12h 21m
Merged PRs (30d)
6

Description

  • cattrs version: 1.8.0
  • Python version: 3.8
  • Operating System: PopOS
Description

The current implementation of adapted_fields check first if the type if a dataclass when you should instead test if it's a attr class first. This leads to miscategorizing type as non attr class and not use the attrs_fields

What I Did
    @strawberry.type  # definie method typical of python dataclass that gets picked up by cattr.is_dataclass
    @attr.define
    class UserAttributes:
        birthday: Optional[date] = attr.field(validator=attr.validators.optional(attr.validators.instance_of(date)),
                                              default=None)
    structure({}, UserAttributes)  # fails with KeyError 'birthday' because the default of attr is not handled

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at the adapted_fields implementation and reproduce the issue with the UserAttributes example from the report. Check how the type is classified before attrs fields are read, then verify that structure({}, UserAttributes) handles the birthday default without a KeyError. Add or update a regression test for the subclassing and decorator combination described.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
backend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.