faker icon indicating copy to clipboard operation
faker copied to clipboard

Question: uniqueness except bools

Open WilliamDEdwards opened this issue 9 months ago • 5 comments

Sometimes, Faker generates duplicate values.

Faker supports a unique mode, which guarantees unique values: https://faker.readthedocs.io/en/master/#unique-values

I could implement this by changing get_fakers return value to fake.unique, but that would also apply to bools, which causes unique values (true/false) to be exhausted real quick. And I don't care about duplicate booleans.

Does Faker support excluding specific types from uniqueness (bools in this case)?

To decrease the chance of uniques, I now add unique to all Faker invocations (on instances returned by get_faker). However, this is not foolproof (one could forget to add unique), and inevitably still causes duplicates incidentally, albeit less often.

WilliamDEdwards avatar Apr 15 '25 13:04 WilliamDEdwards

Hi @WilliamDEdwards Uniqueness in general is a very debated topic in the world of random data generation.

I agree with you that for some providers, it does not make any sense, like the boolean.

I'm exploring two approaches, the first is marking methods with annotations to skip the Uniqueness proxy, the second is to pass some configurable "exceptions" to the unique proxy.

joke2k avatar Apr 15 '25 18:04 joke2k

Hi @joke2k,

I think passing exceptions to the unique proxy makes most sense.

Marking providers as being exempt from uniqueness seems like doing things in the wrong direction. In my view, providers are solely meant to provide data, not have opinions on the context they are used in.

A counter-argument to that: I think it's a safe assumption that for 99% of users, bools should not be unique, so defaulting them to be non-unique is good for DX. However:

  • Making bools non-unique by default would be a breaking change
  • Adding an exception to the uniqueness proxy is 2 seconds of work

Thanks for your reply!

WilliamDEdwards avatar Jul 07 '25 14:07 WilliamDEdwards

This issue is stale because it has been open for 30 days with no activity.

github-actions[bot] avatar Oct 06 '25 02:10 github-actions[bot]

Still relevant.

WilliamDEdwards avatar Oct 06 '25 06:10 WilliamDEdwards

Hi @joke2k,

Have you made any (thinking) progress on this by any chance?

WilliamDEdwards avatar Oct 10 '25 14:10 WilliamDEdwards