-
Notifications
You must be signed in to change notification settings - Fork 16k
Add reserved option range for mypy-protobuf #10446
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
Conversation
Mypy-protobuf is here https://github.com/nipunn1313/mypy-protobuf It currently uses extensions as explained here. https://github.com/nipunn1313/mypy-protobuf/blob/main/proto/mypy_protobuf/extensions.proto It's a fairly stable project for generating type stubs for protobuf. It's been around since ~2015 (open source since 2017). Since open sourcing it, it would make sense to reserve some extension numbers in the global range. See nipunn1313/mypy-protobuf#396
|
It seems like you only need one extension number since you are using a message with subfields. |
|
Hi @fowles - appreciate the quick turnaround. The proto extension (as used today) uses three option extensions (from https://github.com/nipunn1313/mypy-protobuf/blob/main/proto/mypy_protobuf/extensions.proto) extend google.protobuf.FieldOptions {
// Tells mypy to use a specific newtype rather than the normal type for this field.
optional string casttype = 60000;
// Tells mypy to use a specific type for keys; only makes sense on map fields
optional string keytype = 60002;
// Tells mypy to use a specific type for values; only makes sense on map fields
optional string valuetype = 60003;
}Example usage is like this (from https://github.com/nipunn1313/mypy-protobuf/blob/main/proto/testproto/test.proto#L70): message Simple1 {
optional uint32 user_id = 21 [(mypy_protobuf.casttype)="test/test_generated_mypy.UserId"];
optional string email = 22 [(mypy_protobuf.casttype)="test/test_generated_mypy.Email"];
map<uint32, string> email_by_uid = 23 [
(mypy_protobuf.keytype)="test/test_generated_mypy.UserId",
(mypy_protobuf.valuetype)="test/test_generated_mypy.Email"
];
}Which generates class Simple1:
user_id: test.test_generated_mypy.UserId
email: test.test_generated_mypy.Email
@property
def email_by_uid(self) -> google.protobuf.internal.containers.ScalarMap[test.test_generated_mypy.UserId, test.test_generated_mypy.Email]: ...As you can see here, there are 3 options used (so far). I spent some time reading through the docs and found this - https://developers.google.com/protocol-buffers/docs/proto#customoptions - specifically We could do this, however it would be a non-backward compatible change to mypy-protobuf. message Options {
// Tells mypy to use a specific newtype rather than the normal type for this field.
optional string casttype = 1;
// Tells mypy to use a specific type for keys; only makes sense on map fields
optional string keytype = 2;
// Tells mypy to use a specific type for values; only makes sense on map fields
optional string valuetype = 3;
}
extend google.protobuf.FieldOptions {
optional Options options = 1151;
}Callsite would have to switch to: message Simple1 {
optional uint32 user_id = 21 [(mypy_protobuf.options).casttype="test/test_generated_mypy.UserId"];
optional string email = 22 [(mypy_protobuf.options).casttype="test/test_generated_mypy.Email"];
map<uint32, string> email_by_uid = 23 [
(mypy_protobuf.options).keytype="test/test_generated_mypy.UserId",
(mypy_protobuf.options).valuetype="test/test_generated_mypy.Email"
];
}I would really prefer to be able to make this switch in a code-compatible way, by reserving at least 3 numbers. One possibility is that we could reserve 4 numbers. 3 for the legacy options and a 4th one for the |
|
I went ahead and implemented this "reserve 4 numbers" strategy. 3 legacy numbers (for backward compat) and one main number going forward. |
Mypy-protobuf is here
https://github.com/nipunn1313/mypy-protobuf
It currently uses extensions as explained here.
https://github.com/nipunn1313/mypy-protobuf/blob/main/proto/mypy_protobuf/extensions.proto
It's a fairly stable project for generating type stubs for protobuf.
It's been around since ~2015 (open source since 2017). Since open
sourcing it, it would make sense to reserve some extension numbers in
the global range.
See
nipunn1313/mypy-protobuf#396