Class helper functions - Support for generic classes

Open
#1,049 3 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
45/100
Issue type
Feature
Clarity
Mostly clear
Activity status
Stale
Tech stack
python
Domain
devtools

Research direction

Start by reproducing the examples for attr.has, attr.fields, and attr.fields_dict with A and A[str], then inspect the implementations of those helpers. Done means generic class aliases are recognized and the helpers return the same expected results as for the unsubscripted class.

Written by the indexing model from the issue text.

Description

Typing

I noticed that functions like attr.has, attr.fields, attr.fields_dict doesn't work as i expected when i pass in class with generic type defined:


from attr import frozen, evolve, mutable, has, fields, fields_dict, Attribute

T = TypeVar("T")

@frozen
class A(Generic[T]):
    a: int
    b: T

has(A) => True
has(A[str]) => False (should be True)

fields(A) => Sequence[Attribute]
fields(A[str]) => TypeError: Passed object must be a class (should be Sequence[Attribute])

fields_dict(A) => Mapping[str, Attribute]
fields(A[str]) => TypeError: Passed object must be a class (should be Mapping[str, Attribute])```
Dominant language
Python
Stars
5.8k
Forks
480
Avg merge
2h 15m
Merged PRs (30d)
2

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.

More from python-attrs/attrs

All issues in python-attrs/attrs

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.