FuzzyDateTime with `force_year` might yield ValueError
See original GitHub issueDescription
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:
- Created a year ago
- Comments:9 (5 by maintainers)
Top 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 >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
Good that you’ve found a replacement in Faker! 👍
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.