-
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
proto3 Extensions defined at module level produce bad mypy ExtensionDict types #244
Comments
Hi! Just to help debug, what version of mypy-protobuf were you on
previously?
Also which versions of mypy before and after?
Thanks!
—Nipunn
…On Tue, Jul 13, 2021 at 10:18 AM Pascal Corpet ***@***.***> wrote:
I'm trying the new versions of mypy-protobuf 2.6 together with
types-protobuf 3.17.0 and I'm getting some errors when using extensions.
syntax = "proto3";
extend google.protobuf.FieldOptions {
// Give a bit more semantics to a field so that we can programatically
// identify sensitive fields to handle with care.
FieldUsage field_usage = 50000;
}
enum FieldUsage {
// Many fields are not tagged, this is either because they have not been
// tagged yet or because they define the user's project and as such do not
// need special care yet. In the future we might add a tag for those if there
// is a need.
UNKNOWN_FIELD_USAGE = 0;
// A field that may uniquely identify a user.
PERSONAL_IDENTIFIER = 1;
// A field that is only used for the app's UX or UI: it does not contain
// actual user data.
APP_ONLY = 2;
// A field whose value is provided by the user as a feedback.
USER_FEEDBACK = 3;
// A field populated by the app as a result of an algorithm.
ALGORITHM_RESULT = 4;
}
extensions = field_descriptor.GetOptions().Extensions
field_usage = extensions[options_pb2.field_usage]
Here's what mypy tells me for line 1 and 2 (respectively)
Need type annotation for "field_usage"
Invalid index type "FieldDescriptor" for "_ExtensionDict[FieldOptions]"; expected type "_ExtensionFieldDescriptor[FieldOptions, <nothing>]"
I've dug a bit and it seems that mypy-protobuf types
options_pb2.field_usage as a generic
google.protobuf.descriptor.FieldDescriptor instead of having a _ExtensionFieldDescriptor[FieldOptions,
options_pb2.FieldUsage>].
Does someone else have this issue? Should I change the generated type?
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
<#244>, or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAJ5PI7XXHVP32NISATBSHTTXRKOBANCNFSM5AJLUXSQ>
.
|
@EPronovost added some support for better typing on extension fields in proto2, but it seems like it may have broken proto3 - I can see the issue you're describing. The extensions change landed in mypy-protobuf 1.24 (see CHANGELOG.md) See #145 and #154 Trying to write some tests for the issue you're seeing and coming up w/ some kind of patch |
ok I think I've isolated the issue I think python/typeshed#5754 may have been premature, as it appears that there is still use cases for indexing by FieldOptions rather than via _ExtensionFieldDescriptor I believe the issue is that extensions scoped within messages are supported, but ones at global scope were overlooked
My plan is to spend a few minutes trying to fix forward the extension issue you're describing. quick workaround is to use types-protobuf 0.1.14 |
Proto itself supports primitives, not just messages. See nipunn1313/mypy-protobuf#244 for an example motivating this change. Test Plan: I was able to use MYPYPATH to test an updated version of mypy-protobuf with this change.
Proto itself supports primitives, not just messages. See nipunn1313/mypy-protobuf#244 for an example motivating this change. Test Plan: I was able to use MYPYPATH to test an updated version of mypy-protobuf with this change.
Proto itself supports primitives, not just messages. See nipunn1313/mypy-protobuf#244 for an example motivating this change. Test Plan: I was able to use MYPYPATH to test an updated version of mypy-protobuf with this change.
Update types-protobuf pin from typeshed Depends on python/typeshed#5774 Fixes #244
I was able to whip up fixes with python/typeshed#5774 and #245. |
Proto itself supports primitives, not just messages. See nipunn1313/mypy-protobuf#244 for an example motivating this change. Test Plan: I was able to use MYPYPATH to test an updated version of mypy-protobuf with this change.
Wow, thanks so much for the quick fix |
Update types-protobuf pin from typeshed Depends on python/typeshed#5774 Fixes #244
Update types-protobuf pin from typeshed Depends on python/typeshed#5774 Fixes #244
Update types-protobuf pin from typeshed Depends on python/typeshed#5774 Fixes #244
Should be better now @pcorpet - you can try out the unreleased mypy-protobuf (instructions on README) to see if the issue is resolved for you. LMK - would be nice to get some validation before release that it's solving the issue for you. |
It works way better thank you. One last remaining factor is when the option is a syntax = "proto3";
extend google.protobuf.FieldOptions {
// Give more information about the format of a string field.
repeated StringFormat string_format = 50001;
}
enum StringFormat {
// Many fields are not tagged, this is either because they have not been
// tagged yet or because they do not have a specific format.
UNKNOWN_STRING_FORMAT = 0;
// A field whose value(s) are in natural language.
NATURAL_LANGUAGE = 1;
// A field whose value is a URL.
URL_FORMAT = 2;
// A field whose value is only a partial sentence, and as such should never start with an
// uppercase letter or end with a punctuation mark.
PARTIAL_SENTENCE = 3;
} The the type of the extension shouldn't be error: "V" has no attribute "__iter__" (not iterable) |
Ah let me reopen and fix that issue as well. |
This should not change any of the generated code - simply refactoring into a helper in order to unblock progress on #244 - which wants to apply this to extensions.
This should not change any of the generated code - simply refactoring into a helper in order to unblock progress on #244 - which wants to apply this to extensions.
This should not change any of the generated code - simply refactoring into a helper in order to unblock progress on #244 - which wants to apply this to extensions.
This should not change any of the generated code - simply refactoring into a helper in order to unblock progress on #244 - which wants to apply this to extensions.
See nipunn1313/mypy-protobuf#244 for the inspiration for this. Repeated extensions are allowed by protobuf, and they generate to extension values with repeated fields. Notably map fields (ScalarMap and MessageMap) are NOT allowed to be extension values - producing errors as such - so those are omitted. testproto/test_extensions3.proto:19:6: Map fields are not allowed to be extensions.
See nipunn1313/mypy-protobuf#244 for the inspiration for this. Repeated extensions are allowed by protobuf, and they generate to extension values with repeated fields. Notably map fields (ScalarMap and MessageMap) are NOT allowed to be extension values - producing errors as such - so those are omitted. testproto/test_extensions3.proto:19:6: Map fields are not allowed to be extensions.
See nipunn1313/mypy-protobuf#244 for the inspiration for this. Repeated extensions are allowed by protobuf, and they generate to extension values with repeated fields. Notably map fields (ScalarMap and MessageMap) are NOT allowed to be extension values - producing errors as such - so those are omitted. testproto/test_extensions3.proto:19:6: Map fields are not allowed to be extensions.
See nipunn1313/mypy-protobuf#244 for the inspiration for this. Repeated extensions are allowed by protobuf, and they generate to extension values with repeated fields. Notably map fields (ScalarMap and MessageMap) are NOT allowed to be extension values - producing errors as such - so those are omitted. testproto/test_extensions3.proto:19:6: Map fields are not allowed to be extensions.
I confirm, that this is now working as expected for my codebase (using types-protobuf 3.17.3). Thanks @nipunn1313 ! |
Can you publish a new version of |
Working on #227 - will try to complete that and then produce a new version. It is coming! |
I'm trying the new versions of
mypy-protobuf
2.6 together withtypes-protobuf
3.17.0 and I'm getting some errors when using extensions.Here's what mypy tells me for line 1 and 2 (respectively)
I've dug a bit and it seems that
mypy-protobuf
typesoptions_pb2.field_usage
as a genericgoogle.protobuf.descriptor.FieldDescriptor
instead of having a_ExtensionFieldDescriptor[FieldOptions, options_pb2.FieldUsage>]
.Does someone else have this issue? Should I change the generated type?
The text was updated successfully, but these errors were encountered: