Question: `make_class` equivalent of the "new API" - defining fields using type annotations
Nobody has claimed this yet.
Assessment
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Newbie friendliness
- 25/100
Research direction
Start by examining attr.make_class with the supplied inspect.get_annotations example, including the frozen=True, on_setattr=True, and auto_attribs=True options. Determine whether the requested programmatic equivalent is supported or needs a documented change, and verify the result against the behavior of attr.frozen(Test).
Written by the indexing model from the issue text.
Description
I have been trying to use attr.make_class to create a class based on type annotations. This is exemplified by the following code:
import attr
import inspect
class Test:
a: int
b: str
aTest = attr.frozen(Test)
sig = inspect.get_annotations(Test)
print(sig) # {"a": int, "b": str}
# fails with AttributeError: type object 'int' has no attribute 'type'
bTest = attr.make_class("Test", sig, frozen=True, on_setattr=True, auto_attribs=True)
The intent were for bTest to be equivalent to aTest, but generated programatically. However, that doesn't seem to work. What is the standard way to create an attr class programmatically from type annotations?
- Dominant language
- Python
- Stars
- 5.8k
- Forks
- 480
- Avg merge
- 2h 15m
- Merged PRs (30d)
- 2
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.
More from python-attrs/attrs
-
Difficulty 5/5 Over a week Newbie friendliness 20/100
python-attrs/attrs#1620 ·
-
Difficulty 3/5 1-2 days Newbie friendliness 55/100
python-attrs/attrs#1596 · 2 comments · 1 reaction ·
-
Difficulty 4/5 3-5 days Newbie friendliness 35/100
python-attrs/attrs#1549 · 3 comments ·
-
Difficulty 4/5 3-5 days Newbie friendliness 38/100
python-attrs/attrs#1543 · 2 comments ·
-
Difficulty 4/5 3-5 days Newbie friendliness 35/100
python-attrs/attrs#1532 ·
All issues in python-attrs/attrs
Similar issues
-
documentation help wanted
Difficulty 2/5 1-3 hours Newbie friendliness 90/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 90/100
simonw/sqlite-utils#872 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 88/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100