Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
superlevure opened this issue Jun 24, 2024 · 0 comments
Labels
bug mypy got something wrong

Comments

@superlevure
Copy link

superlevure commented Jun 24, 2024

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

  • 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
  • Python version used: 3.12
@superlevure superlevure added the bug mypy got something wrong label Jun 24, 2024
@superlevure superlevure changed the title Metaclass defined class attribute raise "name-defined" error Metaclass defined class attribute raise "name-defined" error when used as a type Jun 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong
Projects
None yet
Development

No branches or pull requests

1 participant