|
43 | 43 | import pytest |
44 | 44 |
|
45 | 45 | import astroid |
46 | | -from astroid import MANAGER, bases, builder, nodes, test_utils, util |
| 46 | +from astroid import MANAGER, bases, builder, nodes, objects, test_utils, util |
47 | 47 |
|
48 | 48 | try: |
49 | 49 | import multiprocessing # pylint: disable=unused-import |
@@ -993,6 +993,61 @@ class ContentType(Enum): |
993 | 993 | node = astroid.extract_node(code) |
994 | 994 | next(node.infer()) |
995 | 995 |
|
| 996 | + def test_enum_name_is_str_on_self(self): |
| 997 | + code = """ |
| 998 | + from enum import Enum |
| 999 | + class TestEnum(Enum): |
| 1000 | + def func(self): |
| 1001 | + self.name #@ |
| 1002 | + self.value #@ |
| 1003 | + TestEnum.name #@ |
| 1004 | + TestEnum.value #@ |
| 1005 | + """ |
| 1006 | + i_name, i_value, c_name, c_value = astroid.extract_node(code) |
| 1007 | + |
| 1008 | + # <instance>.name should be a string, <class>.name should be a property (that |
| 1009 | + # forwards the lookup to __getattr__) |
| 1010 | + inferred = next(i_name.infer()) |
| 1011 | + assert isinstance(inferred, nodes.Const) |
| 1012 | + assert inferred.pytype() == "builtins.str" |
| 1013 | + inferred = next(c_name.infer()) |
| 1014 | + assert isinstance(inferred, objects.Property) |
| 1015 | + |
| 1016 | + # Inferring .value should not raise InferenceError. It is probably Uninferable |
| 1017 | + # but we don't particularly care |
| 1018 | + next(i_value.infer()) |
| 1019 | + inferred = next(c_value.infer()) |
| 1020 | + assert isinstance(inferred, objects.Property) |
| 1021 | + |
| 1022 | + def test_enum_name_and_value_members_override_dynamicclassattr(self): |
| 1023 | + code = """ |
| 1024 | + from enum import Enum |
| 1025 | + class TrickyEnum(Enum): |
| 1026 | + name = 1 |
| 1027 | + value = 2 |
| 1028 | +
|
| 1029 | + def func(self): |
| 1030 | + self.name #@ |
| 1031 | + self.value #@ |
| 1032 | + TrickyEnum.name #@ |
| 1033 | + TrickyEnum.value #@ |
| 1034 | + """ |
| 1035 | + i_name, i_value, c_name, c_value = astroid.extract_node(code) |
| 1036 | + |
| 1037 | + # All of these cases should be inferred as enum members |
| 1038 | + inferred = next(i_name.infer()) |
| 1039 | + assert isinstance(inferred, bases.Instance) |
| 1040 | + assert inferred.pytype() == ".TrickyEnum.name" |
| 1041 | + inferred = next(c_name.infer()) |
| 1042 | + assert isinstance(inferred, bases.Instance) |
| 1043 | + assert inferred.pytype() == ".TrickyEnum.name" |
| 1044 | + inferred = next(i_value.infer()) |
| 1045 | + assert isinstance(inferred, bases.Instance) |
| 1046 | + assert inferred.pytype() == ".TrickyEnum.value" |
| 1047 | + inferred = next(c_value.infer()) |
| 1048 | + assert isinstance(inferred, bases.Instance) |
| 1049 | + assert inferred.pytype() == ".TrickyEnum.value" |
| 1050 | + |
996 | 1051 |
|
997 | 1052 | @unittest.skipUnless(HAS_DATEUTIL, "This test requires the dateutil library.") |
998 | 1053 | class DateutilBrainTest(unittest.TestCase): |
|
0 commit comments