attrs icon indicating copy to clipboard operation
attrs copied to clipboard

Class helper functions - Support for generic classes

Open LukasKrocek opened this issue 3 years ago • 3 comments

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])```

LukasKrocek avatar Nov 03 '22 18:11 LukasKrocek

Will be taking a look at this soon.

Tinche avatar Dec 31 '22 00:12 Tinche

+1

This is quite annoying when I'm using cattrs' hook factories... e.g. the snake_case to camelCase cattrs example doesn't work with generic classes

fredsensibill avatar Jan 13 '23 00:01 fredsensibill

first support has been merged as part of #1079

hynek avatar Feb 06 '23 08:02 hynek