diff options
| author | David Liu <david@cs.toronto.edu> | 2021-08-08 18:19:24 -0400 |
|---|---|---|
| committer | Pierre Sassoulas <pierre.sassoulas@gmail.com> | 2021-08-12 22:08:52 +0200 |
| commit | 97115acfbb44c2c17f58e507a27ddb45e464d87e (patch) | |
| tree | b85356db5843f7b11e220dcb7801848542da2ea7 | |
| parent | ca3897d1084e45107481ebba08a7935b814135a5 (diff) | |
| download | astroid-git-97115acfbb44c2c17f58e507a27ddb45e464d87e.tar.gz | |
Remove early return in _is_enum_subclass
| -rw-r--r-- | astroid/brain/brain_namedtuple_enum.py | 25 | ||||
| -rw-r--r-- | tests/unittest_brain.py | 32 |
2 files changed, 28 insertions, 29 deletions
diff --git a/astroid/brain/brain_namedtuple_enum.py b/astroid/brain/brain_namedtuple_enum.py index 82408a2c..9cbeae1f 100644 --- a/astroid/brain/brain_namedtuple_enum.py +++ b/astroid/brain/brain_namedtuple_enum.py @@ -545,30 +545,7 @@ def infer_typing_namedtuple(node, context=None): def _is_enum_subclass(cls: astroid.ClassDef) -> bool: - """Return whether cls is a subclass of an Enum. - - Warning: For efficiency purposes, this function immediately returns False if enum hasn't - been imported in the module of the ClassDef. This means it fails to detect an Enum - subclass that is imported from a new module and subclassed, e.g. - - # a.py - import enum - - class A(enum.Enum): - pass - - # b.py - from a import A - - class B(A): # is_enum_subclass returns False - attr = 1 - """ - mod_locals = cls.root().locals - if "enum" not in mod_locals and all( - base not in mod_locals for base in ENUM_BASE_NAMES - ): - return False - + """Return whether cls is a subclass of an Enum.""" try: return any( klass.name in ENUM_BASE_NAMES diff --git a/tests/unittest_brain.py b/tests/unittest_brain.py index ddc25616..97cec6b1 100644 --- a/tests/unittest_brain.py +++ b/tests/unittest_brain.py @@ -1128,6 +1128,32 @@ class EnumBrainTest(unittest.TestCase): self.assertIsInstance(inferred[0], astroid.Const) self.assertEqual(inferred[0].value, "red") + def test_enum_subclass_different_modules(self): + # See Pylint issue #2626 + astroid.extract_node( + """ + from enum import Enum + + class EnumSubclass(Enum): + pass + """, + "a" + ) + ast_node = astroid.extract_node( + """ + from a import EnumSubclass + + class Color(EnumSubclass): + red = 1 + + Color.red.value #@ + """ + ) + inferred = ast_node.inferred() + self.assertEqual(len(inferred), 1) + self.assertIsInstance(inferred[0], astroid.Const) + self.assertEqual(inferred[0].value, 1) + @unittest.skipUnless(HAS_DATEUTIL, "This test requires the dateutil library.") class DateutilBrainTest(unittest.TestCase): @@ -1645,7 +1671,7 @@ class TypingBrain(unittest.TestCase): @test_utils.require_version(minver="3.7") def test_typing_generic_slots(self): - """Test cache reset for slots if Generic subscript is inferred.""" + """Test slots for Generic subclass.""" node = builder.extract_node( """ from typing import Generic, TypeVar @@ -1657,10 +1683,6 @@ class TypingBrain(unittest.TestCase): """ ) inferred = next(node.infer()) - assert len(inferred.slots()) == 0 - # Only after the subscript base is inferred and the inference tip applied, - # will slots contain the correct value - next(node.bases[0].infer()) slots = inferred.slots() assert len(slots) == 1 assert isinstance(slots[0], nodes.Const) |
