From 4e45b46bd4cb1a0a70a104ed5869d432b0855247 Mon Sep 17 00:00:00 2001 From: Nipunn Koorapati Date: Tue, 29 Dec 2020 20:03:24 +0000 Subject: [PATCH] Provide a type alias V on EnumTypeWrapper This should provide a runtime alias, which can be used in mypy stubs to provide better typing for enum values Fixes #8175 --- python/google/protobuf/internal/enum_type_wrapper.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/python/google/protobuf/internal/enum_type_wrapper.py b/python/google/protobuf/internal/enum_type_wrapper.py index 9ae0066584990..565cbcb5f13f1 100644 --- a/python/google/protobuf/internal/enum_type_wrapper.py +++ b/python/google/protobuf/internal/enum_type_wrapper.py @@ -45,6 +45,13 @@ class EnumTypeWrapper(object): DESCRIPTOR = None + # This is a type alias, which mypy typing stubs can type as + # a genericized parameter constrained to an int, allowing subclasses + # to be typed with more constraint + # Eg. + # def Name(self, number: MyGeneratedEnum.V) -> str + V = int + def __init__(self, enum_type): """Inits EnumTypeWrapper with an EnumDescriptor.""" self._enum_type = enum_type