summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorMark Byrne <31762852+mbyrnepr2@users.noreply.github.com>2023-01-15 18:26:47 +0100
committerGitHub <noreply@github.com>2023-01-15 18:26:47 +0100
commitc267397eda848544bcbea04e889815ac4faa6ba8 (patch)
treec1508f72cee5d2d92b58beb57f55811fb734c3ee /tests
parent8581d9d26ae76ea3d5719bdd9a0e103bf79e1528 (diff)
downloadastroid-git-c267397eda848544bcbea04e889815ac4faa6ba8.tar.gz
Fix a false positive with user-defined `Enum` class (#1967)
Co-authored-by: Daniƫl van Noord <13665637+DanielNoord@users.noreply.github.com>
Diffstat (limited to 'tests')
-rw-r--r--tests/unittest_brain.py44
1 files changed, 44 insertions, 0 deletions
diff --git a/tests/unittest_brain.py b/tests/unittest_brain.py
index 11dd1139..9ee0f98f 100644
--- a/tests/unittest_brain.py
+++ b/tests/unittest_brain.py
@@ -1230,6 +1230,50 @@ class EnumBrainTest(unittest.TestCase):
assert isinstance(inferred, bases.Instance)
assert inferred._proxied.name == "ENUM_KEY"
+ def test_class_named_enum(self) -> None:
+ """Test that the user-defined class named `Enum` is not inferred as `enum.Enum`"""
+ astroid.extract_node(
+ """
+ class Enum:
+ def __init__(self, one, two):
+ self.one = one
+ self.two = two
+ def pear(self):
+ ...
+ """,
+ "module_with_class_named_enum",
+ )
+
+ attribute_nodes = astroid.extract_node(
+ """
+ import module_with_class_named_enum
+ module_with_class_named_enum.Enum("apple", "orange") #@
+ typo_module_with_class_named_enum.Enum("apple", "orange") #@
+ """
+ )
+
+ name_nodes = astroid.extract_node(
+ """
+ from module_with_class_named_enum import Enum
+ Enum("apple", "orange") #@
+ TypoEnum("apple", "orange") #@
+ """
+ )
+
+ # Test that both of the successfully inferred `Name` & `Attribute`
+ # nodes refer to the user-defined Enum class.
+ for inferred in (attribute_nodes[0].inferred()[0], name_nodes[0].inferred()[0]):
+ assert isinstance(inferred, astroid.Instance)
+ assert inferred.name == "Enum"
+ assert inferred.qname() == "module_with_class_named_enum.Enum"
+ assert "pear" in inferred.locals
+
+ # Test that an `InferenceError` is raised when an attempt is made to
+ # infer a `Name` or `Attribute` node & they cannot be found.
+ for node in (attribute_nodes[1], name_nodes[1]):
+ with pytest.raises(InferenceError):
+ node.inferred()
+
@unittest.skipUnless(HAS_DATEUTIL, "This test requires the dateutil library.")
class DateutilBrainTest(unittest.TestCase):