-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Makes DESCRIPTOR
non nullable again
#7125
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
Makes DESCRIPTOR
non nullable again
#7125
Conversation
cc: @nipunn1313 |
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.
Looks good! Thanks!
@@ -3,6 +3,8 @@ | |||
google.protobuf\..*_pb2\..* | |||
|
|||
google.protobuf.internal.containers.BaseContainer.sort | |||
google.protobuf.internal.enum_type_wrapper.EnumTypeWrapper.DESCRIPTOR | |||
google.protobuf.message.Message.DESCRIPTOR |
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 would appreciate a short comment here.
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.
@Akuli - I'm new to mypy stubtests but I'm seeing the following locally (linux 5.13.0-28-generic, python 3.9.5).
Without these two allowlist entries:
python tests/stubtest_third_party.py protobuf
...
error: google.protobuf.internal.enum_type_wrapper.EnumTypeWrapper.DESCRIPTOR variable differs from runtime type None
Stub: at line 10
google.protobuf.descriptor.EnumDescriptor
Runtime:
None
error: google.protobuf.message.Message.DESCRIPTOR variable differs from runtime type None
Stub: at line 14
google.protobuf.descriptor.Descriptor
Runtime:
None
stubtest failed for protobuf
With these two allowlist entries:
python tests/stubtest_third_party.py protobuf
...
stubtest succeeded for protobuf
My interpretation of that is: while Message
and Descriptor
are both defined with a null DESCRIPTOR
, sublcasses of Message
and instances of EnumTypeWrapper
require this value to be set, and since These type stubs are intended for use with protoc generated python it's more accurate to make them non-nullable.
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.
while Message and Descriptor are both defined with a null DESCRIPTOR, sublcasses of Message and instances of EnumTypeWrapper require this value to be set, and since These type stubs are intended for use with protoc generated python it's more accurate to make them non-nullable.
Add a comment that says this to the allowlist :)
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.
Yep! Your understanding is perfect.
Just need a comment in the allowlist.txt file so that someone in the future doesn't revert your change by accident.
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.
Ah ok, got it, done.
…list.txt Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
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!
This change is a partial revert of 90f5422, specifically making the
DESCRIPTOR
class attribute ofMessage
andEnumTypeWrapper
non nullable again.What we're seeing is that when doing something like this:
The following mypy error occurs:
Item "None" of "Optional[Descriptor]" has no attribute "fields_by_name"
The
Message
abstract base class definesDESCRIPTOR
as None, but there's a comment specifying the type for subclasses ofMessage
: https://github.com/protocolbuffers/protobuf/blob/3be46483ba11605ec9d29ae439a16c4fc6bf02c3/python/google/protobuf/message.py#L77-L78This seems to imply that None is not actually a valid value for the
DESCRIPTOR
attribute of subclasses ofMessage
, and this seems to be reflected in the protoc generated python message types I've looked at (DESCRIPTOR
is never None).Message
is called an abstract base class in its docstring, it doesn't inherit from python'sabc.ABC
.Likewise The
EnumTypeWrapper
class definesDESCRIPTOR
as None, but requires it be set in its init:https://github.com/protocolbuffers/protobuf/blob/3be46483ba11605ec9d29ae439a16c4fc6bf02c3/python/google/protobuf/internal/enum_type_wrapper.py#L55-L58