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

False positive no-member for subclass of enum.Enum #533

Closed
pylint-bot opened this issue May 8, 2015 · 16 comments · Fixed by pylint-dev/astroid#1121
Closed

False positive no-member for subclass of enum.Enum #533

pylint-bot opened this issue May 8, 2015 · 16 comments · Fixed by pylint-dev/astroid#1121
Labels
Bug 🪲 False Positive 🦟 A message is emitted but nothing is wrong with the code Help wanted 🙏 Outside help would be appreciated, good for new contributors High priority Issue with more than 10 reactions Needs astroid update Needs an astroid update (probably a release too) before being mergable

Comments

@pylint-bot
Copy link

pylint-bot commented May 8, 2015

Originally reported by: BitBucket: iceout, GitHub: @iceout?


Python-2.7.6 + enum34 (Enum backported from Python-3.4.0)

test code:

# pylint: disable=missing-docstring, invalid-name


from enum import Enum


class OrderedEnum(Enum):

    def __ge__(self, other):
        if self.__class__ is other.__class__:
            return self.value >= other.value  # line 11
        return NotImplemented

    def __gt__(self, other):
        if self.__class__ is other.__class__:
            return self.value > other.value  # line 16
        return NotImplemented


class Color(OrderedEnum):
    red = 0
    green = 1


class People(Enum):
    jack = 0
    john = 1


print(Color.red.value)  # line 29
print(People.jack.name)

pylint -E test.py:

E: 11,19: Instance of 'OrderedEnum' has no 'value' member (no-member)
E: 16,19: Instance of 'OrderedEnum' has no 'value' member (no-member)
E: 29, 6: Instance of 'int' has no 'value' member (no-member)

@pylint-bot
Copy link
Author

Original comment by Claudiu Popa (BitBucket: PCManticore, GitHub: @PCManticore):


Yes, this was intentional (handling subclasses for enums was a little complicated when support for them was added). This should be fixed in astroid at some point, here's the relevant code: https://bitbucket.org/logilab/astroid/src/f75482328c8c39538c74fa9a85915ec5c58ce1fa/astroid/brain/py2stdlib.py?at=default#cl-281

@PCManticore PCManticore added this to the 1.6.0 milestone May 9, 2016
@PCManticore PCManticore modified the milestones: 2.1, 2.0, 2.3, 2.2 Jun 15, 2016
@PCManticore PCManticore removed this from the 2.4 milestone Aug 11, 2016
@xvjiarui
Copy link

have the same problem.

import inspect
from enum import Enum

class ChoiceEnum(Enum):

	@classmethod
	def choices(cls):
		# get all members of the class
		members = inspect.getmembers(cls, lambda m: not(inspect.isroutine(m)))
		# filter down to just properties
		props = [m for m in members if not(m[0][:2] == '__')]
		# format into django choice tuple
		choices = tuple([(p[1].value, p[0]) for p in props])
		return choices

class BookingState(ChoiceEnum):
	IS_BOOKING = 0
	CONFIRMED = 1
	PAID = 2

class TourOfferingState(ChoiceEnum):
	OPEN = 0
	CONFIRMED = 1
	CANCELED = 2
	CLOSED = 3 

@LJBD
Copy link

LJBD commented Jun 29, 2018

Is there anything that can be done in this issue?
I've got the same problem subclassing IntEnum in Python 3.6.

@PCManticore
Copy link
Contributor

@LJBD Apart of disabling the check for now, no, there's no other solution. It would be great to have this fixed, but can't promise exactly when that's going to happen, unless someone proposes a patch to speed this up.

@mplanchard
Copy link

FYI a partial workaround here is to define your enum overrides as a mixin, and then subclass enum explicitly in all of your "subclasses." For example:

from enum import Enum

class SpecialEnumMixin:
  def __new__(cls, value, info):
    obj = object.__new__(cls)
    obj._value_ = value
    obj._info = info
    return obj

  @property
  def info(self):
    return self._info

class SpecialEnum(SpecialEnumMixin, Enum):
  A = ("a", "this is a")
  B = ("b", "this is b")

assert SpecialEnum.A.info == "this is a"
assert SpecialEnum("a") is SpecialEnum.A

Doing it this way, pylint recognizes the "subclass" correctly as an enum.

@dufferzafar
Copy link

Is there a way to disable such no-member violations? Without disabling ALL of them?

@gbrennon
Copy link

gbrennon commented Jun 5, 2020

Why people didn't fix this?

@mplanchard
Copy link

@gbrennon looks like they're willing to accept patches if you can find the cause of the issue and fix it yourself

@manimathma
Copy link

Is there a way to disable such no-member violations? Without disabling ALL of them?

@dufferzafar : this might help https://pylint.readthedocs.io/en/latest/user_guide/message-control.html

@Pierre-Sassoulas
Copy link
Member

In pylint 2.8 the result is now:

************* Module functional.e.enum_subclass
enum_subclass.py:13:19: W0143: Comparing against a callable, did you omit the parenthesis? (comparison-with-callable)
enum_subclass.py:18:19: W0143: Comparing against a callable, did you omit the parenthesis? (comparison-with-callable)
enum_subclass.py:32:6: E1101: Instance of 'int' has no 'value' member (no-member)

So it's still an issue. The relevant code seems to be here: https://github.com/PyCQA/astroid/blob/master/astroid/brain/brain_namedtuple_enum.py

@Pierre-Sassoulas Pierre-Sassoulas added False Positive 🦟 A message is emitted but nothing is wrong with the code Help wanted 🙏 Outside help would be appreciated, good for new contributors Needs astroid update Needs an astroid update (probably a release too) before being mergable labels Apr 24, 2021
@Pierre-Sassoulas Pierre-Sassoulas added the High priority Issue with more than 10 reactions label May 30, 2021
@Pierre-Sassoulas Pierre-Sassoulas pinned this issue Jul 1, 2021
@Pierre-Sassoulas Pierre-Sassoulas changed the title Not support subclass of Enum(enum34) False positive no-member for subclass of enum.Enum Jul 1, 2021
@Pierre-Sassoulas Pierre-Sassoulas unpinned this issue Aug 16, 2021
@OldIMP
Copy link

OldIMP commented Oct 10, 2021

Hi @Pierre-Sassoulas:

I still get the false positive E1101 for self defined members.

Example:

  • Enum Layout w/ self defined members col & row
  • Here will then be reported falsely as E1101:

    r_quiz.py:174:12: E1101: Instance of 'SIMPLE' has no 'row' member (no-member)
    r_quiz.py:174:12: E1101: Instance of 'SCHRIFTLICH' has no 'row' member (no-member)
    r_quiz.py:174:25: E1101: Instance of 'SIMPLE' has no 'col' member (no-member)
    r_quiz.py:174:25: E1101: Instance of 'SCHRIFTLICH' has no 'col' member (no-member)

pylint --version

pylint 2.11.1
astroid 2.8.2
Python 3.9.7 | packaged by conda-forge | (default, Sep 29 2021, 19:15:42) [MSC v.1916 64 bit (AMD64)]

Should I create a new issue for it?

@Pierre-Sassoulas
Copy link
Member

That syntax is very handy, I did not know it worked. Could you open a new issue, please ?

@OldIMP
Copy link

OldIMP commented Oct 10, 2021

#5138 created

@bluefish6
Copy link

Hi @Pierre-Sassoulas, I see the same behaviour when I'm importing enum.Enum with a different name (to prevent confusion when also doing from sqlalchemy.dialects.postgresql import ENUM):

from enum import Enum as PyEnum


class MyEnum(PyEnum):
    ENUM_KEY = "enum_value"


foo = MyEnum.ENUM_KEY.value
pylint example.py 
************* Module example
example.py:8:6: E1101: Instance of 'str' has no 'value' member (no-member)

The error goes away when Enum is imported without renaming to PyEnum

$ pylint --version

pylint 2.12.2
astroid 2.9.3
Python 3.8.10 (default, Nov 26 2021, 20:14:08) 
[GCC 9.3.0]

Should I open a new issue or this one can be reopened?

@Pierre-Sassoulas
Copy link
Member

Thank you for the report, could you open a new issue, please ? :)

@bluefish6
Copy link

Thank you for the report, could you open a new issue, please ? :)

Opened #5776, thanks! :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug 🪲 False Positive 🦟 A message is emitted but nothing is wrong with the code Help wanted 🙏 Outside help would be appreciated, good for new contributors High priority Issue with more than 10 reactions Needs astroid update Needs an astroid update (probably a release too) before being mergable
Projects
None yet
Development

Successfully merging a pull request may close this issue.