instance_of validator cannot refer to the current class
Nobody has claimed this yet.
Assessment
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Newbie friendliness
- 35/100
Research direction
Start by reading the validator implementation and its existing tests, then trace how instance_of resolves the expected type. Compare the proposed type_getter approach with the current lambda workaround; done means current-class references can be validated without the workaround and existing behavior remains covered.
Written by the indexing model from the issue text.
Description
Hi,
I'm working on a project where it's useful for an attribute to refer to the current class. Currently, I'm using something like:
from __future__ import annotations # Needed for recursive type annotations.
@attr.s(slots=True)
class Namespace(object):
parent: Namespace = attr.ib(
validator=lambda inst, attr_, val: attr.validators.instance_of(Namespace)(inst, attr_, val)
)
Is there currently a better way to do this?
If not, would you be open to a pull request that might allow something like this?
validator = attr.validators.instance_of(type_getter=lambda: Namespace)
(Also, if you have other suggestions for an approach, I'd be happy to implement those in a PR instead.)
- 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
-
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
-
enhancement
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 74/100