Title: Incorrect types are used to describe google.protobuf enums · Issue #2521 · python/typeshed · GitHub
Open Graph Title: Incorrect types are used to describe google.protobuf enums · Issue #2521 · python/typeshed
X Title: Incorrect types are used to describe google.protobuf enums · Issue #2521 · python/typeshed
Description: Let's take google.protobuf.descriptor_pb2.FieldDescriptorProto as an example. In Python FieldDescriptorProto.TYPE_DOUBLE is instance of int In mypy FieldDescriptorProto.TYPE_DOUBLE is instance of FieldDescriptorProto.Type In Python Field...
Open Graph Description: Let's take google.protobuf.descriptor_pb2.FieldDescriptorProto as an example. In Python FieldDescriptorProto.TYPE_DOUBLE is instance of int In mypy FieldDescriptorProto.TYPE_DOUBLE is instance of F...
X Description: Let's take google.protobuf.descriptor_pb2.FieldDescriptorProto as an example. In Python FieldDescriptorProto.TYPE_DOUBLE is instance of int In mypy FieldDescriptorProto.TYPE_DOUBLE is instance ...
Opengraph URL: https://github.com/python/typeshed/issues/2521
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Incorrect types are used to describe google.protobuf enums","articleBody":"Let's take `google.protobuf.descriptor_pb2.FieldDescriptorProto` as an example.\r\n\r\nIn Python `FieldDescriptorProto.TYPE_DOUBLE` is instance of `int`\r\nIn mypy `FieldDescriptorProto.TYPE_DOUBLE` is instance of `FieldDescriptorProto.Type`\r\n\r\nIn Python `FieldDescriptorProto.Type` is instance of `EnumTypeWrapper`\r\nIn mypy `FieldDescriptorProto.Type` is subclass of `int` with additional methods\r\n\r\nIn Python `FieldDescriptorProto.Type.keys()` is of type `List[str]`\r\nIn mypy `FieldDescriptorProto.Type.keys()` is of type `List[bytes]`\r\n\r\nIn Python `EnumTypeWrapper.items` is an `instancemethod`\r\nIn mypy `EnumTypeWrapper.items` is a `classmethod`\r\n\r\nIn mypy this code works:\r\n```python\r\nfrom google.protobuf.descriptor_pb2 import FieldDescriptorProto\r\nprint(FieldDescriptorProto.TYPE_DOUBLE.values())\r\n```\r\nIn Python it fails:\r\n```python\r\nTraceback (most recent call last):\r\n File \"sandbox.py\", line 5, in \u003cmodule>\r\n print(FieldDescriptorProto.TYPE_DOUBLE.values())\r\nAttributeError: 'int' object has no attribute 'values'\r\n```\r\nCurrently this stub looks like this:\r\n```python\r\nclass FieldDescriptorProto(Message):\r\n class Type(int):\r\n @classmethod\r\n def Name(cls, number: int) -> bytes: ...\r\n\r\n @classmethod\r\n def Value(cls, name: bytes) -> FieldDescriptorProto.Type: ...\r\n\r\n @classmethod\r\n def keys(cls) -> List[bytes]: ...\r\n\r\n @classmethod\r\n def values(cls) -> List[FieldDescriptorProto.Type]: ...\r\n\r\n @classmethod\r\n def items(cls) -> List[Tuple[bytes, FieldDescriptorProto.Type]]: ...\r\n TYPE_DOUBLE: Type\r\n ...\r\n```\r\nI think it is possible to change it into this:\r\n```python\r\nclass FieldDescriptorProto(Message):\r\n FieldDescriptorProto_Type = typing.NewType('FieldDescriptorProto_Type', int)\r\n Type: EnumTypeWrapper[FieldDescriptorProto_Type]\r\n TYPE_DOUBLE: FieldDescriptorProto_Type\r\n ...\r\n```\r\nwhere `google.protobuf.internal.enum_type_wrapper.EnumTypeWrapper` looks like this:\r\n```python\r\nclass EnumTypeWrapper(Generic[T]):\r\n def Name(self, number: T) -> str: ...\r\n def Value(self, name: str) -> T: ...\r\n def keys(self) -> List[str]: ...\r\n def values(self) -> List[T]: ...\r\n def items(self) -> List[Tuple[str, T]]: ...\r\n```\r\nThe only problem I see is how to generate unique type name for enum values, in the example above I'm using concatenation of message name and enum name: `FieldDescriptorProto_Type`.\r\n\r\nIs it ok to create PR with this fix?","author":{"url":"https://github.com/vmagamedov","@type":"Person","name":"vmagamedov"},"datePublished":"2018-10-16T11:09:46Z","url":"https://github.com/python/typeshed/issues/2521"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:98db1efb-8a9b-198b-0187-eeaf99c11353 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | D27C:59847:CB229B:110ADBA:6B00532B |
| html-safe-nonce | 3d4a74ed4ef7ad91730306413169d5a56e2f185806921beb0dc20e56b6ed2c3b |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJEMjdDOjU5ODQ3OkNCMjI5QjoxMTBBREJBOjZCMDA1MzJCIiwidmlzaXRvcl9pZCI6IjEzMjkyMjYxNjQzOTYzODA5NzEiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | ba06816fa2dd6c3eb9713a22095e4f908e8cee57b212e2e376cf9aea3f875ef2 |
| hovercard-subject-tag | issue:370559239 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/python/typeshed/2521/issue_layout |
| twitter:image | https://opengraph.githubassets.com/e383359561cef935067851eb212ee6acc817271b65015b4a3979d149123aef64/python/typeshed/issues/2521 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/e383359561cef935067851eb212ee6acc817271b65015b4a3979d149123aef64/python/typeshed/issues/2521 |
| og:image:alt | Let's take google.protobuf.descriptor_pb2.FieldDescriptorProto as an example. In Python FieldDescriptorProto.TYPE_DOUBLE is instance of int In mypy FieldDescriptorProto.TYPE_DOUBLE is instance of F... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | vmagamedov |
| hostname | github.com |
| expected-hostname | github.com |
| None | f420f4996e6b1ff1ccd6d7683f8134f4b5f0f8a50124c210dbfe7490e5644abb |
| turbo-cache-control | no-preview |
| go-import | github.com/python/typeshed git https://github.com/python/typeshed.git |
| octolytics-dimension-user_id | 1525981 |
| octolytics-dimension-user_login | python |
| octolytics-dimension-repository_id | 31696383 |
| octolytics-dimension-repository_nwo | python/typeshed |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 31696383 |
| octolytics-dimension-repository_network_root_nwo | python/typeshed |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 26e00df917aba0f128135dc073ea0c5e33a70d40 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width