question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Allow declarations in Faker kwargs

See original GitHub issue

The 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:closed
  • Created 3 years ago
  • Reactions:1
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
rbarroiscommented, Aug 21, 2020

Hi!

Thanks for the report!

This has been covered by #772, and should be available in release 3.1 😉

0reactions
maxrothmancommented, May 3, 2020

@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?

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found