Skip to content
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

Fix from_dict() in the presence of optional datetime fields. #329

Merged
merged 1 commit into from
Feb 3, 2022
Merged

Fix from_dict() in the presence of optional datetime fields. #329

merged 1 commit into from
Feb 3, 2022

Conversation

emosenkis
Copy link
Contributor

@emosenkis emosenkis commented Jan 31, 2022

Fixes #333

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

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:
Copy link
Collaborator

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?

Copy link
Contributor Author

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.

Copy link
Collaborator

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.

Copy link
Collaborator

@kalzoo kalzoo left a 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:
Copy link
Collaborator

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.

@Gobot1234 Gobot1234 merged commit 8c727d9 into danielgtaylor:master Feb 3, 2022
@emosenkis
Copy link
Contributor Author

Thanks! Could you push to pypi?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

from_dict() raises TypeError in the presence of proto3 optional google.protobuf.Timestamp field
3 participants