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

Attempted to reuse member name in Enum definition #17234

Open
lanzz opened this issue May 11, 2024 · 0 comments
Open

Attempted to reuse member name in Enum definition #17234

lanzz opened this issue May 11, 2024 · 0 comments
Labels
bug mypy got something wrong

Comments

@lanzz
Copy link

lanzz commented May 11, 2024

I'm trying to follow the dynamic generation of Enum values example as described in the Enum documentation, and I'm getting this from mypy:

example.py:23: error: Attempted to reuse member name "i" in Enum definition "Slot"  [misc]

Now, I am trying to reuse the member name; in contrast to the Enum example, I have multiple loops that generate several sets of Enum values, and I am reusing the same loop variable across all of them (see the example below). mypy doesn't complain if I only have one loop in my Enum.

But considering I've already declared that member to be ignored in _ignore_, I don't see why I should use multiple distinct loop variables for all my loops, if all of them are going to be ignored by Enum plumbing anyway.

Moreover, the loop variable is used in a loop anyway; it will definitely get reassigned multiple values even during the first loop execution. I don't see how having multiple loops makes any difference to having a single loop.

To Reproduce

Run source through mypy:

import enum


class MyEnum(enum.StrEnum):
    _ignore_ = 'MyEnum i'
    MyEnum = vars()

    for i in range(8):
        MyEnum[f'SOMETHING_{i}'] = f'Something{i}'

    for i in range(4):
        MyEnum[f'ANOTHER_{i}'] = f'Another{i}'

Expected Behavior

mypy should not complain about this code.

Actual Behavior

mypy complains about the member being reused at the line of the second loop.

Your Environment

  • Mypy version used: 1.10.0
  • Mypy command-line flags: none
  • Mypy configuration options from mypy.ini (and other config files):
[mypy]
files = src
mypy_path = stubs
strict = True
install_types = True
  • Python version used: 3.11.9
@lanzz lanzz added the bug mypy got something wrong label May 11, 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