Skip to content

Commit

Permalink
Provide a type alias field ValueType on EnumTypeWrapper (#8182)
Browse files Browse the repository at this point in the history
This should provide a runtime alias, which can be
used in mypy stubs to provide better typing for enum values

Fixes #8175
  • Loading branch information
nipunn1313 committed Oct 21, 2021
1 parent adc1f93 commit 0707f2e
Showing 1 changed file with 9 additions and 0 deletions.
9 changes: 9 additions & 0 deletions python/google/protobuf/internal/enum_type_wrapper.py
Expand Up @@ -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
Expand Down

0 comments on commit 0707f2e

Please sign in to comment.