Skip to content

Special case Enum _generate_next_value_ #7591

Closed

Description

  • Are you reporting a bug, or opening a feature request? Bug, I think :)

I'm trying to set up a convenience Enum subclass:

from enum import Enum
from typing import Any, List


class AutoNameEnum(Enum):
    def _generate_next_value_(name, start: int, count: int, last_values: List[Any]) -> str:  # noqa: N805
        return str(name).lower()

I would expect this to work since Enum is a core class, but mypy reports the following error:

Signature of "generate_next_value" incompatible with supertype "Enum"

It seems any typing of this signature leads to the same error. Typing name: str results in the previous error and

Self argument missing for a non-static method (or an invalid type for self)

while stripping all types instead results in the following error:

Function is missing a type annotation

  • What are the versions of mypy and Python you are using?
$ mypy --version
mypy 0.730
$ python --version
Python 3.6.8
  • What are the mypy flags you are using?
[mypy]
check_untyped_defs = true
disallow_any_generics = true
disallow_untyped_defs = true
follow_imports = skip
ignore_missing_imports = true
no_implicit_optional = true
warn_redundant_casts = true
warn_return_any = true
warn_unused_ignores = true
  • Workaround
def _generate_next_value_(  # type: ignore
    name: str, start: int, count: int, last_values: List[Any]  # noqa: N805
) -> str:
    return name.lower()

Activity

  1. gvanrossum commented on Oct 1, 2019

    @gvanrossum
    Member

    Maybe adding @staticmethod helps? Or move the helper out of the class (since it doesn't seem to have anything to do with the class)?

  2. gvanrossum commented on Oct 1, 2019

    @gvanrossum
    Member

    Oh wait, I didn't realize that you were overriding an internal API. But if @staticmethod works let's close this issue. (I think mypy was trying to tell you that. :-)

  3. kokumura commented on Jul 2, 2020

    @kokumura

    In Python 3.8, @staticmethod won't work.

    from enum import Enum, auto
    from typing import Any, List
    
    class AutoNameEnum(Enum):
        @staticmethod
        def _generate_next_value_(
            name, start: int, count: int, last_values: List[Any]
        ) -> str:
            return str(name).lower()
    
        foo = auto()
        bar = auto()

    results:

    Traceback (most recent call last):
      File "auto_name_enum.py", line 5, in <module>
        class AutoNameEnum(Enum):
      File "auto_name_enum.py", line 12, in AutoNameEnum
        foo = auto()
      File "/Users/okumura/.pyenv/versions/3.8.1/lib/python3.8/enum.py", line 104, in __setitem__
        value.value = self._generate_next_value(key, 1, len(self._member_names), self._last_values[:])
    TypeError: 'staticmethod' object is not callable
    
  4. Venom1991 commented on Feb 17, 2021

    @Venom1991

    This problem bit me just now and the @staticmethod approach seems to work in Python 3.9.1 (no exception is being thrown).

  5. l0b0 commented on Jan 10, 2023

    @l0b0

    @Venom1991 @staticmethod doesn't seem to work in Python 3.9.15:

    Run poetry run cdk deploy --all --require-approval never --strict --change-set-name "ci-${GITHUB_RUN_ID}"
      poetry run cdk deploy --all --require-approval never --strict --change-set-name "ci-${GITHUB_RUN_ID}"
      shell: /usr/bin/bash -e {0}
      env: […]
    Traceback (most recent call last):
      File "/home/runner/work/geostore/geostore/app.py", line 7, in <module>
        from infrastructure.application_stack import Application
      File "/home/runner/work/geostore/geostore/infrastructure/application_stack.py", line 8, in <module>
        from .constructs.api import API
      File "/home/runner/work/geostore/geostore/infrastructure/constructs/api.py", line 18, in <module>
        from .table import Table
      File "/home/runner/work/geostore/geostore/infrastructure/constructs/table.py", line 6, in <module>
        from geostore.parameter_store import ParameterName
      File "/home/runner/work/geostore/geostore/geostore/parameter_store.py", line 24, in <module>
        class ParameterName(Enum):
      File "/home/runner/work/geostore/geostore/geostore/parameter_store.py", line 31, in ParameterName
        GIT_COMMIT = auto()
      File "/opt/hostedtoolcache/Python/3.9.15/x64/lib/python3.9/enum.py", line 142, in __setitem__
        value.value = self._generate_next_value(
    TypeError: 'staticmethod' object is not callable
    
    Subprocess exited with error 1
    Error: Process completed with exit code 1.
    

    Is this caused by python/cpython#86844?

  6. added a commit that references this issue on Apr 3, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions