From 181642f33c3ba71f39e73efb334502ad300dfe5a Mon Sep 17 00:00:00 2001 From: Marc Mueller <30130371+cdce8p@users.noreply.github.com> Date: Sun, 28 Feb 2021 21:16:14 +0100 Subject: Solves "Duplicates found in MROs" false positives. (#905, #916) * Adds inference support for all typing types that are defined through _alias function * Instead of creating a new class (by the mean of TYPING_TYPE_TEMPLATE) infer the origin class : i.e MutableSet = _alias(collections.MutableSet ...) origin is the class in collections module. Needs to add __getitem method on its metaclass so that is support indexing (MutableSet[T]). * Enable _alias mocking and testing only if python version is at least 3.7 Co-authored-by: hippo91 --- tests/unittest_brain.py | 115 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 115 insertions(+) (limited to 'tests') diff --git a/tests/unittest_brain.py b/tests/unittest_brain.py index 2a4f7f8c..86cd2e82 100644 --- a/tests/unittest_brain.py +++ b/tests/unittest_brain.py @@ -99,6 +99,11 @@ import astroid import astroid.test_utils as test_utils +def assertEqualMro(klass, expected_mro): + """Check mro names.""" + assert [member.name for member in klass.mro()] == expected_mro + + class HashlibTest(unittest.TestCase): def _assert_hashlib_class(self, class_obj): self.assertIn("update", class_obj) @@ -1206,6 +1211,116 @@ class TypingBrain(unittest.TestCase): assert len(typing_module.locals["TypedDict"]) == 1 assert inferred_base == typing_module.locals["TypedDict"][0] + @test_utils.require_version("3.8") + def test_typing_alias_type(self): + """ + Test that the type aliased thanks to typing._alias function are + correctly inferred. + """ + + def check_metaclass(node: nodes.ClassDef): + meta = node.metaclass() + assert isinstance(meta, nodes.ClassDef) + assert meta.name == "ABCMeta_typing" + assert "ABCMeta" == meta.basenames[0] + assert meta.locals.get("__getitem__") is not None + + abc_meta = next(meta.bases[0].infer()) + assert isinstance(abc_meta, nodes.ClassDef) + assert abc_meta.name == "ABCMeta" + assert abc_meta.locals.get("__getitem__") is None + + node = builder.extract_node( + """ + from typing import TypeVar, MutableSet + + T = TypeVar("T") + MutableSet[T] + + class Derived1(MutableSet[T]): + pass + """ + ) + inferred = next(node.infer()) + check_metaclass(inferred) + assertEqualMro( + inferred, + [ + "Derived1", + "MutableSet_typing", + "MutableSet", + "Set", + "Collection", + "Sized", + "Iterable", + "Container", + "object", + ], + ) + + node = builder.extract_node( + """ + import typing + class Derived2(typing.OrderedDict[int, str]): + pass + """ + ) + inferred = next(node.infer()) + check_metaclass(inferred) + assertEqualMro( + inferred, + [ + "Derived2", + "OrderedDict_typing", + "OrderedDict", + "dict", + "object", + ], + ) + + node = builder.extract_node( + """ + import typing + class Derived3(typing.Pattern[str]): + pass + """ + ) + inferred = next(node.infer()) + check_metaclass(inferred) + assertEqualMro( + inferred, + [ + "Derived3", + "Pattern", + "object", + ], + ) + + @test_utils.require_version("3.8") + def test_typing_alias_side_effects(self): + """Test that typing._alias changes doesn't have unwanted consequences.""" + node = builder.extract_node( + """ + import typing + import collections.abc + + class Derived(collections.abc.Iterator[int]): + pass + """ + ) + inferred = next(node.infer()) + assert inferred.metaclass() is None # Should this be ABCMeta? + assertEqualMro( + inferred, + [ + "Derived", + # Should this be more? + # "Iterator_typing"? + # "Iterator", + # "object", + ], + ) + class ReBrainTest(unittest.TestCase): def test_regex_flags(self): -- cgit v1.2.1