python-attrs / python-attrs/cattrs

Unstructuring subclassed built-ins

Open
#287 0 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: 22.1.0
  • Python version: 3.10.5
  • Operating System: Linux
Description

Hi, I wanted to have fancy code and decided to subclass built-in list. I run into problems when I tried to unstructure it using cattrs. It behaves differently. I am not sure if this is on purpose or a bug. Can you please help me, should I register some hooks for all such classes? Thank you.

What I Did
from attr import define
from cattrs import GenConverter
from typing import TypeVar

T = TypeVar("T")


@define
class Foo:
    val: int


class mylist(list[T]):
    pass


def main() -> None:
    a = list[Foo]([Foo(val=1)])
    b = mylist[Foo]([Foo(val=1)])

    converter = GenConverter()

    print(converter.unstructure(a))  # [{'val': 1}]
    print(converter.unstructure(b))  # [Foo(val=1)]

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 with the provided Python reproducer and compare GenConverter.unstructure on list[Foo] and the subclassed mylist[Foo]. Trace how the converter dispatches these entry points and determine whether the differing output is intended. Done means the behavior is clarified and, if confirmed as a bug, covered by a regression test.

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.