From 0707f2e7f556c8396d6027d0533ec3a56d1061db Mon Sep 17 00:00:00 2001 From: Nipunn Koorapati Date: Thu, 21 Oct 2021 15:09:41 -0700 Subject: [PATCH] Provide a type alias field ValueType on EnumTypeWrapper (#8182) 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 | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/python/google/protobuf/internal/enum_type_wrapper.py b/python/google/protobuf/internal/enum_type_wrapper.py index 9a53999a43..65d2c7a5c0 100644 --- a/python/google/protobuf/internal/enum_type_wrapper.py +++ b/python/google/protobuf/internal/enum_type_wrapper.py @@ -43,6 +43,15 @@ 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 in .pyi stubs + # Eg. + # def MyGeneratedEnum(Message): + # ValueType = NewType('ValueType', int) + # def Name(self, number: MyGeneratedEnum.ValueType) -> str + ValueType = int + def __init__(self, enum_type): """Inits EnumTypeWrapper with an EnumDescriptor.""" self._enum_type = enum_type