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.

FuzzyDateTime with `force_year` might yield ValueError

See original GitHub issue

Description

Using FuzzyDateTime with the force_year option might yield to Python raising a ValueError, as leap days are not handled.

To Reproduce

Generate lots of random dates with force_year being set to the current year. Sporadically, this will fail with a ValueError.

Model / Factory code
import datetime
import pytz
from factory import DjangoModelFactory, fuzzy

class Factory(DjangoModelFactory):
    class Meta:
        model = MyModel

    created = fuzzy.FuzzyDateTime(start_dt=datetime.datetime(2020, 1, 1, tzinfo=pytz.utc), force_year=datetime.date.today().year)
The issue
instance = Factory()
  File "/home/user/lib/python3.6/site-packages/factory/base.py", line 46, in __call__
    return cls.create(**kwargs)
  File "/home/user/lib/python3.6/site-packages/factory/base.py", line 564, in create
    return cls._generate(enums.CREATE_STRATEGY, kwargs)
  File "/home/user/lib/python3.6/site-packages/factory/django.py", line 141, in _generate
    return super(DjangoModelFactory, cls)._generate(strategy, params)
  File "/home/user/lib/python3.6/site-packages/factory/base.py", line 501, in _generate
    return step.build()
  File "/home/user/lib/python3.6/site-packages/factory/builder.py", line 272, in build
    step.resolve(pre)
  File "/home/user/lib/python3.6/site-packages/factory/builder.py", line 221, in resolve
    self.attributes[field_name] = getattr(self.stub, field_name)
  File "/home/user/lib/python3.6/site-packages/factory/builder.py", line 375, in __getattr__
    extra=context,
  File "/home/user/lib/python3.6/site-packages/factory/fuzzy.py", line 65, in evaluate
    return self.fuzz()
  File "/home/user/lib/python3.6/site-packages/factory/fuzzy.py", line 263, in fuzz
    result = result.replace(year=self.force_year)
ValueError: day is out of range for month

Notes

Add any notes you feel relevant here 😃

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:9 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
francoisfreitagcommented, Aug 29, 2022

Good that you’ve found a replacement in Faker! 👍

1reaction
stefan6419846commented, Aug 29, 2022

In the meantime, I saw from the docs that one might want to replace these fuzzy generators with the faker ones (although they might lack some features, while they provide more flexibility in other cases).

Nevertheless, I have already replaced the offending line with created = factory.Faker('date_time_this_year') now, so for me this issue should not pop up again.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Fuzzy attributes — Factory Boy stable documentation
The FuzzyChoice fuzzer yields random choices from the given iterable. Note. The passed in choices will be converted into a list upon first...
Read more >
Deprecate Fuzzy Attributes that are already supported in Faker
Since fuzzy attributes (will) rely on an external dependency, what do you think about making ... FuzzyDateTime with force_year might yield ValueError #969....
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