-
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
"None" as protobuf type names breaks mypy codegen #221
Comments
See also: #80 |
Here's a repo showing I split into many files so that I could reuse the name None multiple times in different contexts (which otherwise produces errors) |
Unfortunately, our tests missed this, because all our tests with reserved names for message types also had reserved names for the identifiers. Added a couple tests for the new scenario, reproed, and fixed by replacing such instances with `Any` (best we can do) Fixes #221
Great find and thank you for reproing! I was able to repro, add tests and whip up a fix (coming soon) |
Unfortunately, our tests missed this, because all our tests with reserved names for message types also had reserved names for the identifiers. Added a couple tests for the new scenario, reproed, and fixed by replacing such instances with `Any` (best we can do) Fixes #221
Unfortunately, our tests missed this, because all our tests with reserved names for message types also had reserved names for the identifiers. Added a couple tests for the new scenario, reproed, and fixed by replacing such instances with `Any` (best we can do) Fixes #221
Added you as a contributor in that diff! Really good bugreport. |
Unfortunately, our tests missed this, because all our tests with reserved names for message types also had reserved names for the identifiers. Added a couple tests for the new scenario, reproed, and fixed by replacing such instances with `Any` (best we can do) Fixes #221
@nipunn1313 great, and no problem. For the attempted solution, I agree that typing it as Any and not giving it a class will be an immediate fix to the syntactically invalid code, so that's cool (and I'm happy with the PR). What do you think about generating the types with the usual underscore suffix? E.g. from_, to_, None_, and so on? They can still be more helpful than Any in these contexts. On a more tangential note, and thinking out loud... The protobuf compiler handles these cases somewhat gracefully---I generated the normal Python code for this and committed it back to the repo above if you want to take a look. For top-level messages, protoc compiler inserts the types by their string name into the FWIW I tried some naive things to combine my suffix suggestion with the runtime business in the .pyi file and indeed mypy did not like it. For example: class Message(google.protobuf.message.Message):
DESCRIPTOR: google.protobuf.descriptor.Descriptor = ...
class None_(google.protobuf.message.Message):
DESCRIPTOR: google.protobuf.descriptor.Descriptor = ...
FIELD_FIELD_NUMBER: builtins.int
field: typing.Text = ...
def __init__(self,
*,
field : typing.Text = ...,
) -> None: ...
def ClearField(self, field_name: typing_extensions.Literal[u"field",b"field"]) -> None: ...
NONE_FIELD_NUMBER: builtins.int
@property
def none(self) -> global___Message.None_: ...
def __init__(self,
*,
none : typing.Optional[global___Message.None_] = ...,
) -> None: ...
def HasField(self, field_name: typing_extensions.Literal[u"none",b"none"]) -> builtins.bool: ...
def ClearField(self, field_name: typing_extensions.Literal[u"none",b"none"]) -> None: ...
setattr(Message, "None", Message.None_)
global___Message = Message This is runnable/syntactically valid, but there's no way to reference |
Yep - per https://developers.google.com/protocol-buffers/docs/reference/python-generated#keyword-conflicts - if the field name conflicts with a python attr - you end up with a field only accessible with getattr/setattr.
This means that you won't be able to access this type from a mypy typing context. mypy doesn't run code in the way that you describe - so lines like I could potentially imagine using overload + literal to get better typing on Something like
It seems like it might clutter the stubs for limited value, so I'm hesitant to really jump in with such a strategy. |
Unfortunately, our tests missed this, because all our tests with reserved names for message types also had reserved names for the identifiers. Added a couple tests for the new scenario, reproed, and fixed by replacing such instances with `Any` (best we can do) Fixes #221
Unfortunately, our tests missed this, because all our tests with reserved names for message types also had reserved names for the identifiers. Added a couple tests for the new scenario, reproed, and fixed by replacing such instances with `Any` (best we can do) Fixes #221
Unfortunately, our tests missed this, because all our tests with reserved names for message types also had reserved names for the identifiers. Added a couple tests for the new scenario, reproed, and fixed by replacing such instances with `Any` (best we can do) Fixes #221
Hmm, well you can access the field by name when the type name is what is reserved, rather than the field name: The getattr overloads would probably be acceptable if they covered all the use-cases, but since they don't, I agree with your assessment. Valiant attempt though! |
Unfortunately, our tests missed this, because all our tests with reserved names for message types also had reserved names for the identifiers. Added a couple tests for the new scenario, reproed, and fixed by replacing such instances with `Any` (best we can do) Fixes #221
Unfortunately, our tests missed this, because all our tests with reserved names for message types also had reserved names for the identifiers. Added a couple tests for the new scenario, reproed, and fixed by replacing such instances with `Any` (best we can do) Fixes #221
This is a good point. I'll file an issue for supporting messages with python reserved keyword names. |
A protobuf type
message None { ... }
won't appear in the .pyi file at all, and other messages which use the type will reference a non-existingglobal__None
If the message type is an inner message, it generates syntactically invalid code, e.g.
will generate a file with
global__Message.None
which is not valid Python syntax. Can update with a repo showing this behavior soon. I hypothesize this happens with all keywords as well, not justNone
.The text was updated successfully, but these errors were encountered: