-
Notifications
You must be signed in to change notification settings - Fork 107
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Dacite doesn't play well with freezegun #185
Comments
@ehiggs thanks for reporting this issue. As you probably know This will work (both with freeze_time("2020-06-23T12:15:00.256Z"):
@dataclass
class Foo:
timestamp: datetime
from_dict(Foo, {"timestamp": "2022-04-04T15:37:51.258Z"}, config=Config({datetime: isoparse})) TBH I don't see any clean solution - I don't want to add Freezgun is really awesome library (I use it everyday) but because of this hacky monky patching it sometimes doesn't cooperate seamlessly with other tools. I'm closing this issue for now, but feel free to comment/reopen/provide PR ;) |
Thanks for the reply. The proposed solution is less than ideal because it means all imports for tests need to take place in a freeze_time context. Digging into the code a bit more, the issue seems to be when we check if the type parsed correctly: if config.check_types and not is_instance(value, field.type):
raise WrongTypeError(field_path=field.name, field_type=field.type, value=value) So this should fire if the parsed type is not Let's make a dt of type
Great. Note, this is also
But dacite uses a special
So a FakeDatetime IS a datetime. So why would this exception be raisd? 🤔 |
Problem is not that the type doesn't match, problem is that before that, the type hook was not triggered, leading the expected
That is caused by the fact that the list of So this:
is because the type here is in fact not Doesn't solve them problem, but knowing this we can discuss whether the |
A possible workaround to have freezegun and dacity both running: # define a helper dict wrapper
class FreezegunCompatibleTypeHookWrapper(dict):
def __contains__(self, item):
return super().__contains__(item) or str(item) == "<class 'datetime.datetime'>"
def __getitem__(self, item):
if str(item) == "<class 'datetime.datetime'>":
# replace the datetime class with the (maybe monkeypatched) directly imported datetime module
# reason: https://github.com/konradhalas/dacite/issues/185#issuecomment-1433506657
item = datetime
return super().__getitem__(item)
# and then use:
Config(type_hooks=FreezegunCompatibleTypeHookWrapper({datetime: isoparse})) |
freezegun is a super common library used for testing and basically indispensable. But it seems when working with dacite, it can run into issues. As follows:
dacite-1.6.0-py3-none-any.whl
The text was updated successfully, but these errors were encountered: