python-attrs / python-attrs/attrs
Expand allowed types for converters
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 5.8k
- Forks
- 480
- Avg merge
- 2h 15m
- Merged PRs (30d)
- 2
Description
Converters are one-arg functions, some one-arg functions yield type error
Unsupported converter, only named functions and types are currently supported
One of the things I love about attrs is "good engineering practices" baked in - especially typing support for our linters. The current typing omits a couple of use cases:
- class static methods
- class constructor
Global converters are great for global concerns, but more specific concerns should be grouped in the smallest enclosing scope. Given an attrs class with a field that is a nested attrs class, I would love to use a converter on the nested class field that is a static method on that class - only that class knows how to map inbound data to class fields.
Use case: we frequently have a class static method to create an instance from serialized data, e.g. MyClass.from_dict(). I would like to use my_field: MyClass = attrib(converter=MyClass.from_dict).
The second case, class constructor, is useful for single arg constructor classes.
Use case: we use a lot of class StringEnum(str, Enum) derived classes. These are kinda cool because they will yield an enum when passed a str or an Enum. Many simple classes that exist to eliminate string literals, or consolidate global concepts also fit in this category.
What one-arg converters should be prohibited? Not coming up with any except lambdas for the obvious inconsistency and maintenance problems they cause as the proliferate a large code bases.
For clarity: these idioms already work, I just have to # type: ignore each line.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by tracing the converter typing support described in the issue and confirm how named functions and types are currently recognized. Verify the intended behavior for static methods and one-argument constructors, while keeping lambdas excluded. Done means these supported converter forms no longer require type: ignore annotations and their behavior is covered by focused tests.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- developer-experience
- Issue type
- Feature
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100