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

feat: sunder names in enum #2822

merged 3 commits into from Mar 6, 2019


None yet
2 participants
Copy link

commented Mar 3, 2019

This pull request wants to resolve #2766. Some details list here:

Python enum doc

Supported _sunder_ names

  • _name_ – name of the member
  • _value_ – value of the member; can be set / modified in __new__
  • _missing_ – a lookup function used when a value is not found; may be overridden
  • _ignore_ – a list of names, either as a list() or a str(), that will not be transformed into members, and will be removed from the final class
  • _order_ – used in Python 2/3 code to ensure member order is consistent (class attribute, removed during class creation)
  • _generate_next_value_ – used by the Functional API and by auto to get an appropriate value for an enum member; may be overridden

New in version 3.6: _missing_, _order_, _generate_next_value_

New in version 3.7: _ignore_


    def name(self):
        """The name of the Enum member."""
        return self._name_

    def value(self):
        """The value of the Enum member."""
        return self._value_

name and value both are wrapper of _name_ and _value.


        # still not found -- try _missing_ hook
            exc = None
            result = cls._missing_(value)
        except Exception as e:
            exc = e
            result = None

The type of value should to be consistent with __new__ parameter which is object. But the return type is not determinate. It seems like it can be Any.

From the doc example, _order_ should be str.

I'm not sure about _generate_next_value_. From

        if isinstance(names, (tuple, list)) and names and isinstance(names[0], str):
            original_names, names = names, []
            last_values = []
            for count, name in enumerate(original_names):
                value = first_enum._generate_next_value_(name, start, count, last_values[:])
                names.append((name, value))

It can infer that name is str, start is int, count is int and last_values could be List[Any]. But in the docstring of Flag(subclass of Enum).

        name: the name of the member
        start: the initital start value or None
        count: the number of existing members
        last_value: the last value assigned or None

start could be None. But I don't see anywhere passing None.

Another problem is _generate_next_value_ is not decorated by staticmethod, mypy will report Self argument missing for a non-static method (or an invalid type for self).

Copy link

left a comment

Thank you for the pull request. Aside from the comments below, a few style nits:

  • New annotations should use PEP 526-style variable annotations, i.e. _name_: str etc., even if the surrounding code doesn't use them yet. If you prefer you could also change existing fields to PEP 526-style.
  • We try to minimize vertical whitespace in stubs. Classes bodies should have no blank lines.
Show resolved Hide resolved stdlib/3/enum.pyi Outdated
Show resolved Hide resolved stdlib/3/enum.pyi Outdated
Show resolved Hide resolved stdlib/3/enum.pyi Outdated

Hanaasagi added some commits Mar 6, 2019


srittau approved these changes Mar 6, 2019

@srittau srittau merged commit b588d43 into python:master Mar 6, 2019

1 check passed

continuous-integration/travis-ci/pr The Travis CI build passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.