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

Enum .name property has str type #18786

Open
semohr opened this issue Mar 11, 2025 · 1 comment · May be fixed by #18797
Open

Enum .name property has str type #18786

semohr opened this issue Mar 11, 2025 · 1 comment · May be fixed by #18797
Labels
bug mypy got something wrong topic-enum

Comments

@semohr
Copy link

semohr commented Mar 11, 2025

Bug Report
I stumbled across a weird issue with literals and using the .name property with enums. I was wondering if this is expected behavior. Seems inconsistent to me.

To Reproduce

from enum import Enum
from typing import Literal


class Foo(Enum):
    A = 1
    B = 1


keys = Literal["A", "B", "C"]


def f(name: keys) -> None:
    print(name)


foo: Foo = Foo.A

f(foo.name) # Error: Argument 1 to "f" has incompatible type "str"; expected "Literal['A', 'B', 'C']"
f(Foo.A.name) # No Error

Expected Behavior

I would expect both versions to be valid.

Your Environment

  • Mypy version used: mypy 1.15.0 (compiled: yes)
  • Mypy command-line flags: none
  • Mypy configuration options from mypy.ini (and other config files): none
  • Python version used: 3.11.11
@semohr semohr added the bug mypy got something wrong label Mar 11, 2025
@A5rocks
Copy link
Collaborator

A5rocks commented Mar 11, 2025

cc @sobolevn

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong topic-enum
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants