-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
false positive for type-hinted Enum class instance variable #7402
Comments
From my quick peruse of the code, I believe that pylint will treat all Enum class variables as class constants, even if no value is assigned (the variable is for a type hint). |
Thanks for reporting. I think this is expected since Enum members are expected to be constants. Quoting from the docs: Because Enums are used to represent constants we recommend using UPPER_CASE names for enum members, and will be using that style in our examples. |
I'd also say it's working as intended. |
@mbyrnepr2 not quite so. As reference example, look at the Planet from Python docs. They don't write And here is my own example (that is why I've dug out this issue): from enum import Enum
class Color(Enum):
"""Represents colors as (red, green, blue) tuples."""
YELLOW = 250, 250, 0
KHAKI = 250, 250, 125
MAGENTA = 250, 0, 250
VIOLET = 250, 125, 250
CYAN = 0, 250, 250
AQUAMARINE = 125, 250, 250
# Class constant name "red" doesn't conform to UPPER_CASE naming style (invalid-name)
# Class constant name "green" doesn't conform to UPPER_CASE naming style (invalid-name)
# Class constant name "blue" doesn't conform to UPPER_CASE naming style (invalid-name)
red: int
green: int
blue: int
def __init__(self, red: int, green: int, blue: int) -> None:
self.red = red
self.green = green
self.blue = blue
@property
def as_hex(self) -> str:
"""Get hex 'abcdef' representation for a color."""
return f'{self.red:0{2}x}{self.green:0{2}x}{self.blue:0{2}x}' @Pierre-Sassoulas I disagree with you:) @huwcbjones clearly showed to us that Pylint sees no difference between enum member and its instance variables. Is it possible to reopen the issue? |
Thank you @andrei-korshikov 👍 Completely agree with you. |
…, from the ``__members__`` container of an ``Enum`` class. Refs pylint-dev/pylint#7402
…ontainer before doing the ``invalid-name`` check. Refs pylint-dev#7402
…ontainer before doing the ``invalid-name`` check. Refs pylint-dev#7402
…ontainer before doing the ``invalid-name`` check. Refs pylint-dev#7402
…s__ container (#2263) * Exclude type-annotated class attributes, which have no assigned value, from the ``__members__`` container of an ``Enum`` class. Refs pylint-dev/pylint#7402 * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Fix exising test. The value if now `Uninferable` in the case of an annotated attribute of an `enum.Enum` class with no assigned value. * Update astroid/brain/brain_namedtuple_enum.py Co-authored-by: Jacob Walls <jacobtylerwalls@gmail.com> * Update tests. * Update test: Use `infer()` instead of `inferred()`. * Update type annotations of PEP 695 nodes (#2264) These attributes cannot be none in real-world situations, see python/cpython#106145. * Update sphinx requirement from ~=7.0 to ~=7.1 (#2265) Updates the requirements on [sphinx](https://github.com/sphinx-doc/sphinx) to permit the latest version. - [Release notes](https://github.com/sphinx-doc/sphinx/releases) - [Changelog](https://github.com/sphinx-doc/sphinx/blob/master/CHANGES) - [Commits](sphinx-doc/sphinx@v7.0.0...v7.1.1) --- updated-dependencies: - dependency-name: sphinx dependency-type: direct:production ... Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> * Ensure a node is inferred in the case when there is only one member. * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Undo unintended changes. --------- Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Jacob Walls <jacobtylerwalls@gmail.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
…and ``_value_`` sunders of an Enum member value. Refs pylint-dev/pylint#7402
#2289) Refs pylint-dev/pylint#9015 * Fix a false positive for ``no-member`` when accessing the ``_name_`` and ``_value_`` sunders of an Enum member value. Refs pylint-dev/pylint#7402
…ontainer before doing the ``invalid-name`` check. Refs pylint-dev#7402
@mbyrnepr2 In which PR has this been fixed? Might be good to link here. |
Two changes for this fix. A Pylint PR: #8905 and an astroid PR: pylint-dev/astroid#2263 |
Bug description
As per https://mypy.readthedocs.io/en/stable/cheat_sheet_py3.html#classes, you can optionally declare instance variables in the class body. For normal classes, this is fine, but for Enums pylint raises a false positive
Define an enum with a type-hinted instance variable and you get an invalid name! 😞
Configuration
No response
Command used
(
pylint mwe.py
also works, I just wanted to filter out the irrelavent messages)Pylint output
Expected behavior
No error
Pylint version
OS / Environment
macOS
The text was updated successfully, but these errors were encountered: