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

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

Closed
sakurai-youhei opened this issue Jul 4, 2018 · 6 comments

Comments

@sakurai-youhei
Copy link

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

@JelleZijlstra
Copy link
Member

Mypy needs to be able to determine the enum members during static analysis. It's unlikely that we'll ever gain support for most of the cases you mention.

@sakurai-youhei
Copy link
Author

Thank you, I understand it. :)

@dvarrazzo
Copy link

I think the message is misleading. It's not Enum to require literals as second argument, mypy requires them. AFAICS List[str] and Dict[str, Any] shouldn't be reported as errors.

@hf-kklein
Copy link

Dict[str, Any] shouldn't be reported as errors.

At least with mypy 0.971 Dict[str, Any] still causes the error message mentioned above.

@MestreLion
Copy link
Contributor

Mypy needs to be able to determine the enum members during static analysis. It's unlikely that we'll ever gain support for most of the cases you mention.

@JelleZijlstra This is perfectly understandable, but given this is a mypy limitation, can we please reopen this to change the misleading message, as @dvarrazzo suggested?

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

How about:
Non-literal string, tuple, list or dict as the second argument of Enum() is not supported

@MestreLion
Copy link
Contributor

For those coming now, this was partially fixed in #8664 to solve #8219, so at least some cases are covered.

hauntsaninja pushed a commit that referenced this issue Feb 2, 2023
Clarifying this is a mypy limitation, not an Enum() requirement

Not a true fix, but a workaround for #5317 to avoid confusion (as seen
by the many duplicates)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants