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
pull/9144/head
Nipunn Koorapati 3 years ago committed by GitHub
parent adc1f931e3
commit 0707f2e7f5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 9
      python/google/protobuf/internal/enum_type_wrapper.py

@ -43,6 +43,15 @@ class EnumTypeWrapper(object):
DESCRIPTOR = None 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): def __init__(self, enum_type):
"""Inits EnumTypeWrapper with an EnumDescriptor.""" """Inits EnumTypeWrapper with an EnumDescriptor."""
self._enum_type = enum_type self._enum_type = enum_type

Loading…
Cancel
Save