Skip to content

error: Enum() expects a string, tuple, list or dict literal as the second argument #5317

@sakurai-youhei

Description

@sakurai-youhei

Hello,

I'm reporting corner cases around at second argument of enum.Enum, which can be dict-like object.

Documentation says:

The second argument ... can be ... a mapping (e.g. dictionary) of names to values.

But mypy says:

Enum() expects a string, tuple, list or dict literal as the second argument

If mypy reports the error in these cases expectedly, please feel free to close this issue.

Thanks.


  • Are you reporting a bug, or opening a feature request?

Not sure since I don't have clear understanding on what mypy scopes as of now.

  • Please insert below the code you are checking with mypy,
from collections import OrderedDict
from enum import Enum


class DictLike:
    def __init__(self):
        self._keys = iter("a")

    def __iter__(self):
        return self

    def __next__(self) -> str:
        return next(self._keys)

    def __getitem__(self, key: str) -> str:
        return key


repro_py_line19 = Enum("", {"a": "a"})                                   # ok.
repro_py_line20 = Enum("", dict(a="a"))                                  # error but there is TODO comment.
repro_py_line21 = Enum("", {k: v for k, v in ["aa"]})                    # error because of DictionaryComprehension.
repro_py_line22 = Enum("", {s: s for s in "a"})                          # error but not sure if DictionaryComprehension also covers this format.
repro_py_line23 = Enum("", {s: s for s in map(str.strip, ["  a  \n"])})  # error but not sure if typing this is technically possible.
repro_py_line24 = Enum("", OrderedDict(a="a"))                           # error against Enum's duck typing??
repro_py_line25 = Enum("", DictLike())                                   # error against Enum's duck typing.

HERE is the TODO comment that I refer.

  • What is the actual behavior/output?
$ python -m mypy repro.py
repro.py:20: error: Enum() expects a string, tuple, list or dict literal as the second argument
repro.py:21: error: Enum() expects a string, tuple, list or dict literal as the second argument
repro.py:22: error: Enum() expects a string, tuple, list or dict literal as the second argument
repro.py:23: error: Enum() expects a string, tuple, list or dict literal as the second argument
repro.py:24: error: Enum() expects a string, tuple, list or dict literal as the second argument
repro.py:25: error: Enum() expects a string, tuple, list or dict literal as the second argument
  • What is the behavior/output you expect?
repro.py:23: error: Enum() expects a string, tuple, list or dict literal as the second argument
repro.py:25: error: Enum() expects a string, tuple, list or dict literal as the second argument

or

repro.py:25: error: Enum() expects a string, tuple, list or dict literal as the second argument

or

(no error)
  • What are the versions of mypy and Python you are using?

Python 3.7.0 (v3.7.0:1bf9cc5093, Jun 27 2018, 04:59:51) [MSC v.1914 64 bit (AMD64)]
mypy-0.620+dev.f5058bcb269a55b33a02c3e3fd345541839bf06e

  • What are the mypy flags you are using? (For example --strict-optional)

Nothing.

  • If mypy crashed with a traceback, please paste the full traceback below.

n/a.


See also python/typeshed#2305

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions