Allow declarations in Faker kwargs
See original GitHub issueThe problem
Take this model for example:
class User:
address
city
state
zip
A first approach to building a factory would look like this:
class User(Factory):
address = Faker('street_address')
city = Faker('city')
state = Faker('state')
zip = Faker('zipcode')
But this could cause spurious bugs since the zip and the state might not match. Instead, one might want to take this approach:
class User(Factory):
address1 = Faker('street_address')
city = Faker('city')
state = Faker('state')
zip = Faker('zipcode_in_state', state_abbr=SelfAttribute('state'))
However this fails with the following error:
/usr/local/lib/python3.6/dist-packages/faker/providers/address/en_US/__init__.py:427: in zipcode_in_state
return self.postcode_in_state(state_abbr)
Exception: State Abbreviation not found in list
After doing some digging, it appears that the SelfAttribute
is not being evaluated before being passed to Faker.
Proposed solution
Declarations should be evaluated before being passed to faker, the same way they are before being passed to subfactories.
Issue Analytics
- State:
- Created 3 years ago
- Reactions:1
- Comments:5 (1 by maintainers)
Top Results From Across the Web
Allow declarations in Faker kwargs - - Bountysource
Declarations should be evaluated before being passed to faker, the same way they are before being passed to subfactories. See More.
Read more >Reference — Factory Boy stable documentation
In order to easily define realistic-looking factories, use the Faker attribute declaration. This is a wrapper around faker; its argument is the name...
Read more >How to use lazy_attribute with Faker in Factory Boy
When lazy_attribute come into play you already have generated object on your hand. So you can work with, for example, random and timedelta, ......
Read more >args, **kwargs, and custom class wrappers in Python
Using a simple inheritance pattern along with Python's *args and **kwargs symbols, we can insert our own metadata into a wrapper class ...
Read more >Mock Django models using Faker and Factory Boy - DEV ...
Cover image for Mock Django models using Faker and Factory Boy ... Note that, create_batch also receives kwargs where it allows overriding attributes:....
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Hi!
Thanks for the report!
This has been covered by #772, and should be available in release 3.1 😉
@interDist ah, I see what you mean. Looking at #732, it appears to describe a similar issue to this one. I appreciate the workaround, but would y’all be interested in the proposed fix?