python / mypy

Optional static typing for Python
https://www.mypy-lang.org/
Other
17.85k stars 2.74k forks source link

Metaclass defined class attribute raise "name-defined" error when used as a type #17434

Open superlevure opened 1 week ago

superlevure commented 1 week ago

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

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