-
Notifications
You must be signed in to change notification settings - Fork 229
Fix from_dict() in the presence of optional datetime fields. #329
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
Conversation
The added test fails without the fix to `__init__.py`: ``` > setattr(self, field_name, cls().from_dict(value[key])) E TypeError: function missing required argument 'year' (pos 1) src/betterproto/__init__.py:1204: TypeError ```
@@ -1191,16 +1191,15 @@ def from_dict(self: T, value: Dict[str, Any]) -> T: | |||
] | |||
else: | |||
v = [cls().from_dict(item) for item in value[key]] | |||
elif isinstance(v, datetime): | |||
elif cls == datetime: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you change these to if cls is datetime?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For whatever reason, there are a lot of instances of cls == foo
in this file and none of cls is foo
. I think it makes sense to maintain consistency but I'm happy to change all of them to use is
if you prefer.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree with both of you, and it was courteous of you to match the existing style, @emosenkis . I'd say given that it's a style point, leave as-is and fix on a refactor.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @emosenkis for the test and fix!
Before we merge - please create an issue describing the problem (basically the same as the MR description) and link this MR to it. That will help other people discover both the bug and the fix when they run into it.
@@ -1191,16 +1191,15 @@ def from_dict(self: T, value: Dict[str, Any]) -> T: | |||
] | |||
else: | |||
v = [cls().from_dict(item) for item in value[key]] | |||
elif isinstance(v, datetime): | |||
elif cls == datetime: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree with both of you, and it was courteous of you to match the existing style, @emosenkis . I'd say given that it's a style point, leave as-is and fix on a refactor.
Thanks! Could you push to pypi? |
Fixes #333
The added test fails without the fix to
__init__.py
: