Skip to content

Commit 0707f2e

Browse files
authored
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
1 parent adc1f93 commit 0707f2e

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
@@ -43,6 +43,15 @@ class EnumTypeWrapper(object):
4343

4444
DESCRIPTOR = None
4545

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

0 commit comments

Comments
 (0)