Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow arguments in Factory #22

Closed
hynek opened this issue Aug 3, 2015 · 2 comments
Closed

Allow arguments in Factory #22

hynek opened this issue Aug 3, 2015 · 2 comments

Comments

@hynek
Copy link
Member

hynek commented Aug 3, 2015

useful e.g. with structlog

x = attr.ib(default=Factory(logger.new, foo="bar"))
@Zac-HD
Copy link
Contributor

Zac-HD commented May 25, 2018

Based on my experience with Hypothesis (especially the builds() strategy), I'd suggest that attrs should instead leave users with

x = attr.ib(factory=lambda: logger.new(foo="bar"))

(though you might want to add some documentation suggesting this, or functools.partial)

Passing through arbitrary args and kwargs can cause a variety of problems. The main two for this case are that attr.Factory already has named arguments - so users cannot provide arguments with those names to a callable factory - and takes_self is in some ways worse (impossible to provide star-args without explicitly passing a value for takes_self positionally). Using named args=(), kw_args=None arguments and dealing with the None-to-dict() issue would work, but IMO it's screamingly ugly and the lambda-or-partial solution works just fine.

TLDR please don't add this feature.

@hynek
Copy link
Member Author

hynek commented May 25, 2018

Funny you brought it up because I was tending to the same conclusion but I wasn’t quite over the jump to close the issue. :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants