diff options
Diffstat (limited to 'tests')
24 files changed, 230 insertions, 43 deletions
diff --git a/tests/resources.py b/tests/resources.py index 7113f2b1..20bd4113 100644 --- a/tests/resources.py +++ b/tests/resources.py @@ -1,8 +1,9 @@ # Copyright (c) 2014 Google, Inc. -# Copyright (c) 2015-2016, 2018-2019 Claudiu Popa <pcmanticore@gmail.com> +# Copyright (c) 2015-2016, 2018-2020 Claudiu Popa <pcmanticore@gmail.com> # Copyright (c) 2016 Ceridwen <ceridwenv@gmail.com> # Copyright (c) 2018 Nick Drozd <nicholasdrozd@gmail.com> # Copyright (c) 2019 Ashley Whetter <ashley@awhetter.co.uk> +# Copyright (c) 2020 David Cain <davidjosephcain@gmail.com> # Licensed under the LGPL: https://www.gnu.org/licenses/old-licenses/lgpl-2.1.en.html # For details: https://github.com/PyCQA/astroid/blob/master/COPYING.LESSER diff --git a/tests/testdata/python3/data/nonregr.py b/tests/testdata/python3/data/nonregr.py index 78765c85..073135d2 100644 --- a/tests/testdata/python3/data/nonregr.py +++ b/tests/testdata/python3/data/nonregr.py @@ -16,9 +16,7 @@ def toto(value): print(v.get('yo')) -import imp -fp, mpath, desc = imp.find_module('optparse',a) -s_opt = imp.load_module('std_optparse', fp, mpath, desc) +import optparse as s_opt class OptionParser(s_opt.OptionParser): diff --git a/tests/unittest_brain.py b/tests/unittest_brain.py index 0a833664..e6ff6928 100644 --- a/tests/unittest_brain.py +++ b/tests/unittest_brain.py @@ -18,11 +18,12 @@ # Copyright (c) 2018 Anthony Sottile <asottile@umich.edu> # Copyright (c) 2018 Ioana Tagirta <ioana.tagirta@gmail.com> # Copyright (c) 2018 Ahmed Azzaoui <ahmed.azzaoui@engie.com> +# Copyright (c) 2019-2020 Bryce Guinta <bryce.guinta@protonmail.com> # Copyright (c) 2019 Ashley Whetter <ashley@awhetter.co.uk> # Copyright (c) 2019 Tomas Novak <ext.Tomas.Novak@skoda-auto.cz> # Copyright (c) 2019 Hugo van Kemenade <hugovk@users.noreply.github.com> # Copyright (c) 2019 Grygorii Iermolenko <gyermolenko@gmail.com> -# Copyright (c) 2019 Bryce Guinta <bryce.guinta@protonmail.com> +# Copyright (c) 2020 Peter Kolbus <peter.kolbus@gmail.com> # Licensed under the LGPL: https://www.gnu.org/licenses/old-licenses/lgpl-2.1.en.html # For details: https://github.com/PyCQA/astroid/blob/master/COPYING.LESSER @@ -540,8 +541,10 @@ class MultiprocessingBrainTest(unittest.TestCase): obj = next(module[attr].infer()) self.assertEqual(obj.qname(), "{}.{}".format(bases.BUILTINS, attr)) - array = next(module["array"].infer()) - self.assertEqual(array.qname(), "array.array") + # pypy's implementation of array.__spec__ return None. This causes problems for this inference. + if not hasattr(sys, "pypy_version_info"): + array = next(module["array"].infer()) + self.assertEqual(array.qname(), "array.array") manager = next(module["manager"].infer()) # Verify that we have these attributes @@ -839,6 +842,16 @@ class EnumBrainTest(unittest.TestCase): assert inferred_tuple_node.as_string() == "(1, 2)" assert inferred_list_node.as_string() == "[2, 4]" + def test_enum_starred_is_skipped(self): + code = """ + from enum import Enum + class ContentType(Enum): + TEXT, PHOTO, VIDEO, GIF, YOUTUBE, *_ = [1, 2, 3, 4, 5, 6] + ContentType.TEXT #@ + """ + node = astroid.extract_node(code) + next(node.infer()) + @unittest.skipUnless(HAS_DATEUTIL, "This test requires the dateutil library.") class DateutilBrainTest(unittest.TestCase): @@ -924,6 +937,42 @@ class IOBrainTest(unittest.TestCase): self.assertEqual(raw.name, "FileIO") +@test_utils.require_version("3.9") +class TypeBrain(unittest.TestCase): + def test_type_subscript(self): + """ + Check that type object has the __class_getitem__ method + when it is used as a subscript + """ + src = builder.extract_node( + """ + a: type[int] = int + """ + ) + val_inf = src.annotation.value.inferred()[0] + self.assertIsInstance(val_inf, astroid.ClassDef) + self.assertEqual(val_inf.name, "type") + meth_inf = val_inf.getattr("__class_getitem__")[0] + self.assertIsInstance(meth_inf, astroid.FunctionDef) + + def test_invalid_type_subscript(self): + """ + Check that a type (str for example) that inherits + from type does not have __class_getitem__ method even + when it is used as a subscript + """ + src = builder.extract_node( + """ + a: str[int] = "abc" + """ + ) + val_inf = src.annotation.value.inferred()[0] + self.assertIsInstance(val_inf, astroid.ClassDef) + self.assertEqual(val_inf.name, "str") + with self.assertRaises(astroid.exceptions.AttributeInferenceError): + meth_inf = val_inf.getattr("__class_getitem__")[0] + + @test_utils.require_version("3.6") class TypingBrain(unittest.TestCase): def test_namedtuple_base(self): @@ -1279,6 +1328,13 @@ class SubprocessTest(unittest.TestCase): assert isinstance(inferred, astroid.Const) assert isinstance(inferred.value, (str, bytes)) + @test_utils.require_version("3.9") + def test_popen_does_not_have_class_getitem(self): + code = """import subprocess; subprocess.Popen""" + node = astroid.extract_node(code) + inferred = next(node.infer()) + assert "__class_getitem__" in inferred + class TestIsinstanceInference: """Test isinstance builtin inference""" @@ -2020,5 +2076,36 @@ def test_dataclasses(): assert isinstance(name[0], astroid.Unknown) +@pytest.mark.parametrize( + "code,expected_class,expected_value", + [ + ("'hey'.encode()", astroid.Const, b""), + ("b'hey'.decode()", astroid.Const, ""), + ("'hey'.encode().decode()", astroid.Const, ""), + ], +) +def test_str_and_bytes(code, expected_class, expected_value): + node = astroid.extract_node(code) + inferred = next(node.infer()) + assert isinstance(inferred, expected_class) + assert inferred.value == expected_value + + +def test_no_recursionerror_on_self_referential_length_check(): + """ + Regression test for https://github.com/PyCQA/astroid/issues/777 + """ + with pytest.raises(astroid.InferenceError): + node = astroid.extract_node( + """ + class Crash: + def __len__(self) -> int: + return len(self) + len(Crash()) #@ + """ + ) + node.inferred() + + if __name__ == "__main__": unittest.main() diff --git a/tests/unittest_brain_numpy_core_fromnumeric.py b/tests/unittest_brain_numpy_core_fromnumeric.py index fd571f30..73ecccc2 100644 --- a/tests/unittest_brain_numpy_core_fromnumeric.py +++ b/tests/unittest_brain_numpy_core_fromnumeric.py @@ -1,6 +1,7 @@ # -*- encoding=utf-8 -*- # Copyright (c) 2019 Ashley Whetter <ashley@awhetter.co.uk> # Copyright (c) 2019 hippo91 <guillaume.peillex@gmail.com> +# Copyright (c) 2020 Claudiu Popa <pcmanticore@gmail.com> # Licensed under the LGPL: https://www.gnu.org/licenses/old-licenses/lgpl-2.1.en.html # For details: https://github.com/PyCQA/astroid/blob/master/COPYING.LESSER diff --git a/tests/unittest_brain_numpy_core_function_base.py b/tests/unittest_brain_numpy_core_function_base.py index 109238a2..df44b645 100644 --- a/tests/unittest_brain_numpy_core_function_base.py +++ b/tests/unittest_brain_numpy_core_function_base.py @@ -1,6 +1,7 @@ # -*- encoding=utf-8 -*- # Copyright (c) 2019 Ashley Whetter <ashley@awhetter.co.uk> # Copyright (c) 2019 hippo91 <guillaume.peillex@gmail.com> +# Copyright (c) 2020 Claudiu Popa <pcmanticore@gmail.com> # Licensed under the LGPL: https://www.gnu.org/licenses/old-licenses/lgpl-2.1.en.html # For details: https://github.com/PyCQA/astroid/blob/master/COPYING.LESSER diff --git a/tests/unittest_brain_numpy_core_multiarray.py b/tests/unittest_brain_numpy_core_multiarray.py index 9e945d21..cd68db06 100644 --- a/tests/unittest_brain_numpy_core_multiarray.py +++ b/tests/unittest_brain_numpy_core_multiarray.py @@ -1,6 +1,7 @@ # -*- encoding=utf-8 -*- # Copyright (c) 2019 hippo91 <guillaume.peillex@gmail.com> # Copyright (c) 2019 Ashley Whetter <ashley@awhetter.co.uk> +# Copyright (c) 2020 Claudiu Popa <pcmanticore@gmail.com> # Licensed under the LGPL: https://www.gnu.org/licenses/old-licenses/lgpl-2.1.en.html # For details: https://github.com/PyCQA/astroid/blob/master/COPYING.LESSER diff --git a/tests/unittest_brain_numpy_core_numeric.py b/tests/unittest_brain_numpy_core_numeric.py index a39fb19e..47894ca9 100644 --- a/tests/unittest_brain_numpy_core_numeric.py +++ b/tests/unittest_brain_numpy_core_numeric.py @@ -1,6 +1,7 @@ # -*- encoding=utf-8 -*- # Copyright (c) 2019 Ashley Whetter <ashley@awhetter.co.uk> # Copyright (c) 2019 hippo91 <guillaume.peillex@gmail.com> +# Copyright (c) 2020 Claudiu Popa <pcmanticore@gmail.com> # Licensed under the LGPL: https://www.gnu.org/licenses/old-licenses/lgpl-2.1.en.html # For details: https://github.com/PyCQA/astroid/blob/master/COPYING.LESSER diff --git a/tests/unittest_brain_numpy_core_numerictypes.py b/tests/unittest_brain_numpy_core_numerictypes.py index db4fdd23..e05f8eb7 100644 --- a/tests/unittest_brain_numpy_core_numerictypes.py +++ b/tests/unittest_brain_numpy_core_numerictypes.py @@ -1,6 +1,6 @@ # -*- encoding=utf-8 -*- +# Copyright (c) 2017-2018, 2020 Claudiu Popa <pcmanticore@gmail.com> # Copyright (c) 2017-2020 hippo91 <guillaume.peillex@gmail.com> -# Copyright (c) 2017-2018 Claudiu Popa <pcmanticore@gmail.com> # Copyright (c) 2018 Bryce Guinta <bryce.paul.guinta@gmail.com> # Copyright (c) 2019 Ashley Whetter <ashley@awhetter.co.uk> diff --git a/tests/unittest_brain_numpy_core_umath.py b/tests/unittest_brain_numpy_core_umath.py index 3db3f149..acfaeb70 100644 --- a/tests/unittest_brain_numpy_core_umath.py +++ b/tests/unittest_brain_numpy_core_umath.py @@ -1,6 +1,7 @@ # -*- encoding=utf-8 -*- # Copyright (c) 2019 hippo91 <guillaume.peillex@gmail.com> # Copyright (c) 2019 Ashley Whetter <ashley@awhetter.co.uk> +# Copyright (c) 2020 Claudiu Popa <pcmanticore@gmail.com> # Licensed under the LGPL: https://www.gnu.org/licenses/old-licenses/lgpl-2.1.en.html # For details: https://github.com/PyCQA/astroid/blob/master/COPYING.LESSER @@ -36,6 +37,7 @@ class NumpyBrainCoreUmathTest(unittest.TestCase): "conjugate", "cosh", "deg2rad", + "degrees", "exp2", "expm1", "fabs", @@ -50,6 +52,7 @@ class NumpyBrainCoreUmathTest(unittest.TestCase): "negative", "positive", "rad2deg", + "radians", "reciprocal", "rint", "sign", diff --git a/tests/unittest_brain_numpy_ndarray.py b/tests/unittest_brain_numpy_ndarray.py index defce47d..e53ce540 100644 --- a/tests/unittest_brain_numpy_ndarray.py +++ b/tests/unittest_brain_numpy_ndarray.py @@ -1,6 +1,6 @@ # -*- encoding=utf-8 -*- # Copyright (c) 2017-2020 hippo91 <guillaume.peillex@gmail.com> -# Copyright (c) 2017-2018 Claudiu Popa <pcmanticore@gmail.com> +# Copyright (c) 2017-2018, 2020 Claudiu Popa <pcmanticore@gmail.com> # Copyright (c) 2018 Bryce Guinta <bryce.paul.guinta@gmail.com> # Copyright (c) 2019 Ashley Whetter <ashley@awhetter.co.uk> @@ -154,7 +154,7 @@ class NumpyBrainNdarrayTest(unittest.TestCase): Test that some numpy ndarray attributes are inferred as numpy.ndarray """ licit_array_types = ".ndarray" - for attr_ in ("real", "imag"): + for attr_ in ("real", "imag", "shape", "T"): with self.subTest(typ=attr_): inferred_values = list(self._inferred_ndarray_attribute(attr_)) self.assertTrue( diff --git a/tests/unittest_brain_numpy_random_mtrand.py b/tests/unittest_brain_numpy_random_mtrand.py index 20a5d310..ec2bfcf7 100644 --- a/tests/unittest_brain_numpy_random_mtrand.py +++ b/tests/unittest_brain_numpy_random_mtrand.py @@ -1,6 +1,7 @@ # -*- encoding=utf-8 -*- # Copyright (c) 2019 Ashley Whetter <ashley@awhetter.co.uk> # Copyright (c) 2019 hippo91 <guillaume.peillex@gmail.com> +# Copyright (c) 2020 Claudiu Popa <pcmanticore@gmail.com> # Licensed under the LGPL: https://www.gnu.org/licenses/old-licenses/lgpl-2.1.en.html # For details: https://github.com/PyCQA/astroid/blob/master/COPYING.LESSER @@ -55,6 +56,7 @@ class NumpyBrainRandomMtrandTest(unittest.TestCase): "rand": (["args"], []), "randint": (["low", "high", "size", "dtype"], [None, None, "l"]), "randn": (["args"], []), + "random": (["size"], [None]), "random_integers": (["low", "high", "size"], [None, None]), "random_sample": (["size"], [None]), "rayleigh": (["scale", "size"], [1.0, None]), diff --git a/tests/unittest_builder.py b/tests/unittest_builder.py index 22e49e60..8f1205d6 100644 --- a/tests/unittest_builder.py +++ b/tests/unittest_builder.py @@ -1,6 +1,6 @@ # -*- coding: utf-8 -*- # Copyright (c) 2006-2014 LOGILAB S.A. (Paris, FRANCE) <contact@logilab.fr> -# Copyright (c) 2014-2019 Claudiu Popa <pcmanticore@gmail.com> +# Copyright (c) 2014-2020 Claudiu Popa <pcmanticore@gmail.com> # Copyright (c) 2014-2015 Google, Inc. # Copyright (c) 2015-2016 Ceridwen <ceridwenv@gmail.com> # Copyright (c) 2015 Florian Bruhin <me@the-compiler.org> diff --git a/tests/unittest_inference.py b/tests/unittest_inference.py index b3df5f6c..7b80b530 100644 --- a/tests/unittest_inference.py +++ b/tests/unittest_inference.py @@ -14,14 +14,18 @@ # Copyright (c) 2017 Calen Pennington <calen.pennington@gmail.com> # Copyright (c) 2017 David Euresti <david@dropbox.com> # Copyright (c) 2017 Derek Gustafson <degustaf@gmail.com> +# Copyright (c) 2018 Bryce Guinta <bryce.paul.guinta@gmail.com> # Copyright (c) 2018 Daniel Martin <daniel.martin@crowdstrike.com> # Copyright (c) 2018 Ville Skyttä <ville.skytta@iki.fi> -# Copyright (c) 2018 Bryce Guinta <bryce.paul.guinta@gmail.com> # Copyright (c) 2018 Anthony Sottile <asottile@umich.edu> # Copyright (c) 2019 Stanislav Levin <slev@altlinux.org> # Copyright (c) 2019 David Liu <david@cs.toronto.edu> # Copyright (c) 2019 Ashley Whetter <ashley@awhetter.co.uk> # Copyright (c) 2019 Hugo van Kemenade <hugovk@users.noreply.github.com> +# Copyright (c) 2020 Peter Kolbus <peter.kolbus@gmail.com> +# Copyright (c) 2020 hippo91 <guillaume.peillex@gmail.com> +# Copyright (c) 2020 Karthikeyan Singaravelan <tir.karthi@gmail.com> +# Copyright (c) 2020 Bryce Guinta <bryce.guinta@protonmail.com> # Licensed under the LGPL: https://www.gnu.org/licenses/old-licenses/lgpl-2.1.en.html # For details: https://github.com/PyCQA/astroid/blob/master/COPYING.LESSER @@ -428,7 +432,7 @@ class InferenceTest(resources.SysPathSetup, unittest.TestCase): del undefined_attr """ delete = extract_node(code, __name__) - self.assertRaises(InferenceError, delete.infer) + self.assertRaises(InferenceError, next, delete.infer()) def test_del2(self): code = """ @@ -541,18 +545,6 @@ class InferenceTest(resources.SysPathSetup, unittest.TestCase): self.assertEqual(ancestor.root().name, BUILTINS) self.assertRaises(StopIteration, partial(next, ancestors)) - def test_qqch(self): - code = """ - from astroid.modutils import load_module_from_name - xxx = load_module_from_name('__pkginfo__') - """ - ast = parse(code, __name__) - xxx = ast["xxx"] - self.assertSetEqual( - {n.__class__ for n in xxx.inferred()}, - {nodes.Const, util.Uninferable.__class__}, - ) - def test_method_argument(self): code = ''' class ErudiEntitySchema: @@ -2083,8 +2075,6 @@ class InferenceTest(resources.SysPathSetup, unittest.TestCase): def test_str_methods(self): code = """ ' '.decode() #@ - - ' '.encode() #@ ' '.join('abcd') #@ ' '.replace('a', 'b') #@ ' '.format('a') #@ @@ -2106,15 +2096,13 @@ class InferenceTest(resources.SysPathSetup, unittest.TestCase): """ ast = extract_node(code, __name__) self.assertInferConst(ast[0], "") - for i in range(1, 16): + for i in range(1, 15): self.assertInferConst(ast[i], "") - for i in range(16, 19): + for i in range(15, 18): self.assertInferConst(ast[i], 0) def test_unicode_methods(self): code = """ - u' '.encode() #@ - u' '.decode() #@ u' '.join('abcd') #@ u' '.replace('a', 'b') #@ @@ -2137,9 +2125,9 @@ class InferenceTest(resources.SysPathSetup, unittest.TestCase): """ ast = extract_node(code, __name__) self.assertInferConst(ast[0], "") - for i in range(1, 16): + for i in range(1, 15): self.assertInferConst(ast[i], "") - for i in range(16, 19): + for i in range(15, 18): self.assertInferConst(ast[i], 0) def test_scope_lookup_same_attributes(self): @@ -2459,7 +2447,6 @@ class InferenceTest(resources.SysPathSetup, unittest.TestCase): 1 ** (lambda x: x) #@ {} * {} #@ {} - {} #@ - {} | {} #@ {} >> {} #@ [] + () #@ () + [] #@ @@ -2504,7 +2491,6 @@ class InferenceTest(resources.SysPathSetup, unittest.TestCase): msg.format(op="**", lhs="int", rhs="function"), msg.format(op="*", lhs="dict", rhs="dict"), msg.format(op="-", lhs="dict", rhs="dict"), - msg.format(op="|", lhs="dict", rhs="dict"), msg.format(op=">>", lhs="dict", rhs="dict"), msg.format(op="+", lhs="list", rhs="tuple"), msg.format(op="+", lhs="tuple", rhs="list"), @@ -2519,6 +2505,12 @@ class InferenceTest(resources.SysPathSetup, unittest.TestCase): msg.format(op="+=", lhs="int", rhs="A"), msg.format(op="+=", lhs="int", rhs="list"), ] + + # PEP-584 supports | for dictionary union + if sys.version_info < (3, 9): + ast_nodes.append(extract_node("{} | {} #@")) + expected.append(msg.format(op="|", lhs="dict", rhs="dict")) + for node, expected_value in zip(ast_nodes, expected): errors = node.type_errors() self.assertEqual(len(errors), 1) @@ -5668,6 +5660,10 @@ def test_custom_decorators_for_classmethod_and_staticmethods(code, obj, obj_type @pytest.mark.skipif(sys.version_info < (3, 8), reason="Needs dataclasses available") +@pytest.mark.skipif( + sys.version_info >= (3, 9), + reason="Exact inference with dataclasses (replace function) in python3.9", +) def test_dataclasses_subscript_inference_recursion_error(): code = """ from dataclasses import dataclass, replace @@ -5688,6 +5684,31 @@ def test_dataclasses_subscript_inference_recursion_error(): assert helpers.safe_infer(node) is None +@pytest.mark.skipif( + sys.version_info < (3, 9), + reason="Exact inference with dataclasses (replace function) in python3.9", +) +def test_dataclasses_subscript_inference_recursion_error_39(): + code = """ + from dataclasses import dataclass, replace + + @dataclass + class ProxyConfig: + auth: str = "/auth" + + + a = ProxyConfig("") + test_dict = {"proxy" : {"auth" : "", "bla" : "f"}} + + foo = test_dict['proxy'] + replace(a, **test_dict['proxy']) # This fails + """ + node = extract_node(code) + infer_val = helpers.safe_infer(node) + assert isinstance(infer_val, Instance) + assert infer_val.pytype() == ".ProxyConfig" + + def test_self_reference_infer_does_not_trigger_recursion_error(): # Prevents https://github.com/PyCQA/pylint/issues/1285 code = """ @@ -5864,5 +5885,19 @@ def test_infer_generated_setter(): assert list(inferred.nodes_of_class(nodes.Const)) == [] +def test_infer_list_of_uninferables_does_not_crash(): + code = """ + x = [A] * 1 + f = [x, [A] * 2] + x = list(f) + [] # List[Uninferable] + tuple(x[0]) + """ + node = extract_node(code) + inferred = next(node.infer()) + assert isinstance(inferred, nodes.Tuple) + # Would not be able to infer the first element. + assert not inferred.elts + + if __name__ == "__main__": unittest.main() diff --git a/tests/unittest_lookup.py b/tests/unittest_lookup.py index 84fd5433..bf30b796 100644 --- a/tests/unittest_lookup.py +++ b/tests/unittest_lookup.py @@ -1,6 +1,6 @@ # Copyright (c) 2007-2013 LOGILAB S.A. (Paris, FRANCE) <contact@logilab.fr> # Copyright (c) 2010 Daniel Harding <dharding@gmail.com> -# Copyright (c) 2014-2016, 2018-2019 Claudiu Popa <pcmanticore@gmail.com> +# Copyright (c) 2014-2016, 2018-2020 Claudiu Popa <pcmanticore@gmail.com> # Copyright (c) 2014 Google, Inc. # Copyright (c) 2015-2016 Ceridwen <ceridwenv@gmail.com> # Copyright (c) 2019 Ashley Whetter <ashley@awhetter.co.uk> diff --git a/tests/unittest_manager.py b/tests/unittest_manager.py index d7878b59..91a781c6 100644 --- a/tests/unittest_manager.py +++ b/tests/unittest_manager.py @@ -1,7 +1,7 @@ # -*- coding: utf-8 -*- # Copyright (c) 2006, 2009-2014 LOGILAB S.A. (Paris, FRANCE) <contact@logilab.fr> # Copyright (c) 2013 AndroWiiid <androwiiid@gmail.com> -# Copyright (c) 2014-2019 Claudiu Popa <pcmanticore@gmail.com> +# Copyright (c) 2014-2020 Claudiu Popa <pcmanticore@gmail.com> # Copyright (c) 2014 Google, Inc. # Copyright (c) 2015-2016 Ceridwen <ceridwenv@gmail.com> # Copyright (c) 2017 Chris Philip <chrisp533@gmail.com> diff --git a/tests/unittest_modutils.py b/tests/unittest_modutils.py index b5c41bf0..9b3cecf4 100644 --- a/tests/unittest_modutils.py +++ b/tests/unittest_modutils.py @@ -10,6 +10,7 @@ # Copyright (c) 2019 Ashley Whetter <ashley@awhetter.co.uk> # Copyright (c) 2019 Hugo van Kemenade <hugovk@users.noreply.github.com> # Copyright (c) 2019 markmcclain <markmcclain@users.noreply.github.com> +# Copyright (c) 2020 Peter Kolbus <peter.kolbus@gmail.com> # Licensed under the LGPL: https://www.gnu.org/licenses/old-licenses/lgpl-2.1.en.html # For details: https://github.com/PyCQA/astroid/blob/master/COPYING.LESSER @@ -82,7 +83,7 @@ class LoadModuleFromNameTest(unittest.TestCase): def test_raise_load_module_from_name_1(self): self.assertRaises( - ImportError, modutils.load_module_from_name, "os.path", use_sys=0 + ImportError, modutils.load_module_from_name, "_this_module_does_not_exist_" ) @@ -297,6 +298,23 @@ class IsRelativeTest(unittest.TestCase): def test_knownValues_is_relative_3(self): self.assertFalse(modutils.is_relative("astroid", astroid.__path__[0])) + def test_deep_relative(self): + self.assertTrue(modutils.is_relative("ElementTree", xml.etree.__path__[0])) + + def test_deep_relative2(self): + self.assertFalse(modutils.is_relative("ElementTree", xml.__path__[0])) + + def test_deep_relative3(self): + self.assertTrue(modutils.is_relative("etree.ElementTree", xml.__path__[0])) + + def test_deep_relative4(self): + self.assertTrue(modutils.is_relative("etree.gibberish", xml.__path__[0])) + + def test_is_relative_bad_path(self): + self.assertFalse( + modutils.is_relative("ElementTree", os.path.join(xml.__path__[0], "ftree")) + ) + class GetModuleFilesTest(unittest.TestCase): def test_get_module_files_1(self): diff --git a/tests/unittest_nodes.py b/tests/unittest_nodes.py index 5b6a39e3..d138ee17 100644 --- a/tests/unittest_nodes.py +++ b/tests/unittest_nodes.py @@ -1360,5 +1360,16 @@ def test_is_generator_for_yield_in_if(): assert bool(node.is_generator()) +def test_is_generator_for_yield_in_aug_assign(): + code = """ + def test(): + buf = '' + while True: + buf += yield + """ + node = astroid.extract_node(code) + assert bool(node.is_generator()) + + if __name__ == "__main__": unittest.main() diff --git a/tests/unittest_protocols.py b/tests/unittest_protocols.py index babff51e..4f9bfbfc 100644 --- a/tests/unittest_protocols.py +++ b/tests/unittest_protocols.py @@ -1,5 +1,5 @@ # -*- coding: utf-8 -*- -# Copyright (c) 2015-2019 Claudiu Popa <pcmanticore@gmail.com> +# Copyright (c) 2015-2020 Claudiu Popa <pcmanticore@gmail.com> # Copyright (c) 2015-2016 Ceridwen <ceridwenv@gmail.com> # Copyright (c) 2016 Jakub Wilk <jwilk@jwilk.net> # Copyright (c) 2017 Łukasz Rogalski <rogalski.91@gmail.com> diff --git a/tests/unittest_python3.py b/tests/unittest_python3.py index b1759f03..db2d233a 100644 --- a/tests/unittest_python3.py +++ b/tests/unittest_python3.py @@ -1,7 +1,7 @@ # -*- coding: utf-8 -*- # Copyright (c) 2010, 2013-2014 LOGILAB S.A. (Paris, FRANCE) <contact@logilab.fr> # Copyright (c) 2012 FELD Boris <lothiraldan@gmail.com> -# Copyright (c) 2013-2018 Claudiu Popa <pcmanticore@gmail.com> +# Copyright (c) 2013-2018, 2020 Claudiu Popa <pcmanticore@gmail.com> # Copyright (c) 2014 Google, Inc. # Copyright (c) 2015-2016 Ceridwen <ceridwenv@gmail.com> # Copyright (c) 2016 Jared Garst <jgarst@users.noreply.github.com> diff --git a/tests/unittest_raw_building.py b/tests/unittest_raw_building.py index f782cdb2..160c88d7 100644 --- a/tests/unittest_raw_building.py +++ b/tests/unittest_raw_building.py @@ -1,5 +1,5 @@ # Copyright (c) 2013 AndroWiiid <androwiiid@gmail.com> -# Copyright (c) 2014-2016, 2018-2019 Claudiu Popa <pcmanticore@gmail.com> +# Copyright (c) 2014-2016, 2018-2020 Claudiu Popa <pcmanticore@gmail.com> # Copyright (c) 2014 Google, Inc. # Copyright (c) 2015-2016 Ceridwen <ceridwenv@gmail.com> # Copyright (c) 2018 Anthony Sottile <asottile@umich.edu> diff --git a/tests/unittest_regrtest.py b/tests/unittest_regrtest.py index 79a86cb2..1444da30 100644 --- a/tests/unittest_regrtest.py +++ b/tests/unittest_regrtest.py @@ -343,5 +343,16 @@ def test_ancestor_looking_up_redefined_function(): assert isinstance(found[0], nodes.FunctionDef) +def test_crash_in_dunder_inference_prevented(): + code = """ + class MyClass(): + def fu(self, objects): + delitem = dict.__delitem__.__get__(self, dict) + delitem #@ + """ + inferred = next(extract_node(code).infer()) + assert "builtins.dict.__delitem__" == inferred.qname() + + if __name__ == "__main__": unittest.main() diff --git a/tests/unittest_scoped_nodes.py b/tests/unittest_scoped_nodes.py index c4597fa6..b28605e6 100644 --- a/tests/unittest_scoped_nodes.py +++ b/tests/unittest_scoped_nodes.py @@ -19,6 +19,7 @@ # Copyright (c) 2019 Hugo van Kemenade <hugovk@users.noreply.github.com> # Copyright (c) 2019 Peter de Blanc <peter@standard.ai> # Copyright (c) 2019 hippo91 <guillaume.peillex@gmail.com> +# Copyright (c) 2020 Tim Martin <tim@asymptotic.co.uk> # Licensed under the LGPL: https://www.gnu.org/licenses/old-licenses/lgpl-2.1.en.html # For details: https://github.com/PyCQA/astroid/blob/master/COPYING.LESSER @@ -1417,6 +1418,22 @@ class ClassNodeTest(ModuleLoader, unittest.TestCase): ], ) + def test_mro_with_attribute_classes(self): + cls = builder.extract_node( + """ + class A: + pass + class B: + pass + scope = object() + scope.A = A + scope.B = B + class C(scope.A, scope.B): + pass + """ + ) + self.assertEqualMro(cls, ["C", "A", "B", "object"]) + def test_generator_from_infer_call_result_parent(self): func = builder.extract_node( """ diff --git a/tests/unittest_transforms.py b/tests/unittest_transforms.py index 922f10f9..13d220a1 100644 --- a/tests/unittest_transforms.py +++ b/tests/unittest_transforms.py @@ -1,4 +1,4 @@ -# Copyright (c) 2015-2018 Claudiu Popa <pcmanticore@gmail.com> +# Copyright (c) 2015-2018, 2020 Claudiu Popa <pcmanticore@gmail.com> # Copyright (c) 2015-2016 Ceridwen <ceridwenv@gmail.com> # Copyright (c) 2016 Jakub Wilk <jwilk@jwilk.net> # Copyright (c) 2018 Bryce Guinta <bryce.paul.guinta@gmail.com> diff --git a/tests/unittest_utils.py b/tests/unittest_utils.py index 428026b1..1cc9afb6 100644 --- a/tests/unittest_utils.py +++ b/tests/unittest_utils.py @@ -1,6 +1,6 @@ # Copyright (c) 2008-2010, 2013 LOGILAB S.A. (Paris, FRANCE) <contact@logilab.fr> # Copyright (c) 2014 Google, Inc. -# Copyright (c) 2015-2016, 2018 Claudiu Popa <pcmanticore@gmail.com> +# Copyright (c) 2015-2016, 2018, 2020 Claudiu Popa <pcmanticore@gmail.com> # Copyright (c) 2016 Ceridwen <ceridwenv@gmail.com> # Copyright (c) 2016 Dave Baum <dbaum@google.com> # Copyright (c) 2019 Ashley Whetter <ashley@awhetter.co.uk> |