-
Notifications
You must be signed in to change notification settings - Fork 79
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
OneOf fields not typed as optional in constructor #580
Comments
In the mypy-protobuf generator, we have this for field in constructor_fields:
field_type = self.python_type(field, generic_container=True)
if self.fd.syntax == "proto3" and is_scalar(field) and field.label != d.FieldDescriptorProto.LABEL_REPEATED and not self.relax_strict_optional_primitives and not field.proto3_optional:
wl(f"{field.name}: {field_type} = ...,")
else:
wl(f"{field.name}: {field_type} | None = ...,") so it looks pretty explicit that we don't allow passing None in when the field is scalar. We have some tests of this here - that show that protos give TypeError when you pass in None for a scalar field. Hence only the message fields (not scalar) are optional. I don't actually know what happens for enums inside oneof. We have such a thing in the tests, but not in the runtime test cases. I think step one is figuring out what the runtime behavior is for enums inside oneof - do they operate more like messages or like scalars. Can do this by adding a test case here Once we know how it should behave, we can decide how to proceed. Thank you for the report! |
So here is some exploration I did. Interestingly I was able to pass
I think technically anything inside a |
ok interesting! Cool find - seems like the behavior is slightly different when the scalar field is part of a oneof vs in the message. Would take a fix along with a unit test that proves that the types match the runtime behavior. |
Given this proto:
The constructor looks something like this:
But the
OneOf
block is effectively marking thesome_enum
field as optional, but type checkers error when you try and passNone
to the constructor.The text was updated successfully, but these errors were encountered: