Skip to content

Private property in subclass of Enum is not indexable #5312

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

Closed
jcrada opened this issue Jul 3, 2018 · 1 comment · Fixed by #8302
Closed

Private property in subclass of Enum is not indexable #5312

jcrada opened this issue Jul 3, 2018 · 1 comment · Fixed by #8302
Labels
bug mypy got something wrong false-positive mypy gave an error on correct code priority-1-normal

Comments

@jcrada
Copy link

jcrada commented Jul 3, 2018

Hi,

  • Are you reporting a bug, or opening a feature request?
    Reporting a minor bug in --strict mode when adding private property to Enum subclass.
import operator
import enum
from typing import Callable

@enum.unique
class Comparator(enum.Enum):
    LessThan = "<"
    LessThanOrEqualTo = "<="
    EqualTo = "=="
    NotEqualTo = "!="
    GreaterThanOrEqualTo = ">="
    GreaterThan = ">"

    __operator__ = {
        LessThan: operator.lt,
        LessThanOrEqualTo: operator.le,
        EqualTo: operator.eq,
        NotEqualTo: operator.ne,
        GreaterThanOrEqualTo: operator.ge,
        GreaterThan: operator.gt
    }

    @property
    def operator(self) -> Callable[[object, object], bool]:
        return Comparator.__operator__[self.value]

Comparator.LessThan.operator(1,2)
Comparator("<").operator(1,2)
  • What is the actual behavior/output?
    The output is the following, where the first line is a missing stub for operator, and the second is the error in this report:
<string>:26: warning: Returning Any from function declared to return "Callable[[object, object], bool]"
<string>:26: error: Value of type "Comparator" is not indexable
  • What is the behavior/output you expect?
    <string>:26: error: Value of type "Comparator" is not indexable should not appear.

  • What are the versions of mypy and Python you are using?
    mypy: 0.610
    python: 3.6.5
    Could not run mypy from master

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

@JelleZijlstra
Copy link
Member

Probably what's happening here is that mypy doesn't implement the enum feature where names surrounded by underscores don't turn into enum members. This should be relatively easy to fix.

@emmatyping emmatyping added bug mypy got something wrong priority-1-normal false-positive mypy gave an error on correct code labels Jul 3, 2018
JukkaL pushed a commit that referenced this issue Jan 17, 2020
Fixes #5312.

From @JelleZijlstra 's description, for underscored attributes, we don't make 
them as enum members.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong false-positive mypy gave an error on correct code priority-1-normal
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants