-
Notifications
You must be signed in to change notification settings - Fork 410
feat: support customizing attr getter in SubFactory #1135
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
feat: support customizing attr getter in SubFactory #1135
Conversation
francoisfreitag
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the issue and pull request!
I’m not sure about the behavior of silently switching to __getitem__ in case __getattr__ failed. Users may have subclassed __getitem__ on a class and not expect that code to run from SelfAttribute().
The documented behavior is:
Some fields should reference another field of the object being constructed, or an attribute thereof.
field is more general and would apply to a dict entry.
The docstring were clearly written with attribute fetching in mind.
Because DictFactory is not documented, I am inclined to stick with the current behavior to avoid surprises, especially since the suggested patch has no opt-out. Maybe an option forward would be to make the resolver function configurable (defaulting to the current deepgetattr), so that interested users can implement their own resolution?
What would this look like? Some parameter passed to |
Yes, that’s what I had in mind. |
|
@francoisfreitag so what I am hearing based on this:
Is that there should be a new parameter added to SubFactory to define a custom callable to retrieve attributes and nested attributes. This callable should default to |
|
I meant to add a kwarg to The main goal is to keep the current implementation, because the proposed call to |
|
Got it. I will make the changes. I will also add a |
This change should allow for the case where a `SelfAttribute` attempts to access the field of a `SubFactory` that is a `DictFactory`. The user can override the `deepgetattr_func` of the `SubFactory` to use `deepdictgetattr`, or one of their own choosing, which will be used to retrieve the attribute. closes FactoryBoy#1134
ac8c501 to
bb6d861
Compare
|
@francoisfreitag I've made the suggested changes. Let me know if this is acceptable. |
|
Hello, I'm truly sorry, but I'm not going to merge this, since As the Zen of Python says: There should be one-- and preferably only one --obvious way to do it. By the way: it's easier for us if discussion about possible implementations/solutions are made in an issue, before jumping to "let's add some code" — the less code, the easier it is for us to maintain. |
@rbarrois thanks for the consideration. I didn't have time to wait to discuss so I made a fix that worked for me in the spirit of what I thought FactoryBoy was trying to achieve by offering a DictFactory. I tried to be a good citizen and contribute it back rather than leave it hidden in some fork. FWIW if there is not going to be a behavior change to |
This change should allow for the case where a
SelfAttributeattempts to access the field of aSubFactorythat is aDictFactory. The user can override thedeepgetattr_funcof theSubFactoryto usedeepdictgetattr, or one of their own choosing, which will be used to retrieve the attribute.closes #1134