Possible lazy=True Feature?
Nobody has claimed this yet.
Assessment
- Difficulty
- 5/5
- Estimated time
- Over a week
- Newbie friendliness
- 25/100
Research direction
No files, tests, or entry points are named. Start by reviewing the requested lazy=True and Builder behavior alongside attrs attribute, default, validator, converter, and factory semantics. Done means an agreed design and implementation for lazy, cached evaluation of the demonstrated attributes, with its interactions defined.
Written by the indexing model from the issue text.
Description
Hi Everyone,
First, thanks for the great tool -- I really love writing Python code with it!
I find myself doing things like this a lot when I want b and c to only run when called (and then only once with the result cached).
import attr
from cached_property import cached_property
@attr.s
class MyClass:
a = attr.ib(
validator=attr.validators.instance_of(int),
converter=int,
)
@cached_property
def b(self):
return str(self.a) * 3
@cached_property
def c(self):
return int(self.b) * 3
foo = MyClass(a='1')
print(f'foo.a: {foo.a}')
print(f'foo.b: {foo.b}')
print(f'foo.c: {foo.c}')
But then I have to "break out of attrs" and loose the other goodness it provides. I know I can do something like this:
import attr
@attr.s
class MyClass:
a = attr.ib(
validator=attr.validators.instance_of(int),
converter=int,
)
b = attr.ib(
validator=attr.validators.instance_of(str),
default=attr.Factory(
takes_self=True,
factory=lambda self: str(self.a) * 3,
)
)
c = attr.ib(
validator=attr.validators.instance_of(int),
default=attr.Factory(
takes_self=True,
factory=lambda self: int(self.b) * 3,
)
)
foo = MyClass(a='1')
print(f'foo.a: {foo.a}')
print(f'foo.b: {foo.b}')
print(f'foo.c: {foo.c}')
But then b and c get "run" or "built" every time, even if they aren't needed (and I'm obviously assuming in reality they do more work they my toy example here shows).
What I really want to do is something like:
import attr
@attr.s
class MyClass:
a = attr.ib(
validator=attr.validators.instance_of(int),
converter=int,
)
b = attr.ib(
validator=attr.validators.instance_of(str),
lazy=True,
default=attr.Factory(
takes_self=True,
factory=lambda self: str(self.a) * 3,
)
)
c = attr.ib(
validator=attr.validators.instance_of(int),
lazy=True,
default=attr.Factory(
takes_self=True,
factory=lambda self: int(self.b) * 3,
)
)
foo = MyClass(a='1')
print(f'foo.a: {foo.a}')
print(f'foo.b: {foo.b}')
print(f'foo.c: {foo.c}')
Or even better, something that doesn't require me to specify takes_self=True every time... maybe something like:
import attr
@attr.s
class MyClass:
a = attr.ib(
validator=attr.validators.instance_of(int),
converter=int,
)
b = attr.ib(
validator=attr.validators.instance_of(str),
lazy=True,
builder=attr.Builder(
factory=lambda self: str(self.a) * 3,
)
)
c = attr.ib(
validator=attr.validators.instance_of(int),
lazy=True,
builder=attr.Builder(
factory=lambda self: int(self.b) * 3,
)
)
foo = MyClass(a='1')
print(f'foo.a: {foo.a}')
print(f'foo.b: {foo.b}')
print(f'foo.c: {foo.c}')
Thoughts on if something like this would be possible? I haven't dug into guts of attrs yet enough to know if it's doable, but it seems like it would be a great enhancement if something like that is possible. I came not too long ago from a non-Python environment where we built classes almost exactly like I show in my last example above, and it's a super efficient and expressive way to do things.
Thanks!
- 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
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 86/100
zostera/django-bootstrap4#894 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
use-agent-os/agent-os#3276 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 88/100
zephyrproject-rtos/zephyr#119726 ·
-
area/auth bug comp/agent P3 platform/discord type/security
Difficulty 2/5 1-3 hours Newbie friendliness 88/100
NousResearch/hermes-agent#117848 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
zilliztech/memsearch#759 ·