Search code examples
pythonfaker

How to prove that `fake.unique.ascii_free_email()` will never return a duplicate email address no matter how many times its run


  • I want to understand and prove via some results that in no case the email address returned by this faker provider will return a duplicate email for a run session
  • I tried running the below snippet and found that there was no duplicate in list of 20 result emails but i want to ask if there is a way by which I can prove for certain that lets say if I run this snippet for N number of times then there will be no duplicates in the list of N resultant emails
from faker import Faker

fake = Faker()

def test_faker():
    for _ in range(20):
        print(fake.unique.ascii_free_email())

Solution

  • That's what fake.unique does. The UniqueProxy wraps every fake method with a decorator that ensures that within this run that each value is only ever returned once. See https://github.com/joke2k/faker/blob/master/faker/proxy.py