from enum import Enum
class Message:
class Direction(Enum):
IN = "IN"
OUT = "OUT"
class MetaProtoWrapper(type):
def __getattribute__(cls, name: str):
try:
return super().__getattribute__(name)
except AttributeError:
return getattr(cls.Meta.protocol_buffer_type, name)
class ProtoWrapper(metaclass=MetaProtoWrapper):
class Meta:
protocol_buffer_type = Message
direction_in = ProtoWrapper.Direction.IN # OK
direction_in: ProtoWrapper.Direction = ProtoWrapper.Direction.IN # Name "ProtoWrapper.Direction" is not defined [name-defined]
Expected Behavior
ProtoWrapper.Direction should be a valid type
Actual Behavior
ProtoWrapper.Direction raises name-defined error
Your Environment
Mypy version used: mypy 1.10.0 (compiled: yes)
Mypy command-line flags: NA
Mypy configuration options from mypy.ini (and other config files): NA
Using a Metaclass to define dynamic class (not instance) attributes raises
name-defined
error when trying to use an attribute as a type.To Reproduce
Gist URL: https://gist.github.com/mypy-play/bb0277c6f339cbf374a1d2288cb7d5e6
Playground URL: https://mypy-play.net/?mypy=latest&python=3.12&gist=bb0277c6f339cbf374a1d2288cb7d5e6
Expected Behavior
ProtoWrapper.Direction
should be a valid typeActual Behavior
ProtoWrapper.Direction
raisesname-defined
errorYour Environment
mypy 1.10.0 (compiled: yes)
mypy.ini
(and other config files): NA