Skip to content

Commit 428ef16

Browse files
committed
Provide a type alias field ValueType on EnumTypeWrapper
This should provide a runtime alias, which can be used in mypy stubs to provide better typing for enum values Fixes #8175
1 parent d662ec9 commit 428ef16

File tree

1 file changed

+9
-0
lines changed

1 file changed

+9
-0
lines changed

python/google/protobuf/internal/enum_type_wrapper.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,15 @@ class EnumTypeWrapper(object):
4545

4646
DESCRIPTOR = None
4747

48+
# This is a type alias, which mypy typing stubs can type as
49+
# a genericized parameter constrained to an int, allowing subclasses
50+
# to be typed with more constraint in .pyi stubs
51+
# Eg.
52+
# def MyGeneratedEnum(Message):
53+
# ValueType = NewType('ValueType', int)
54+
# def Name(self, number: MyGeneratedEnum.ValueType) -> str
55+
ValueType = int
56+
4857
def __init__(self, enum_type):
4958
"""Inits EnumTypeWrapper with an EnumDescriptor."""
5059
self._enum_type = enum_type

0 commit comments

Comments
 (0)