diff options
| author | Claudiu Popa <pcmanticore@gmail.com> | 2016-06-29 10:04:26 +0100 |
|---|---|---|
| committer | Claudiu Popa <pcmanticore@gmail.com> | 2016-06-29 10:04:26 +0100 |
| commit | 76cfc8e8a02a6ef7e6bc61624d3b24503ee3a235 (patch) | |
| tree | 741df3f0b807b758b7ea156351bece7cd9f49f0b | |
| parent | 02f1250178c22f4f4b249e4e5377651de407c522 (diff) | |
| download | astroid-git-76cfc8e8a02a6ef7e6bc61624d3b24503ee3a235.tar.gz | |
New function, astroid.extract_node, exported out from astroid.test_utils.
| -rw-r--r-- | ChangeLog | 2 | ||||
| -rw-r--r-- | astroid/__init__.py | 2 | ||||
| -rw-r--r-- | astroid/brain/brain_functools.py | 5 | ||||
| -rw-r--r-- | astroid/builder.py | 176 | ||||
| -rw-r--r-- | astroid/test_utils.py | 177 | ||||
| -rw-r--r-- | astroid/tests/unittest_brain.py | 31 | ||||
| -rw-r--r-- | astroid/tests/unittest_builder.py | 6 | ||||
| -rw-r--r-- | astroid/tests/unittest_helpers.py | 27 | ||||
| -rw-r--r-- | astroid/tests/unittest_inference.py | 296 | ||||
| -rw-r--r-- | astroid/tests/unittest_lookup.py | 5 | ||||
| -rw-r--r-- | astroid/tests/unittest_nodes.py | 56 | ||||
| -rw-r--r-- | astroid/tests/unittest_object_model.py | 54 | ||||
| -rw-r--r-- | astroid/tests/unittest_objects.py | 49 | ||||
| -rw-r--r-- | astroid/tests/unittest_protocols.py | 3 | ||||
| -rw-r--r-- | astroid/tests/unittest_python3.py | 4 | ||||
| -rw-r--r-- | astroid/tests/unittest_raw_building.py | 4 | ||||
| -rw-r--r-- | astroid/tests/unittest_regrtest.py | 4 | ||||
| -rw-r--r-- | astroid/tests/unittest_scoped_nodes.py | 62 | ||||
| -rw-r--r-- | astroid/tests/unittest_utils.py | 5 |
19 files changed, 484 insertions, 484 deletions
@@ -3,6 +3,8 @@ Change log for the astroid package (used to be astng) -- + * New function, astroid.extract_node, exported out from astroid.test_utils. + * Fix a crash which occurred when the class of a namedtuple could not be inferred. * Add support for implicit namespace packages (PEP 420) diff --git a/astroid/__init__.py b/astroid/__init__.py index 72f2c78a..52a9f247 100644 --- a/astroid/__init__.py +++ b/astroid/__init__.py @@ -65,7 +65,7 @@ from astroid import raw_building _builtins = raw_building.ast_from_builtins() from astroid.interpreter.util import are_exclusive, unpack_infer from astroid.interpreter.lookup import builtin_lookup -from astroid.builder import parse +from astroid.builder import parse, extract_node from astroid.util import Uninferable, YES diff --git a/astroid/brain/brain_functools.py b/astroid/brain/brain_functools.py index b80ec1ca..a96fc065 100644 --- a/astroid/brain/brain_functools.py +++ b/astroid/brain/brain_functools.py @@ -7,7 +7,6 @@ import astroid from astroid.interpreter import util as interpreter_util from astroid.interpreter import objects from astroid.interpreter import objectmodel -from astroid.test_utils import extract_node from astroid import MANAGER @@ -27,7 +26,7 @@ class LruWrappedModel(objectmodel.FunctionModel): @property def pycache_info(self): - cache_info = extract_node(''' + cache_info = astroid.extract_node(''' from functools import _CacheInfo _CacheInfo(0, 0, 0, 0) ''') @@ -39,7 +38,7 @@ class LruWrappedModel(objectmodel.FunctionModel): @property def pycache_clear(self): - node = extract_node('''def cache_clear(): pass''') + node = astroid.extract_node('''def cache_clear(): pass''') return objects.BoundMethod(proxy=node, bound=self._instance.parent.scope()) diff --git a/astroid/builder.py b/astroid/builder.py index bd941c53..b3d8b524 100644 --- a/astroid/builder.py +++ b/astroid/builder.py @@ -242,3 +242,179 @@ def parse(code, module_name='', path=None, apply_transforms=True): builder = AstroidBuilder(manager=MANAGER, apply_transforms=apply_transforms) return builder.string_build(code, modname=module_name, path=path) + + +# The name of the transient function that is used to +# wrap expressions to be extracted when calling +# extract_node. +_TRANSIENT_FUNCTION = '__' + +# The comment used to select a statement to be extracted +# when calling extract_node. +_STATEMENT_SELECTOR = '#@' + +def _extract_expressions(node): + """Find expressions in a call to _TRANSIENT_FUNCTION and extract them. + + The function walks the AST recursively to search for expressions that + are wrapped into a call to _TRANSIENT_FUNCTION. If it finds such an + expression, it completely removes the function call node from the tree, + replacing it by the wrapped expression inside the parent. + + :param node: An astroid node. + :type node: astroid.bases.NodeNG + :yields: The sequence of wrapped expressions on the modified tree + expression can be found. + """ + if (isinstance(node, treeabc.Call) + and isinstance(node.func, treeabc.Name) + and node.func.name == _TRANSIENT_FUNCTION): + real_expr = node.args[0] + real_expr.parent = node.parent + # Search for node in all _astng_fields (the fields checked when + # get_children is called) of its parent. Some of those fields may + # be lists or tuples, in which case the elements need to be checked. + # When we find it, replace it by real_expr, so that the AST looks + # like no call to _TRANSIENT_FUNCTION ever took place. + for name in node.parent._astroid_fields: + child = getattr(node.parent, name) + if isinstance(child, (list, tuple)): + for idx, compound_child in enumerate(child): + + # Can't find a cleaner way to do this. + if isinstance(compound_child, treeabc.Parameter): + if compound_child.default is node: + child[idx].default = real_expr + elif compound_child.annotation is node: + child[idx].annotation = real_expr + else: + child[idx] = real_expr + elif compound_child is node: + child[idx] = real_expr + + elif child is node: + setattr(node.parent, name, real_expr) + yield real_expr + else: + for child in node.get_children(): + for result in _extract_expressions(child): + yield result + + +def _find_statement_by_line(node, line): + """Extracts the statement on a specific line from an AST. + + If the line number of node matches line, it will be returned; + otherwise its children are iterated and the function is called + recursively. + + :param node: An astroid node. + :type node: astroid.bases.NodeNG + :param line: The line number of the statement to extract. + :type line: int + :returns: The statement on the line, or None if no statement for the line + can be found. + :rtype: astroid.bases.NodeNG or None + """ + if isinstance(node, (treeabc.ClassDef, treeabc.FunctionDef)): + # This is an inaccuracy in the AST: the nodes that can be + # decorated do not carry explicit information on which line + # the actual definition (class/def), but .fromline seems to + # be close enough. + node_line = node.fromlineno + else: + node_line = node.lineno + + if node_line == line: + return node + + for child in node.get_children(): + result = _find_statement_by_line(child, line) + if result: + return result + + return None + + +def extract_node(code, module_name=''): + """Parses some Python code as a module and extracts a designated AST node. + + Statements: + To extract one or more statement nodes, append #@ to the end of the line + + Examples: + >>> def x(): + >>> def y(): + >>> return 1 #@ + + The return statement will be extracted. + + >>> class X(object): + >>> def meth(self): #@ + >>> pass + + The funcion object 'meth' will be extracted. + + Expressions: + To extract arbitrary expressions, surround them with the fake + function call __(...). After parsing, the surrounded expression + will be returned and the whole AST (accessible via the returned + node's parent attribute) will look like the function call was + never there in the first place. + + Examples: + >>> a = __(1) + + The const node will be extracted. + + >>> def x(d=__(foo.bar)): pass + + The node containing the default argument will be extracted. + + >>> def foo(a, b): + >>> return 0 < __(len(a)) < b + + The node containing the function call 'len' will be extracted. + + If no statements or expressions are selected, the last toplevel + statement will be returned. + + If the selected statement is a discard statement, (i.e. an expression + turned into a statement), the wrapped expression is returned instead. + + For convenience, singleton lists are unpacked. + + :param str code: A piece of Python code that is parsed as + a module. Will be passed through textwrap.dedent first. + :param str module_name: The name of the module. + :returns: The designated node from the parse tree, or a list of nodes. + :rtype: astroid.bases.NodeNG, or a list of nodes. + """ + def _extract(node): + if isinstance(node, treeabc.Expr): + return node.value + else: + return node + + requested_lines = [] + for idx, line in enumerate(code.splitlines()): + if line.strip().endswith(_STATEMENT_SELECTOR): + requested_lines.append(idx + 1) + + tree = parse(code, module_name=module_name) + extracted = [] + if requested_lines: + for line in requested_lines: + extracted.append(_find_statement_by_line(tree, line)) + + # Modifies the tree. + extracted.extend(_extract_expressions(tree)) + + if not extracted: + extracted.append(tree.body[-1]) + + extracted = [_extract(node) for node in extracted] + if len(extracted) == 1: + return extracted[0] + else: + return extracted diff --git a/astroid/test_utils.py b/astroid/test_utils.py index 8dd5bf4e..89ed7f30 100644 --- a/astroid/test_utils.py +++ b/astroid/test_utils.py @@ -7,188 +7,11 @@ import functools import sys import warnings -from astroid import builder from astroid import raw_building from astroid import nodes from astroid import util -# The name of the transient function that is used to -# wrap expressions to be extracted when calling -# extract_node. -_TRANSIENT_FUNCTION = '__' - -# The comment used to select a statement to be extracted -# when calling extract_node. -_STATEMENT_SELECTOR = '#@' - - -def _extract_expressions(node): - """Find expressions in a call to _TRANSIENT_FUNCTION and extract them. - - The function walks the AST recursively to search for expressions that - are wrapped into a call to _TRANSIENT_FUNCTION. If it finds such an - expression, it completely removes the function call node from the tree, - replacing it by the wrapped expression inside the parent. - - :param node: An astroid node. - :type node: astroid.bases.NodeNG - :yields: The sequence of wrapped expressions on the modified tree - expression can be found. - """ - if (isinstance(node, nodes.Call) - and isinstance(node.func, nodes.Name) - and node.func.name == _TRANSIENT_FUNCTION): - real_expr = node.args[0] - real_expr.parent = node.parent - # Search for node in all _astng_fields (the fields checked when - # get_children is called) of its parent. Some of those fields may - # be lists or tuples, in which case the elements need to be checked. - # When we find it, replace it by real_expr, so that the AST looks - # like no call to _TRANSIENT_FUNCTION ever took place. - for name in node.parent._astroid_fields: - child = getattr(node.parent, name) - if isinstance(child, (list, tuple)): - for idx, compound_child in enumerate(child): - - # Can't find a cleaner way to do this. - if isinstance(compound_child, nodes.Parameter): - if compound_child.default is node: - child[idx].default = real_expr - elif compound_child.annotation is node: - child[idx].annotation = real_expr - else: - child[idx] = real_expr - elif compound_child is node: - child[idx] = real_expr - - elif child is node: - setattr(node.parent, name, real_expr) - yield real_expr - else: - for child in node.get_children(): - for result in _extract_expressions(child): - yield result - - -def _find_statement_by_line(node, line): - """Extracts the statement on a specific line from an AST. - - If the line number of node matches line, it will be returned; - otherwise its children are iterated and the function is called - recursively. - - :param node: An astroid node. - :type node: astroid.bases.NodeNG - :param line: The line number of the statement to extract. - :type line: int - :returns: The statement on the line, or None if no statement for the line - can be found. - :rtype: astroid.bases.NodeNG or None - """ - if isinstance(node, (nodes.ClassDef, nodes.FunctionDef)): - # This is an inaccuracy in the AST: the nodes that can be - # decorated do not carry explicit information on which line - # the actual definition (class/def), but .fromline seems to - # be close enough. - node_line = node.fromlineno - else: - node_line = node.lineno - - if node_line == line: - return node - - for child in node.get_children(): - result = _find_statement_by_line(child, line) - if result: - return result - - return None - -def extract_node(code, module_name=''): - """Parses some Python code as a module and extracts a designated AST node. - - Statements: - To extract one or more statement nodes, append #@ to the end of the line - - Examples: - >>> def x(): - >>> def y(): - >>> return 1 #@ - - The return statement will be extracted. - - >>> class X(object): - >>> def meth(self): #@ - >>> pass - - The funcion object 'meth' will be extracted. - - Expressions: - To extract arbitrary expressions, surround them with the fake - function call __(...). After parsing, the surrounded expression - will be returned and the whole AST (accessible via the returned - node's parent attribute) will look like the function call was - never there in the first place. - - Examples: - >>> a = __(1) - - The const node will be extracted. - - >>> def x(d=__(foo.bar)): pass - - The node containing the default argument will be extracted. - - >>> def foo(a, b): - >>> return 0 < __(len(a)) < b - - The node containing the function call 'len' will be extracted. - - If no statements or expressions are selected, the last toplevel - statement will be returned. - - If the selected statement is a discard statement, (i.e. an expression - turned into a statement), the wrapped expression is returned instead. - - For convenience, singleton lists are unpacked. - - :param str code: A piece of Python code that is parsed as - a module. Will be passed through textwrap.dedent first. - :param str module_name: The name of the module. - :returns: The designated node from the parse tree, or a list of nodes. - :rtype: astroid.bases.NodeNG, or a list of nodes. - """ - def _extract(node): - if isinstance(node, nodes.Expr): - return node.value - else: - return node - - requested_lines = [] - for idx, line in enumerate(code.splitlines()): - if line.strip().endswith(_STATEMENT_SELECTOR): - requested_lines.append(idx + 1) - - tree = builder.parse(code, module_name=module_name) - extracted = [] - if requested_lines: - for line in requested_lines: - extracted.append(_find_statement_by_line(tree, line)) - - # Modifies the tree. - extracted.extend(_extract_expressions(tree)) - - if not extracted: - extracted.append(tree.body[-1]) - - extracted = [_extract(node) for node in extracted] - if len(extracted) == 1: - return extracted[0] - else: - return extracted - - def require_version(minver=None, maxver=None): """ Compare version of python interpreter to the given one. Skip the test if older. diff --git a/astroid/tests/unittest_brain.py b/astroid/tests/unittest_brain.py index 766dc56e..b1642bdf 100644 --- a/astroid/tests/unittest_brain.py +++ b/astroid/tests/unittest_brain.py @@ -12,7 +12,6 @@ from astroid import builder from astroid import nodes from astroid.interpreter import objects from astroid.interpreter import util as interpreterutil -from astroid import test_utils from astroid import util import astroid @@ -82,7 +81,7 @@ class HashlibTest(unittest.TestCase): class NamedTupleTest(unittest.TestCase): def test_namedtuple_base(self): - klass = test_utils.extract_node(""" + klass = astroid.extract_node(""" from collections import namedtuple class X(namedtuple("X", ["a", "b", "c"])): @@ -95,7 +94,7 @@ class NamedTupleTest(unittest.TestCase): self.assertFalse(anc.parent is None) def test_namedtuple_inference(self): - klass = test_utils.extract_node(""" + klass = astroid.extract_node(""" from collections import namedtuple name = "X" @@ -110,7 +109,7 @@ class NamedTupleTest(unittest.TestCase): set(base.instantiate_class().instance_attrs)) def test_namedtuple_inference_failure(self): - klass = test_utils.extract_node(""" + klass = astroid.extract_node(""" from collections import namedtuple def foo(fields): @@ -121,7 +120,7 @@ class NamedTupleTest(unittest.TestCase): def test_namedtuple_advanced_inference(self): # urlparse return an object of class ParseResult, which has a # namedtuple call and a mixin as base classes - result = test_utils.extract_node(""" + result = astroid.extract_node(""" import six result = __(six.moves.urllib.parse.urlparse('gopher://')) @@ -135,7 +134,7 @@ class NamedTupleTest(unittest.TestCase): self.assertEqual(instance.name, 'ParseResult') def test_namedtuple_instance_attrs(self): - result = test_utils.extract_node(''' + result = astroid.extract_node(''' from collections import namedtuple namedtuple('a', 'a b c')(1, 2, 3) #@ ''') @@ -144,7 +143,7 @@ class NamedTupleTest(unittest.TestCase): self.assertEqual(attr[0].attrname, name) def test_namedtuple_uninferable_fields(self): - node = test_utils.extract_node(''' + node = astroid.extract_node(''' x = [A] * 2 from collections import namedtuple l = namedtuple('a', x) @@ -166,7 +165,7 @@ class ModuleExtenderTest(unittest.TestCase): class NoseBrainTest(unittest.TestCase): def test_nose_tools(self): - methods = test_utils.extract_node(""" + methods = astroid.extract_node(""" from nose.tools import assert_equal from nose.tools import assert_equals from nose.tools import assert_true @@ -192,7 +191,7 @@ class NoseBrainTest(unittest.TestCase): class SixBrainTest(unittest.TestCase): def test_attribute_access(self): - ast_nodes = test_utils.extract_node(''' + ast_nodes = astroid.extract_node(''' import six six.moves.http_client #@ six.moves.urllib_parse #@ @@ -262,7 +261,7 @@ class SixBrainTest(unittest.TestCase): self.assertEqual(urlretrieve.qname(), 'urllib.request.urlretrieve') def test_from_imports(self): - ast_node = test_utils.extract_node(''' + ast_node = astroid.extract_node(''' from six.moves import http_client http_client.HTTPSConnection #@ ''') @@ -285,7 +284,7 @@ class MultiprocessingBrainTest(unittest.TestCase): # Test that module attributes are working, # especially on Python 3.4+, where they are obtained # from a context. - module = test_utils.extract_node(""" + module = astroid.extract_node(""" import multiprocessing """) module = interpreterutil.do_import_module(module, 'multiprocessing') @@ -296,7 +295,7 @@ class MultiprocessingBrainTest(unittest.TestCase): self.assertIsInstance(cpu_count, astroid.BoundMethod) def test_module_name(self): - module = test_utils.extract_node(""" + module = astroid.extract_node(""" import multiprocessing multiprocessing.SyncManager() """) @@ -365,7 +364,7 @@ class MultiprocessingBrainTest(unittest.TestCase): class ThreadingBrainTest(unittest.TestCase): def test_threading(self): - module = test_utils.extract_node(""" + module = astroid.extract_node(""" import threading threading.Lock() """) @@ -457,7 +456,7 @@ class EnumBrainTest(unittest.TestCase): 'IntEnum based enums should be a subtype of int') def test_enum_func_form(self): - instance_1, instance_2 = test_utils.extract_node(''' + instance_1, instance_2 = astroid.extract_node(''' from enum import Enum f = Enum('Audience', ['a', 'b', 'c']) f #@ @@ -487,7 +486,7 @@ class DateutilBrainTest(unittest.TestCase): class NumpyBrainTest(unittest.TestCase): def test_numpy(self): - node = test_utils.extract_node(''' + node = astroid.extract_node(''' import numpy numpy.ones #@ ''') @@ -499,7 +498,7 @@ class NumpyBrainTest(unittest.TestCase): class PytestBrainTest(unittest.TestCase): def test_pytest(self): - ast_node = test_utils.extract_node(''' + ast_node = astroid.extract_node(''' import pytest pytest #@ ''') diff --git a/astroid/tests/unittest_builder.py b/astroid/tests/unittest_builder.py index dcb4e57c..140a4eeb 100644 --- a/astroid/tests/unittest_builder.py +++ b/astroid/tests/unittest_builder.py @@ -407,7 +407,7 @@ class BuilderTest(unittest.TestCase): if noe: yield more """ - func = test_utils.extract_node(code) + func = builder.extract_node(code) self.assertIsInstance(func, nodes.FunctionDef) stmt = func.body[0] self.assertIsInstance(stmt, nodes.Expr) @@ -555,7 +555,7 @@ class BuilderTest(unittest.TestCase): a.custom_attr = 0 a #@ ''' - name_nodes = test_utils.extract_node(code) + name_nodes = builder.extract_node(code) for node in name_nodes: self.assertNotIn('custom_attr', next(node.infer()).locals) self.assertNotIn('custom_attr', next(node.infer()).instance_attrs) @@ -588,7 +588,7 @@ class BuilderTest(unittest.TestCase): self.assertEqual(chain.value, 'None') def test_not_implemented(self): - node = test_utils.extract_node(''' + node = builder.extract_node(''' NotImplemented #@ ''') inferred = next(node.infer()) diff --git a/astroid/tests/unittest_helpers.py b/astroid/tests/unittest_helpers.py index 1826cfd7..357a2bd9 100644 --- a/astroid/tests/unittest_helpers.py +++ b/astroid/tests/unittest_helpers.py @@ -8,6 +8,7 @@ import unittest import six from six.moves import builtins +import astroid from astroid import builder from astroid import exceptions from astroid.interpreter import util as interpreterutil @@ -43,12 +44,12 @@ class TestHelpers(unittest.TestCase): ('import sys\nsys#@', self._look_up_in_builtins(types.ModuleType.__name__)), ] for code, expected in pairs: - node = test_utils.extract_node(code) + node = astroid.extract_node(code) objtype = interpreterutil.object_type(node) self.assertIs(objtype, expected) def test_object_type_classes_and_functions(self): - ast_nodes = test_utils.extract_node(''' + ast_nodes = astroid.extract_node(''' def generator(): yield @@ -111,7 +112,7 @@ class TestHelpers(unittest.TestCase): @test_utils.require_version(minver='3.0') def test_object_type_most_derived(self): - node = test_utils.extract_node(''' + node = astroid.extract_node(''' class A(type): def __new__(*args, **kwargs): return type.__new__(*args, **kwargs) @@ -128,14 +129,14 @@ class TestHelpers(unittest.TestCase): self.assertEqual(metaclass, obj_type) def test_inference_errors(self): - node = test_utils.extract_node(''' + node = astroid.extract_node(''' from unknown import Unknown u = Unknown #@ ''') self.assertEqual(interpreterutil.object_type(node), util.Uninferable) def test_object_type_too_many_types(self): - node = test_utils.extract_node(''' + node = astroid.extract_node(''' from unknown import Unknown def test(x): if x: @@ -147,7 +148,7 @@ class TestHelpers(unittest.TestCase): self.assertEqual(interpreterutil.object_type(node), util.Uninferable) def test_is_subtype(self): - ast_nodes = test_utils.extract_node(''' + ast_nodes = astroid.extract_node(''' class int_subclass(int): pass class A(object): pass #@ @@ -173,7 +174,7 @@ class TestHelpers(unittest.TestCase): @test_utils.require_version(maxver='3.0') def test_is_subtype_supertype_old_style_classes(self): - cls_a, cls_b = test_utils.extract_node(''' + cls_a, cls_b = astroid.extract_node(''' class A: #@ pass class B(A): #@ @@ -185,7 +186,7 @@ class TestHelpers(unittest.TestCase): self.assertFalse(interpreterutil.is_supertype(cls_b, cls_a)) def test_is_subtype_supertype_mro_error(self): - cls_e, cls_f = test_utils.extract_node(''' + cls_e, cls_f = astroid.extract_node(''' class A(object): pass class B(A): pass class C(A): pass @@ -200,7 +201,7 @@ class TestHelpers(unittest.TestCase): self.assertFalse(interpreterutil.is_supertype(cls_f, cls_e)) def test_is_subtype_supertype_unknown_bases(self): - cls_a, cls_b = test_utils.extract_node(''' + cls_a, cls_b = astroid.extract_node(''' from unknown import Unknown class A(Unknown): pass #@ class B(A): pass #@ @@ -211,7 +212,7 @@ class TestHelpers(unittest.TestCase): interpreterutil.is_supertype(cls_a, cls_b) def test_is_subtype_supertype_unrelated_classes(self): - cls_a, cls_b = test_utils.extract_node(''' + cls_a, cls_b = astroid.extract_node(''' class A(object): pass #@ class B(object): pass #@ ''') @@ -221,7 +222,7 @@ class TestHelpers(unittest.TestCase): self.assertFalse(interpreterutil.is_supertype(cls_b, cls_a)) def test_is_subtype_supertype_classes_no_type_ancestor(self): - cls_a = test_utils.extract_node(''' + cls_a = astroid.extract_node(''' class A(object): #@ pass ''') @@ -230,7 +231,7 @@ class TestHelpers(unittest.TestCase): self.assertFalse(interpreterutil.is_subtype(cls_a, builtin_type)) def test_is_subtype_supertype_classes_metaclasses(self): - cls_a = test_utils.extract_node(''' + cls_a = astroid.extract_node(''' class A(type): #@ pass ''') @@ -240,7 +241,7 @@ class TestHelpers(unittest.TestCase): @test_utils.require_version(maxver='3.0') def test_old_style_class(self): - cls = test_utils.extract_node('''class A: pass''') + cls = astroid.extract_node('''class A: pass''') builtin_type = self._look_up_in_builtins('type') self.assertEqual(interpreterutil.object_type(cls), builtin_type) diff --git a/astroid/tests/unittest_inference.py b/astroid/tests/unittest_inference.py index 5b05a3f6..e5177307 100644 --- a/astroid/tests/unittest_inference.py +++ b/astroid/tests/unittest_inference.py @@ -12,7 +12,7 @@ import warnings import six from astroid import InferenceError, builder, nodes -from astroid.builder import parse +from astroid.builder import parse, extract_node from astroid import exceptions from astroid.inference import infer_end as inference_infer_end from astroid.interpreter.objects import ( @@ -326,7 +326,7 @@ class InferenceTest(resources.SysPathSetup, unittest.TestCase): class A(A): #@ pass ''' - a1, a2 = test_utils.extract_node(code, __name__) + a1, a2 = extract_node(code, __name__) a2_ancestors = list(a2.ancestors()) self.assertEqual(len(a2_ancestors), 2) self.assertIs(a2_ancestors[0], a1) @@ -342,7 +342,7 @@ class InferenceTest(resources.SysPathSetup, unittest.TestCase): class A(B): #@ pass ''' - a1, b, a2 = test_utils.extract_node(code, __name__) + a1, b, a2 = extract_node(code, __name__) a2_ancestors = list(a2.ancestors()) self.assertEqual(len(a2_ancestors), 3) self.assertIs(a2_ancestors[0], b) @@ -381,7 +381,7 @@ class InferenceTest(resources.SysPathSetup, unittest.TestCase): def f(): raise __(NotImplementedError) ''' - error = test_utils.extract_node(code, __name__) + error = extract_node(code, __name__) nie = error.inferred()[0] self.assertIsInstance(nie, nodes.ClassDef) nie_ancestors = [c.name for c in nie.ancestors()] @@ -418,7 +418,7 @@ class InferenceTest(resources.SysPathSetup, unittest.TestCase): code = ''' del undefined_attr ''' - delete = test_utils.extract_node(code, __name__) + delete = extract_node(code, __name__) self.assertRaises(InferenceError, delete.infer) def test_del2(self): @@ -587,7 +587,7 @@ class InferenceTest(resources.SysPathSetup, unittest.TestCase): tags = list(tags) __(tags).append(vid) ''' - name = test_utils.extract_node(code, __name__) + name = extract_node(code, __name__) it = name.infer() tags = next(it) self.assertIsInstance(tags, nodes.List) @@ -649,7 +649,7 @@ class InferenceTest(resources.SysPathSetup, unittest.TestCase): return something return __(something).conjugate() ''' - func, retval = test_utils.extract_node(code, __name__) + func, retval = extract_node(code, __name__) self.assertEqual( [i.value for i in func.ilookup('something')], [1.0, 1.0j]) @@ -688,7 +688,7 @@ class InferenceTest(resources.SysPathSetup, unittest.TestCase): A()[0] #@ A()[-1] #@ ''' - ast_nodes = test_utils.extract_node(code, __name__) + ast_nodes = extract_node(code, __name__) expected = [1, 2, 3, 6, 'value', 'f', 3, 6, 42, 41] for node, expected_value in zip(ast_nodes, expected): inferred = next(node.infer()) @@ -696,7 +696,7 @@ class InferenceTest(resources.SysPathSetup, unittest.TestCase): self.assertEqual(inferred.value, expected_value) def test_invalid_subscripts(self): - ast_nodes = test_utils.extract_node(''' + ast_nodes = extract_node(''' class NoGetitem(object): pass class InvalidGetitem(object): @@ -711,7 +711,7 @@ class InferenceTest(resources.SysPathSetup, unittest.TestCase): self.assertRaises(InferenceError, next, node.infer()) for node in ast_nodes[3:]: self.assertEqual(next(node.infer()), util.Uninferable) - ast_nodes = test_utils.extract_node(''' + ast_nodes = extract_node(''' [1, 2, 3][None] #@ 'lala'['bala'] #@ ''') @@ -719,7 +719,7 @@ class InferenceTest(resources.SysPathSetup, unittest.TestCase): self.assertRaises(InferenceError, next, node.infer()) def test_bytes_subscript(self): - node = test_utils.extract_node('''b'a'[0]''') + node = extract_node('''b'a'[0]''') inferred = next(node.infer()) self.assertIsInstance(inferred, nodes.Const) if six.PY2: @@ -778,7 +778,7 @@ class InferenceTest(resources.SysPathSetup, unittest.TestCase): ''' # XXX failing since __builtin__.help assignment has # been moved into a function... - node = test_utils.extract_node(code, __name__) + node = extract_node(code, __name__) inferred = list(node.func.infer()) self.assertEqual(len(inferred), 1, inferred) self.assertIsInstance(inferred[0], Instance) @@ -788,7 +788,7 @@ class InferenceTest(resources.SysPathSetup, unittest.TestCase): code = ''' open("toto.txt") ''' - node = test_utils.extract_node(code, __name__).func + node = extract_node(code, __name__).func inferred = list(node.infer()) self.assertEqual(len(inferred), 1) if hasattr(sys, 'pypy_version_info'): @@ -886,7 +886,7 @@ class InferenceTest(resources.SysPathSetup, unittest.TestCase): self._test_const_inferred(ast['b'], True) def test_unary_op_numbers(self): - ast_nodes = test_utils.extract_node(''' + ast_nodes = extract_node(''' +1 #@ -1 #@ ~1 #@ @@ -900,7 +900,7 @@ class InferenceTest(resources.SysPathSetup, unittest.TestCase): @test_utils.require_version(minver='3.5') def test_matmul(self): - node = test_utils.extract_node(''' + node = extract_node(''' class Array: def __matmul__(self, other): return 42 @@ -947,7 +947,7 @@ class InferenceTest(resources.SysPathSetup, unittest.TestCase): self._test_const_inferred(ast['a'], 23<<1) def test_binary_op_other_type(self): - ast_nodes = test_utils.extract_node(''' + ast_nodes = extract_node(''' class A: def __add__(self, other): return other + 42 @@ -962,7 +962,7 @@ class InferenceTest(resources.SysPathSetup, unittest.TestCase): self.assertEqual(second, util.Uninferable) def test_binary_op_other_type_using_reflected_operands(self): - ast_nodes = test_utils.extract_node(''' + ast_nodes = extract_node(''' class A(object): def __radd__(self, other): return other + 42 @@ -977,7 +977,7 @@ class InferenceTest(resources.SysPathSetup, unittest.TestCase): self.assertEqual(second.value, 43) def test_binary_op_reflected_and_not_implemented_is_type_error(self): - ast_node = test_utils.extract_node(''' + ast_node = extract_node(''' class A(object): def __radd__(self, other): return NotImplemented @@ -1073,7 +1073,7 @@ class InferenceTest(resources.SysPathSetup, unittest.TestCase): def f(g = lambda: None): __(g()).x ''' - callfuncnode = test_utils.extract_node(code) + callfuncnode = extract_node(code) inferred = list(callfuncnode.infer()) self.assertEqual(len(inferred), 2, inferred) inferred.remove(util.Uninferable) @@ -1332,7 +1332,7 @@ class InferenceTest(resources.SysPathSetup, unittest.TestCase): self.assertEqual(len(inferred), 1, inferred) def test__new__bound_methods(self): - node = test_utils.extract_node(''' + node = extract_node(''' class cls(object): pass cls().__new__(cls) #@ ''') @@ -1533,7 +1533,7 @@ class InferenceTest(resources.SysPathSetup, unittest.TestCase): self.assertEqual(node.type, 'function') def test_no_infinite_ancestor_loop(self): - klass = test_utils.extract_node(""" + klass = extract_node(""" import datetime def method(self): @@ -1553,7 +1553,7 @@ class InferenceTest(resources.SysPathSetup, unittest.TestCase): self.config = {0: self.config[0]} self.config[0].test() #@ """ - ast = test_utils.extract_node(code, __name__) + ast = extract_node(code, __name__) expr = ast.func.expr self.assertRaises(InferenceError, next, expr.infer()) @@ -1573,7 +1573,7 @@ class InferenceTest(resources.SysPathSetup, unittest.TestCase): tuple(1) #@ tuple(1, 2) #@ """ - ast = test_utils.extract_node(code, __name__) + ast = extract_node(code, __name__) self.assertInferTuple(ast[0], []) self.assertInferTuple(ast[1], [1]) @@ -1605,7 +1605,7 @@ class InferenceTest(resources.SysPathSetup, unittest.TestCase): frozenset(1) #@ frozenset(1, 2) #@ """ - ast = test_utils.extract_node(code, __name__) + ast = extract_node(code, __name__) self.assertInferFrozenSet(ast[0], []) self.assertInferFrozenSet(ast[1], [1, 2]) @@ -1636,7 +1636,7 @@ class InferenceTest(resources.SysPathSetup, unittest.TestCase): set(1) #@ set(1, 2) #@ """ - ast = test_utils.extract_node(code, __name__) + ast = extract_node(code, __name__) self.assertInferSet(ast[0], []) self.assertInferSet(ast[1], [1, 2]) @@ -1667,7 +1667,7 @@ class InferenceTest(resources.SysPathSetup, unittest.TestCase): list(1) #@ list(1, 2) #@ """ - ast = test_utils.extract_node(code, __name__) + ast = extract_node(code, __name__) self.assertInferList(ast[0], []) self.assertInferList(ast[1], [1, 1, 2]) self.assertInferList(ast[2], [1, 2, 3]) @@ -1682,7 +1682,7 @@ class InferenceTest(resources.SysPathSetup, unittest.TestCase): self.assertEqual(inferred.qname(), "{}.list".format(BUILTINS)) def test_conversion_of_dict_methods(self): - ast_nodes = test_utils.extract_node(''' + ast_nodes = extract_node(''' list({1:2, 2:3}.values()) #@ list({1:2, 2:3}.keys()) #@ tuple({1:2, 2:3}.values()) #@ @@ -1702,7 +1702,7 @@ class InferenceTest(resources.SysPathSetup, unittest.TestCase): tuple(b"abc") #@ set(b"abc") #@ """ - ast = test_utils.extract_node(code, __name__) + ast = extract_node(code, __name__) self.assertInferList(ast[0], [97, 98, 99]) self.assertInferTuple(ast[1], [97, 98, 99]) self.assertInferSet(ast[2], [97, 98, 99]) @@ -1732,7 +1732,7 @@ class InferenceTest(resources.SysPathSetup, unittest.TestCase): dict([None, None]) #@ """ - ast = test_utils.extract_node(code, __name__) + ast = extract_node(code, __name__) self.assertInferDict(ast[0], {}) self.assertInferDict(ast[1], {'a': 1, 'b': 2, 'c': 3}) for i in range(2, 5): @@ -1750,7 +1750,7 @@ class InferenceTest(resources.SysPathSetup, unittest.TestCase): @unittest.expectedFailure def test_dict_inference_call_context_not_propagated(self): - ast_node = test_utils.extract_node(''' + ast_node = extract_node(''' def using_unknown_kwargs(**kwargs): return dict(**kwargs) using_unknown_kwargs(a=1, b=2) #@ @@ -1758,7 +1758,7 @@ class InferenceTest(resources.SysPathSetup, unittest.TestCase): self.assertInferDict(ast_node, {'a': 1, 'b': 2}) def test_dict_inference_kwargs(self): - ast_node = test_utils.extract_node('''dict(a=1, b=2, **{'c': 3})''') + ast_node = extract_node('''dict(a=1, b=2, **{'c': 3})''') self.assertInferDict(ast_node, {'a': 1, 'b': 2, 'c': 3}) @test_utils.require_version('3.5') @@ -1769,7 +1769,7 @@ class InferenceTest(resources.SysPathSetup, unittest.TestCase): ('dict({"a":1}, b=2, **{"c":3})', {'a':1, 'b':2, 'c':3}), ] for code, expected_value in pairs: - node = test_utils.extract_node(code) + node = extract_node(code) self.assertInferDict(node, expected_value) def test_dict_invalid_args(self): @@ -1779,7 +1779,7 @@ class InferenceTest(resources.SysPathSetup, unittest.TestCase): 'dict(**[])', ] for invalid in invalid_values: - ast_node = test_utils.extract_node(invalid) + ast_node = extract_node(invalid) inferred = next(ast_node.infer()) self.assertIsInstance(inferred, Instance) self.assertEqual(inferred.qname(), "{}.dict".format(BUILTINS)) @@ -1808,7 +1808,7 @@ class InferenceTest(resources.SysPathSetup, unittest.TestCase): ' '.find() #@ ' '.count() #@ """ - ast = test_utils.extract_node(code, __name__) + ast = extract_node(code, __name__) self.assertInferConst(ast[0], u'') for i in range(1, 16): self.assertInferConst(ast[i], '') @@ -1839,7 +1839,7 @@ class InferenceTest(resources.SysPathSetup, unittest.TestCase): u' '.find() #@ u' '.count() #@ """ - ast = test_utils.extract_node(code, __name__) + ast = extract_node(code, __name__) self.assertInferConst(ast[0], '') for i in range(1, 16): self.assertInferConst(ast[i], u'') @@ -2026,11 +2026,11 @@ class InferenceTest(resources.SysPathSetup, unittest.TestCase): self.assertRaises(InferenceError, next, module['no_yield'].infer()) def test_unary_op_leaks_stop_iteration(self): - node = test_utils.extract_node('+[] #@') + node = extract_node('+[] #@') self.assertEqual(util.Uninferable, next(node.infer())) def test_unary_operands(self): - ast_nodes = test_utils.extract_node(''' + ast_nodes = extract_node(''' import os def func(): pass from missing import missing @@ -2082,7 +2082,7 @@ class InferenceTest(resources.SysPathSetup, unittest.TestCase): self.assertEqual(inferred, util.Uninferable) def test_unary_op_instance_method_not_callable(self): - ast_node = test_utils.extract_node(''' + ast_node = extract_node(''' class A: __pos__ = (i for i in range(10)) +A() #@ @@ -2090,7 +2090,7 @@ class InferenceTest(resources.SysPathSetup, unittest.TestCase): self.assertRaises(InferenceError, next, ast_node.infer()) def test_binary_op_type_errors(self): - ast_nodes = test_utils.extract_node(''' + ast_nodes = extract_node(''' import collections 1 + "a" #@ 1 - [] #@ @@ -2165,7 +2165,7 @@ class InferenceTest(resources.SysPathSetup, unittest.TestCase): self.assertEqual(str(error), expected_value) def test_unary_type_errors(self): - ast_nodes = test_utils.extract_node(''' + ast_nodes = extract_node(''' import collections ~[] #@ ~() #@ @@ -2208,7 +2208,7 @@ class InferenceTest(resources.SysPathSetup, unittest.TestCase): def test_unary_empty_type_errors(self): # These aren't supported right now - ast_nodes = test_utils.extract_node(''' + ast_nodes = extract_node(''' ~(2 and []) #@ -(0 or {}) #@ ''') @@ -2233,7 +2233,7 @@ class InferenceTest(resources.SysPathSetup, unittest.TestCase): ('frozenset((1, 2))', True), ] for code, expected in pairs: - node = test_utils.extract_node(code) + node = extract_node(code) inferred = next(node.infer()) self.assertEqual(inferred.bool_value(), expected) @@ -2303,7 +2303,7 @@ class InferenceTest(resources.SysPathSetup, unittest.TestCase): self.assertEqual(compare.bool_value(), util.Uninferable) def test_bool_value_instances(self): - instances = test_utils.extract_node(''' + instances = extract_node(''' class FalseBoolInstance(object): def {bool}(self): return False @@ -2339,7 +2339,7 @@ class InferenceTest(resources.SysPathSetup, unittest.TestCase): self.assertEqual(inferred.bool_value(), expected_value) def test_infer_coercion_rules_for_floats_complex(self): - ast_nodes = test_utils.extract_node(''' + ast_nodes = extract_node(''' 1 + 1.0 #@ 1 * 1.0 #@ 2 - 1.0 #@ @@ -2355,7 +2355,7 @@ class InferenceTest(resources.SysPathSetup, unittest.TestCase): self.assertEqual(inferred.value, expected) def test_binop_list_with_elts(self): - ast_node = test_utils.extract_node(''' + ast_node = extract_node(''' x = [A] * 1 [1] + x ''') @@ -2366,7 +2366,7 @@ class InferenceTest(resources.SysPathSetup, unittest.TestCase): self.assertIs(inferred.elts[1], util.Uninferable) def test_binop_same_types(self): - ast_nodes = test_utils.extract_node(''' + ast_nodes = extract_node(''' class A(object): def __add__(self, other): return 42 @@ -2382,7 +2382,7 @@ class InferenceTest(resources.SysPathSetup, unittest.TestCase): self.assertEqual(inferred.value, expected) def test_binop_different_types_reflected_only(self): - node = test_utils.extract_node(''' + node = extract_node(''' class A(object): pass class B(object): @@ -2395,7 +2395,7 @@ class InferenceTest(resources.SysPathSetup, unittest.TestCase): self.assertEqual(inferred.name, 'A') def test_binop_different_types_normal_not_implemented_and_reflected(self): - node = test_utils.extract_node(''' + node = extract_node(''' class A(object): def __add__(self, other): return NotImplemented @@ -2409,7 +2409,7 @@ class InferenceTest(resources.SysPathSetup, unittest.TestCase): self.assertEqual(inferred.name, 'A') def test_binop_different_types_no_method_implemented(self): - node = test_utils.extract_node(''' + node = extract_node(''' class A(object): pass class B(object): pass @@ -2419,7 +2419,7 @@ class InferenceTest(resources.SysPathSetup, unittest.TestCase): self.assertEqual(inferred, util.Uninferable) def test_binop_different_types_reflected_and_normal_not_implemented(self): - node = test_utils.extract_node(''' + node = extract_node(''' class A(object): def __add__(self, other): return NotImplemented class B(object): @@ -2430,7 +2430,7 @@ class InferenceTest(resources.SysPathSetup, unittest.TestCase): self.assertEqual(inferred, util.Uninferable) def test_binop_subtype(self): - node = test_utils.extract_node(''' + node = extract_node(''' class A(object): pass class B(A): def __add__(self, other): return other @@ -2441,7 +2441,7 @@ class InferenceTest(resources.SysPathSetup, unittest.TestCase): self.assertEqual(inferred.name, 'A') def test_binop_subtype_implemented_in_parent(self): - node = test_utils.extract_node(''' + node = extract_node(''' class A(object): def __add__(self, other): return other class B(A): pass @@ -2452,7 +2452,7 @@ class InferenceTest(resources.SysPathSetup, unittest.TestCase): self.assertEqual(inferred.name, 'A') def test_binop_subtype_not_implemented(self): - node = test_utils.extract_node(''' + node = extract_node(''' class A(object): pass class B(A): @@ -2463,7 +2463,7 @@ class InferenceTest(resources.SysPathSetup, unittest.TestCase): self.assertEqual(inferred, util.Uninferable) def test_binop_supertype(self): - node = test_utils.extract_node(''' + node = extract_node(''' class A(object): pass class B(A): @@ -2476,7 +2476,7 @@ class InferenceTest(resources.SysPathSetup, unittest.TestCase): self.assertEqual(inferred.name, 'A') def test_binop_supertype_rop_not_implemented(self): - node = test_utils.extract_node(''' + node = extract_node(''' class A(object): def __add__(self, other): return other @@ -2490,7 +2490,7 @@ class InferenceTest(resources.SysPathSetup, unittest.TestCase): self.assertEqual(inferred.name, 'B') def test_binop_supertype_both_not_implemented(self): - node = test_utils.extract_node(''' + node = extract_node(''' class A(object): def __add__(self): return NotImplemented class B(A): @@ -2502,7 +2502,7 @@ class InferenceTest(resources.SysPathSetup, unittest.TestCase): self.assertEqual(inferred, util.Uninferable) def test_binop_inferrence_errors(self): - ast_nodes = test_utils.extract_node(''' + ast_nodes = extract_node(''' from unknown import Unknown class A(object): def __add__(self, other): return NotImplemented @@ -2517,7 +2517,7 @@ class InferenceTest(resources.SysPathSetup, unittest.TestCase): self.assertEqual(next(node.infer()), util.Uninferable) def test_binop_ambiguity(self): - ast_nodes = test_utils.extract_node(''' + ast_nodes = extract_node(''' class A(object): def __add__(self, other): if isinstance(other, B): @@ -2540,7 +2540,7 @@ class InferenceTest(resources.SysPathSetup, unittest.TestCase): self.assertEqual(next(node.infer()), util.Uninferable) def test_bin_op_supertype_more_complicated_example(self): - ast_node = test_utils.extract_node(''' + ast_node = extract_node(''' class A(object): def __init__(self): self.foo = 42 @@ -2560,7 +2560,7 @@ class InferenceTest(resources.SysPathSetup, unittest.TestCase): self.assertEqual(int(inferred.value), 45) def test_aug_op_same_type_not_implemented(self): - ast_node = test_utils.extract_node(''' + ast_node = extract_node(''' class A(object): def __iadd__(self, other): return NotImplemented def __add__(self, other): return NotImplemented @@ -2569,7 +2569,7 @@ class InferenceTest(resources.SysPathSetup, unittest.TestCase): self.assertEqual(next(ast_node.infer()), util.Uninferable) def test_aug_op_same_type_aug_implemented(self): - ast_node = test_utils.extract_node(''' + ast_node = extract_node(''' class A(object): def __iadd__(self, other): return other f = A() @@ -2580,7 +2580,7 @@ class InferenceTest(resources.SysPathSetup, unittest.TestCase): self.assertEqual(inferred.name, 'A') def test_aug_op_same_type_aug_not_implemented_normal_implemented(self): - ast_node = test_utils.extract_node(''' + ast_node = extract_node(''' class A(object): def __iadd__(self, other): return NotImplemented def __add__(self, other): return 42 @@ -2592,7 +2592,7 @@ class InferenceTest(resources.SysPathSetup, unittest.TestCase): self.assertEqual(inferred.value, 42) def test_aug_op_subtype_both_not_implemented(self): - ast_node = test_utils.extract_node(''' + ast_node = extract_node(''' class A(object): def __iadd__(self, other): return NotImplemented def __add__(self, other): return NotImplemented @@ -2604,7 +2604,7 @@ class InferenceTest(resources.SysPathSetup, unittest.TestCase): self.assertEqual(next(ast_node.infer()), util.Uninferable) def test_aug_op_subtype_aug_op_is_implemented(self): - ast_node = test_utils.extract_node(''' + ast_node = extract_node(''' class A(object): def __iadd__(self, other): return 42 class B(A): @@ -2617,7 +2617,7 @@ class InferenceTest(resources.SysPathSetup, unittest.TestCase): self.assertEqual(inferred.value, 42) def test_aug_op_subtype_normal_op_is_implemented(self): - ast_node = test_utils.extract_node(''' + ast_node = extract_node(''' class A(object): def __add__(self, other): return 42 class B(A): @@ -2630,7 +2630,7 @@ class InferenceTest(resources.SysPathSetup, unittest.TestCase): self.assertEqual(inferred.value, 42) def test_aug_different_types_no_method_implemented(self): - ast_node = test_utils.extract_node(''' + ast_node = extract_node(''' class A(object): pass class B(object): pass f = A() @@ -2639,7 +2639,7 @@ class InferenceTest(resources.SysPathSetup, unittest.TestCase): self.assertEqual(next(ast_node.infer()), util.Uninferable) def test_aug_different_types_augop_implemented(self): - ast_node = test_utils.extract_node(''' + ast_node = extract_node(''' class A(object): def __iadd__(self, other): return other class B(object): pass @@ -2651,7 +2651,7 @@ class InferenceTest(resources.SysPathSetup, unittest.TestCase): self.assertEqual(inferred.name, 'B') def test_aug_different_types_aug_not_implemented(self): - ast_node = test_utils.extract_node(''' + ast_node = extract_node(''' class A(object): def __iadd__(self, other): return NotImplemented def __add__(self, other): return other @@ -2664,7 +2664,7 @@ class InferenceTest(resources.SysPathSetup, unittest.TestCase): self.assertEqual(inferred.name, 'B') def test_aug_different_types_aug_not_implemented_rop_fallback(self): - ast_node = test_utils.extract_node(''' + ast_node = extract_node(''' class A(object): def __iadd__(self, other): return NotImplemented def __add__(self, other): return NotImplemented @@ -2678,7 +2678,7 @@ class InferenceTest(resources.SysPathSetup, unittest.TestCase): self.assertEqual(inferred.name, 'A') def test_augop_supertypes_none_implemented(self): - ast_node = test_utils.extract_node(''' + ast_node = extract_node(''' class A(object): pass class B(object): pass a = A() @@ -2687,7 +2687,7 @@ class InferenceTest(resources.SysPathSetup, unittest.TestCase): self.assertEqual(next(ast_node.infer()), util.Uninferable) def test_augop_supertypes_not_implemented_returned_for_all(self): - ast_node = test_utils.extract_node(''' + ast_node = extract_node(''' class A(object): def __iadd__(self, other): return NotImplemented def __add__(self, other): return NotImplemented @@ -2699,7 +2699,7 @@ class InferenceTest(resources.SysPathSetup, unittest.TestCase): self.assertEqual(next(ast_node.infer()), util.Uninferable) def test_augop_supertypes_augop_implemented(self): - ast_node = test_utils.extract_node(''' + ast_node = extract_node(''' class A(object): def __iadd__(self, other): return other class B(A): pass @@ -2711,7 +2711,7 @@ class InferenceTest(resources.SysPathSetup, unittest.TestCase): self.assertEqual(inferred.name, 'B') def test_augop_supertypes_reflected_binop_implemented(self): - ast_node = test_utils.extract_node(''' + ast_node = extract_node(''' class A(object): def __iadd__(self, other): return NotImplemented class B(A): @@ -2724,7 +2724,7 @@ class InferenceTest(resources.SysPathSetup, unittest.TestCase): self.assertEqual(inferred.name, 'A') def test_augop_supertypes_normal_binop_implemented(self): - ast_node = test_utils.extract_node(''' + ast_node = extract_node(''' class A(object): def __iadd__(self, other): return NotImplemented def __add__(self, other): return other @@ -2740,7 +2740,7 @@ class InferenceTest(resources.SysPathSetup, unittest.TestCase): @unittest.expectedFailure def test_string_interpolation(self): - ast_nodes = test_utils.extract_node(''' + ast_nodes = extract_node(''' "a%d%d" % (1, 2) #@ "a%(x)s" % {"x": 42} #@ ''') @@ -2751,7 +2751,7 @@ class InferenceTest(resources.SysPathSetup, unittest.TestCase): self.assertEqual(inferred.value, expected_value) def test_mul_list_supports__index__(self): - ast_nodes = test_utils.extract_node(''' + ast_nodes = extract_node(''' class Index(object): def __index__(self): return 2 class NotIndex(object): pass @@ -2771,7 +2771,7 @@ class InferenceTest(resources.SysPathSetup, unittest.TestCase): self.assertEqual(inferred, util.Uninferable) def test_subscript_supports__index__(self): - ast_nodes = test_utils.extract_node(''' + ast_nodes = extract_node(''' class Index(object): def __index__(self): return 2 class LambdaIndex(object): @@ -2794,7 +2794,7 @@ class InferenceTest(resources.SysPathSetup, unittest.TestCase): self.assertRaises(InferenceError, next, ast_nodes[2].infer()) def test_special_method_masquerading_as_another(self): - ast_node = test_utils.extract_node(''' + ast_node = extract_node(''' class Info(object): def __add__(self, other): return "lala" @@ -2808,7 +2808,7 @@ class InferenceTest(resources.SysPathSetup, unittest.TestCase): self.assertEqual(inferred.value, "lala") def test_unary_op_assignment(self): - ast_node = test_utils.extract_node(''' + ast_node = extract_node(''' class A(object): pass def pos(self): return 42 @@ -2822,7 +2822,7 @@ class InferenceTest(resources.SysPathSetup, unittest.TestCase): def _slicing_test_helper(self, pairs, cls, get_elts): for code, expected in pairs: - ast_node = test_utils.extract_node(code) + ast_node = extract_node(code) inferred = next(ast_node.infer()) self.assertIsInstance(inferred, cls) self.assertEqual(get_elts(inferred), expected, @@ -2894,11 +2894,11 @@ class InferenceTest(resources.SysPathSetup, unittest.TestCase): "(1, 2, 3)[1:object]", ] for code in examples: - node = test_utils.extract_node(code) + node = extract_node(code) self.assertRaises(InferenceError, next, node.infer()) def test_instance_slicing(self): - ast_nodes = test_utils.extract_node(''' + ast_nodes = extract_node(''' class A(object): def __getitem__(self, index): return [1, 2, 3, 4, 5][index] @@ -2917,7 +2917,7 @@ class InferenceTest(resources.SysPathSetup, unittest.TestCase): self.assertEqual([elt.value for elt in inferred.elts], expected) def test_instance_slicing_slices(self): - ast_node = test_utils.extract_node(''' + ast_node = extract_node(''' class A(object): def __getitem__(self, index): return index @@ -2929,7 +2929,7 @@ class InferenceTest(resources.SysPathSetup, unittest.TestCase): self.assertIsNone(inferred.upper) def test_instance_slicing_fails(self): - ast_nodes = test_utils.extract_node(''' + ast_nodes = extract_node(''' class A(object): def __getitem__(self, index): return 1[index] @@ -2940,7 +2940,7 @@ class InferenceTest(resources.SysPathSetup, unittest.TestCase): self.assertEqual(next(node.infer()), util.Uninferable) def test_type__new__with_metaclass(self): - ast_node = test_utils.extract_node(''' + ast_node = extract_node(''' class Metaclass(type): pass class Entity(object): @@ -2963,7 +2963,7 @@ class InferenceTest(resources.SysPathSetup, unittest.TestCase): self.assertEqual(a_inferred.value, 1) def test_type__new__not_enough_arguments(self): - ast_nodes = test_utils.extract_node(''' + ast_nodes = extract_node(''' type.__new__(1) #@ type.__new__(1, 2) #@ type.__new__(1, 2, 3) #@ @@ -2974,7 +2974,7 @@ class InferenceTest(resources.SysPathSetup, unittest.TestCase): self.assertIsInstance(inferred, Instance) def test_type__new__invalid_mcs_argument(self): - ast_nodes = test_utils.extract_node(''' + ast_nodes = extract_node(''' class Class(object): pass type.__new__(1, 2, 3, 4) #@ type.__new__(Class, 2, 3, 4) #@ @@ -2984,7 +2984,7 @@ class InferenceTest(resources.SysPathSetup, unittest.TestCase): self.assertIsInstance(inferred, Instance) def test_type__new__invalid_name(self): - ast_nodes = test_utils.extract_node(''' + ast_nodes = extract_node(''' class Class(type): pass type.__new__(Class, object, 1, 2) #@ type.__new__(Class, 1, 1, 2) #@ @@ -2995,7 +2995,7 @@ class InferenceTest(resources.SysPathSetup, unittest.TestCase): self.assertIsInstance(inferred, Instance) def test_type__new__invalid_bases(self): - ast_nodes = test_utils.extract_node(''' + ast_nodes = extract_node(''' type.__new__(type, 'a', 1, 2) #@ type.__new__(type, 'a', [], 2) #@ type.__new__(type, 'a', {}, 2) #@ @@ -3007,7 +3007,7 @@ class InferenceTest(resources.SysPathSetup, unittest.TestCase): self.assertIsInstance(inferred, Instance) def test_type__new__invalid_attrs(self): - ast_nodes = test_utils.extract_node(''' + ast_nodes = extract_node(''' type.__new__(type, 'a', (), ()) #@ type.__new__(type, 'a', (), object) #@ type.__new__(type, 'a', (), 1) #@ @@ -3019,7 +3019,7 @@ class InferenceTest(resources.SysPathSetup, unittest.TestCase): self.assertIsInstance(inferred, Instance) def test_type__new__metaclass_lookup(self): - ast_node = test_utils.extract_node(''' + ast_node = extract_node(''' class Metaclass(type): def test(cls): pass @classmethod @@ -3045,7 +3045,7 @@ class InferenceTest(resources.SysPathSetup, unittest.TestCase): self.assertEqual(attr[0].value, 42) def test_type__new__metaclass_and_ancestors_lookup(self): - ast_node = test_utils.extract_node(''' + ast_node = extract_node(''' class Book(object): title = 'Ubik' class MetaBook(type): @@ -3061,7 +3061,7 @@ class InferenceTest(resources.SysPathSetup, unittest.TestCase): def test_function_metaclasses(self): # These are not supported right now, although # they will be in the future. - ast_node = test_utils.extract_node(''' + ast_node = extract_node(''' import six class BookMeta(type): @@ -3085,7 +3085,7 @@ class InferenceTest(resources.SysPathSetup, unittest.TestCase): def test_subscript_inference_error(self): # Used to raise StopIteration - ast_node = test_utils.extract_node(''' + ast_node = extract_node(''' class AttributeDict(dict): def __getitem__(self, name): return self @@ -3097,7 +3097,7 @@ class InferenceTest(resources.SysPathSetup, unittest.TestCase): self.assertIsNone(safe_infer(ast_node.targets[0])) def test_classmethod_inferred_by_context(self): - ast_node = test_utils.extract_node(''' + ast_node = extract_node(''' class Super(object): def instance(cls): return cls() @@ -3116,7 +3116,7 @@ class InferenceTest(resources.SysPathSetup, unittest.TestCase): self.assertEqual(inferred.name, 'Sub') def test_infer_call_result_invalid_dunder_call_on_instance(self): - ast_nodes = test_utils.extract_node(''' + ast_nodes = extract_node(''' class A: __call__ = 42 class B: @@ -3132,7 +3132,7 @@ class InferenceTest(resources.SysPathSetup, unittest.TestCase): self.assertRaises(InferenceError, next, inferred.infer_call_result(node)) def test_context_call_for_context_managers(self): - ast_nodes = test_utils.extract_node(''' + ast_nodes = extract_node(''' class A: def __enter__(self): return self @@ -3161,7 +3161,7 @@ class InferenceTest(resources.SysPathSetup, unittest.TestCase): self.assertEqual(third_c.name, 'A') def test_metaclass_subclasses_arguments_are_classes_not_instances(self): - ast_node = test_utils.extract_node(''' + ast_node = extract_node(''' class A(type): def test(cls): return cls @@ -3177,7 +3177,7 @@ class InferenceTest(resources.SysPathSetup, unittest.TestCase): self.assertEqual(inferred.name, 'B') def test_infer_cls_in_class_methods(self): - ast_nodes = test_utils.extract_node(''' + ast_nodes = extract_node(''' class A(type): def __call__(cls): cls #@ @@ -3192,7 +3192,7 @@ class InferenceTest(resources.SysPathSetup, unittest.TestCase): @unittest.expectedFailure def test_metaclass_arguments_are_classes_not_instances(self): - ast_node = test_utils.extract_node(''' + ast_node = extract_node(''' class A(type): def test(cls): return cls A.test() #@ @@ -3203,7 +3203,7 @@ class InferenceTest(resources.SysPathSetup, unittest.TestCase): self.assertEqual(inferred.name, 'A') def test_delayed_attributes_without_slots(self): - ast_node = test_utils.extract_node(''' + ast_node = extract_node(''' class A(object): __slots__ = ('a', ) a = A() @@ -3218,7 +3218,7 @@ class InferenceTest(resources.SysPathSetup, unittest.TestCase): @test_utils.require_version(maxver='3.0') def test_delayed_attributes_with_old_style_classes(self): - ast_node = test_utils.extract_node(''' + ast_node = extract_node(''' class A: __slots__ = ('a', ) a = A() @@ -3228,7 +3228,7 @@ class InferenceTest(resources.SysPathSetup, unittest.TestCase): next(ast_node.infer()).getattr('teta') def test_delayed_attributes_writable_property(self): - ast_node = test_utils.extract_node(''' + ast_node = extract_node(''' class A(object): @property def test(self): @@ -3246,7 +3246,7 @@ class InferenceTest(resources.SysPathSetup, unittest.TestCase): self.assertEqual(value.value, "a") def test_delayed_attributes_non_writable_property(self): - ast_node = test_utils.extract_node(''' + ast_node = extract_node(''' class A(object): @property def test(self): @@ -3263,7 +3263,7 @@ class InferenceTest(resources.SysPathSetup, unittest.TestCase): self.assertEqual(value.value, 24) def test_lambda_as_methods(self): - ast_node = test_utils.extract_node(''' + ast_node = extract_node(''' class X: m = lambda self, arg: self.z + arg z = 24 @@ -3275,7 +3275,7 @@ class InferenceTest(resources.SysPathSetup, unittest.TestCase): self.assertEqual(inferred.value, 28) def test_inner_value_redefined_by_subclass(self): - ast_node = test_utils.extract_node(''' + ast_node = extract_node(''' class X(object): M = lambda self, arg: "a" x = 24 @@ -3296,7 +3296,7 @@ class InferenceTest(resources.SysPathSetup, unittest.TestCase): def test_inner_value_redefined_by_subclass_with_mro(self): # This might work, but it currently doesn't due to not being able # to reuse inference contexts. - ast_node = test_utils.extract_node(''' + ast_node = extract_node(''' class X(object): M = lambda self, arg: arg + 1 x = 24 @@ -3320,7 +3320,7 @@ class InferenceTest(resources.SysPathSetup, unittest.TestCase): class GetattrTest(unittest.TestCase): def test_yes_when_unknown(self): - ast_nodes = test_utils.extract_node(''' + ast_nodes = extract_node(''' from missing import Missing getattr(1, Unknown) #@ getattr(Unknown, 'a') #@ @@ -3340,7 +3340,7 @@ class GetattrTest(unittest.TestCase): self.assertEqual(inferred, util.Uninferable, node) def test_attrname_not_string(self): - ast_nodes = test_utils.extract_node(''' + ast_nodes = extract_node(''' getattr(1, 1) #@ c = int getattr(1, c) #@ @@ -3349,7 +3349,7 @@ class GetattrTest(unittest.TestCase): self.assertRaises(InferenceError, next, node.infer()) def test_attribute_missing(self): - ast_nodes = test_utils.extract_node(''' + ast_nodes = extract_node(''' getattr(1, 'ala') #@ getattr(int, 'ala') #@ getattr(float, 'bala') #@ @@ -3359,7 +3359,7 @@ class GetattrTest(unittest.TestCase): self.assertRaises(InferenceError, next, node.infer()) def test_default(self): - ast_nodes = test_utils.extract_node(''' + ast_nodes = extract_node(''' getattr(1, 'ala', None) #@ getattr(int, 'bala', int) #@ getattr(int, 'bala', getattr(int, 'portocala', None)) #@ @@ -3377,7 +3377,7 @@ class GetattrTest(unittest.TestCase): self.assertIsNone(third.value) def test_lookup(self): - ast_nodes = test_utils.extract_node(''' + ast_nodes = extract_node(''' class A(object): def test(self): pass class B(A): @@ -3421,7 +3421,7 @@ class GetattrTest(unittest.TestCase): self.assertEqual(fifth.bound.name, 'X') def test_lambda(self): - node = test_utils.extract_node(''' + node = extract_node(''' getattr(lambda x: x, 'f') #@ ''') inferred = next(node.infer()) @@ -3431,7 +3431,7 @@ class GetattrTest(unittest.TestCase): class HasattrTest(unittest.TestCase): def test_inference_errors(self): - ast_nodes = test_utils.extract_node(''' + ast_nodes = extract_node(''' from missing import Missing hasattr(Unknown, 'ala') #@ @@ -3444,7 +3444,7 @@ class HasattrTest(unittest.TestCase): self.assertEqual(inferred, util.Uninferable) def test_attribute_is_missing(self): - ast_nodes = test_utils.extract_node(''' + ast_nodes = extract_node(''' class A: pass hasattr(int, 'ala') #@ hasattr({}, 'bala') #@ @@ -3456,7 +3456,7 @@ class HasattrTest(unittest.TestCase): self.assertFalse(inferred.value) def test_attribute_is_not_missing(self): - ast_nodes = test_utils.extract_node(''' + ast_nodes = extract_node(''' class A(object): def test(self): pass class B(A): @@ -3480,7 +3480,7 @@ class HasattrTest(unittest.TestCase): self.assertTrue(inferred.value) def test_lambda(self): - node = test_utils.extract_node(''' + node = extract_node(''' hasattr(lambda x: x, 'f') #@ ''') inferred = next(node.infer()) @@ -3508,12 +3508,12 @@ class BoolOpTest(unittest.TestCase): ('not (True or False) and True', False), ] for code, expected_value in expected: - node = test_utils.extract_node(code) + node = extract_node(code) inferred = next(node.infer()) self.assertEqual(inferred.value, expected_value) def test_yes_when_unknown(self): - ast_nodes = test_utils.extract_node(''' + ast_nodes = extract_node(''' from unknown import unknown, any, not_any 0 and unknown #@ unknown or 0 #@ @@ -3524,7 +3524,7 @@ class BoolOpTest(unittest.TestCase): self.assertEqual(inferred, util.Uninferable) def test_other_nodes(self): - ast_nodes = test_utils.extract_node(''' + ast_nodes = extract_node(''' def test(): pass test and 0 #@ 1 and test #@ @@ -3555,12 +3555,12 @@ class TestCallable(unittest.TestCase): callable(C1) #@''', True), ] for code, expected_value in expected: - node = test_utils.extract_node(code) + node = extract_node(code) inferred = next(node.infer()) self.assertEqual(inferred.value, expected_value) def test_callable_methods(self): - ast_nodes = test_utils.extract_node(''' + ast_nodes = extract_node(''' class C: def test(self): pass @staticmethod @@ -3592,7 +3592,7 @@ class TestCallable(unittest.TestCase): self.assertTrue(inferred) def test_inference_errors(self): - ast_nodes = test_utils.extract_node(''' + ast_nodes = extract_node(''' from unknown import unknown callable(unknown) #@ def test(): @@ -3604,7 +3604,7 @@ class TestCallable(unittest.TestCase): self.assertEqual(inferred, util.Uninferable) def test_not_callable(self): - ast_nodes = test_utils.extract_node(''' + ast_nodes = extract_node(''' callable("") #@ callable(1) #@ callable(True) #@ @@ -3630,7 +3630,7 @@ class TestBool(unittest.TestCase): ('from unknown import Unknown; __(bool(Unknown))', util.Uninferable), ] for code, expected in pairs: - node = test_utils.extract_node(code) + node = extract_node(code) inferred = next(node.infer()) if expected is util.Uninferable: self.assertEqual(expected, inferred) @@ -3638,7 +3638,7 @@ class TestBool(unittest.TestCase): self.assertEqual(inferred.value, expected) def test_bool_bool_special_method(self): - ast_nodes = test_utils.extract_node(''' + ast_nodes = extract_node(''' class FalseClass: def {method}(self): return False @@ -3672,7 +3672,7 @@ class TestBool(unittest.TestCase): self.assertEqual(inferred.value, expected_value) def test_bool_instance_not_callable(self): - ast_nodes = test_utils.extract_node(''' + ast_nodes = extract_node(''' class BoolInvalid(object): {method} = 42 class LenInvalid(object): @@ -3698,7 +3698,7 @@ class TestType(unittest.TestCase): ('type(frozenset())', 'frozenset'), ] for code, expected in pairs: - node = test_utils.extract_node(code) + node = extract_node(code) inferred = next(node.infer()) self.assertIsInstance(inferred, nodes.ClassDef) self.assertEqual(inferred.name, expected) @@ -3721,7 +3721,7 @@ class ArgumentsTest(unittest.TestCase): (3, ), (), (3, 4, 5), (), (), (4, ), (4, 5), (), (3, ), (), (), (3, ), (42, )] - ast_nodes = test_utils.extract_node(''' + ast_nodes = extract_node(''' def func(*args): return args func() #@ @@ -3759,7 +3759,7 @@ class ArgumentsTest(unittest.TestCase): (1, 2, 3), (1, 4, 2, 3, 5, 6, 7), ] - ast_nodes = test_utils.extract_node(''' + ast_nodes = extract_node(''' def func(a, b, *args): return args func(1, 2, *(1, ), *(2, 3)) #@ @@ -3772,7 +3772,7 @@ class ArgumentsTest(unittest.TestCase): def test_defaults(self): expected_values = [42, 3, 41, 42] - ast_nodes = test_utils.extract_node(''' + ast_nodes = extract_node(''' def func(a, b, c=42, *args): return c func(1, 2) #@ @@ -3788,7 +3788,7 @@ class ArgumentsTest(unittest.TestCase): @test_utils.require_version('3.0') def test_kwonly_args(self): expected_values = [24, 24, 42, 23, 24, 24, 54] - ast_nodes = test_utils.extract_node(''' + ast_nodes = extract_node(''' def test(*, f, b): return f test(f=24, b=33) #@ def test(a, *, f): return f @@ -3813,7 +3813,7 @@ class ArgumentsTest(unittest.TestCase): [('a', 1)], [('a', 'b')], ] - ast_nodes = test_utils.extract_node(''' + ast_nodes = extract_node(''' def test(**kwargs): return kwargs test(a=1, b=2, c=3) #@ @@ -3827,7 +3827,7 @@ class ArgumentsTest(unittest.TestCase): self.assertEqual(value, expected_value) def test_kwargs_and_other_named_parameters(self): - ast_nodes = test_utils.extract_node(''' + ast_nodes = extract_node(''' def test(a=42, b=24, **kwargs): return kwargs test(42, 24, c=3, d=4) #@ @@ -3849,7 +3849,7 @@ class ArgumentsTest(unittest.TestCase): def test_kwargs_access_by_name(self): expected_values = [42, 42, 42, 24] - ast_nodes = test_utils.extract_node(''' + ast_nodes = extract_node(''' def test(**kwargs): return kwargs['f'] test(f=42) #@ @@ -3873,7 +3873,7 @@ class ArgumentsTest(unittest.TestCase): ('d', 4), ('f', 42), ] - ast_node = test_utils.extract_node(''' + ast_node = extract_node(''' def test(**kwargs): return kwargs test(a=1, b=2, **{'c': 3}, **{'d': 4}, f=42) #@ @@ -3884,7 +3884,7 @@ class ArgumentsTest(unittest.TestCase): self.assertEqual(value, expected_value) def test_kwargs_are_overriden(self): - ast_nodes = test_utils.extract_node(''' + ast_nodes = extract_node(''' def test(f): return f test(f=23, **{'f': 34}) #@ @@ -3897,7 +3897,7 @@ class ArgumentsTest(unittest.TestCase): self.assertEqual(inferred, util.Uninferable) def test_fail_to_infer_args(self): - ast_nodes = test_utils.extract_node(''' + ast_nodes = extract_node(''' def test(a, **kwargs): return a test(*missing) #@ test(*object) #@ @@ -3940,13 +3940,13 @@ class SliceTest(unittest.TestCase): ('[1, 2, 3][slice(0, 3, 2)]', [1, 3]), ] for node, expected_value in ast_nodes: - ast_node = test_utils.extract_node("__({})".format(node)) + ast_node = extract_node("__({})".format(node)) inferred = next(ast_node.infer()) self.assertIsInstance(inferred, nodes.List) self.assertEqual([elt.value for elt in inferred.elts], expected_value) def test_slice_inference_error(self): - ast_nodes = test_utils.extract_node(''' + ast_nodes = extract_node(''' from unknown import unknown [1, 2, 3][slice(None, unknown, unknown)] #@ [1, 2, 3][slice(None, missing, missing)] #@ @@ -3968,7 +3968,7 @@ class SliceTest(unittest.TestCase): ] for code, values in ast_nodes: lower, upper, step = values - node = test_utils.extract_node(code) + node = extract_node(code) inferred = next(node.infer()) self.assertIsInstance(inferred, nodes.Slice) lower_value = next(inferred.igetattr('start')) @@ -3983,7 +3983,7 @@ class SliceTest(unittest.TestCase): self.assertEqual(inferred.pytype(), '%s.slice' % BUILTINS) def test_slice_type(self): - ast_node = test_utils.extract_node('type(slice(None, None, None))') + ast_node = extract_node('type(slice(None, None, None))') inferred = next(ast_node.infer()) self.assertIsInstance(inferred, nodes.ClassDef) self.assertEqual(inferred.name, 'slice') @@ -3992,7 +3992,7 @@ class SliceTest(unittest.TestCase): class DirTest(unittest.TestCase): def test_instance_custom_ok_dir(self): - ast_nodes = test_utils.extract_node(''' + ast_nodes = extract_node(''' class A: def __dir__(self): return ['a', 'b', 'c'] @@ -4029,7 +4029,7 @@ class DirTest(unittest.TestCase): sorted(expected_values)) def test_modules_dir(self): - ast_nodes = test_utils.extract_node(''' + ast_nodes = extract_node(''' import collections class A: def __dir__(self): @@ -4044,14 +4044,14 @@ class DirTest(unittest.TestCase): self.assertTrue({'OrderedDict', 'deque', 'defaultdict', 'namedtuple'}.issubset(elts)) def test_list_dir(self): - ast_node = test_utils.extract_node('dir([])') + ast_node = extract_node('dir([])') inferred = next(ast_node.infer()) elts = {elt.value for elt in inferred.elts} self.assertTrue({'append', 'count', 'extend', 'index', 'insert', 'mro', 'pop', 'remove', 'reverse', 'sort'}.issubset(elts)) def test_metaclass_dir(self): - ast_nodes = test_utils.extract_node(''' + ast_nodes = extract_node(''' import six class Meta(type): def __dir__(self): @@ -4078,7 +4078,7 @@ class DirTest(unittest.TestCase): self.assertEqual(elts, ['a', 'b']) def test_class_dir(self): - ast_nodes = test_utils.extract_node(''' + ast_nodes = extract_node(''' class A: x = 42 class B(A): @@ -4096,7 +4096,7 @@ class DirTest(unittest.TestCase): self.assertTrue({'__bases__', 'x', 'y', '__dict__', '__doc__'}.issubset(elts)) def test_no_args_dir(self): - ast_node = test_utils.extract_node(''' + ast_node = extract_node(''' class A: pass class B: diff --git a/astroid/tests/unittest_lookup.py b/astroid/tests/unittest_lookup.py index a376e3be..e7179888 100644 --- a/astroid/tests/unittest_lookup.py +++ b/astroid/tests/unittest_lookup.py @@ -12,7 +12,6 @@ from astroid import exceptions from astroid.interpreter import lookup from astroid import nodes from astroid.tree import scoped_nodes -from astroid import test_utils from astroid import util from astroid.tests import resources @@ -270,7 +269,7 @@ class LookupTest(resources.SysPathSetup, unittest.TestCase): def test(self): pass ''' - member = test_utils.extract_node(code, __name__).targets[0] + member = builder.extract_node(code, __name__).targets[0] it = member.infer() obj = next(it) self.assertIsInstance(obj, nodes.Const) @@ -287,7 +286,7 @@ class LookupTest(resources.SysPathSetup, unittest.TestCase): def funcA(): return 4 ''' - decname = test_utils.extract_node(code, __name__) + decname = builder.extract_node(code, __name__) it = decname.infer() obj = next(it) self.assertIsInstance(obj, nodes.FunctionDef) diff --git a/astroid/tests/unittest_nodes.py b/astroid/tests/unittest_nodes.py index c9d0aa0f..ef276291 100644 --- a/astroid/tests/unittest_nodes.py +++ b/astroid/tests/unittest_nodes.py @@ -54,7 +54,7 @@ class AsStringTest(resources.SysPathSetup, unittest.TestCase): self.assertEqual(node.as_string().strip(), code.strip()) def test_as_string_for_list_containing_uninferable(self): - node = test_utils.extract_node(''' + node = astroid.extract_node(''' def foo(): bar = [arg] * 1 ''') @@ -64,7 +64,7 @@ class AsStringTest(resources.SysPathSetup, unittest.TestCase): self.assertEqual(binop.as_string(), '([arg]) * (1)') def test_frozenset_as_string(self): - nodes = test_utils.extract_node(''' + nodes = astroid.extract_node(''' frozenset((1, 2, 3)) #@ frozenset({1, 2, 3}) #@ frozenset([1, 2, 3,]) #@ @@ -310,7 +310,7 @@ class ImportNodeTest(resources.SysPathSetup, unittest.TestCase): self.module2 = resources.build_file('data/module2.py', 'data.module2') def test_do_import_module_works_for_all(self): - import_from, import_ = test_utils.extract_node(''' + import_from, import_ = astroid.extract_node(''' from collections import deque #@ import collections #@ ''') @@ -581,7 +581,7 @@ class BoundMethodNodeTest(unittest.TestCase): class Python35AsyncTest(unittest.TestCase): def test_async_await_keywords(self): - async_def, async_for, async_with, await_node = test_utils.extract_node(''' + async_def, async_for, async_with, await_node = astroid.extract_node(''' async def func(): #@ async for i in range(10): #@ f = __(await i) @@ -639,7 +639,7 @@ class BaseTypesTest(unittest.TestCase): class ScopeTest(unittest.TestCase): def test_decorators(self): - ast_node = test_utils.extract_node(''' + ast_node = astroid.extract_node(''' @test def foo(): pass ''') @@ -672,7 +672,7 @@ class ScopeTest(unittest.TestCase): self.assertIsInstance(module['listcomp'].parent.value.scope(), nodes.Module) def test_scope_of_default_argument_value(self): - node = test_utils.extract_node(''' + node = astroid.extract_node(''' def test(a=__(b)): pass ''') @@ -681,7 +681,7 @@ class ScopeTest(unittest.TestCase): @test_utils.require_version(minver='3.0') def test_scope_of_default_keyword_argument_value(self): - node = test_utils.extract_node(''' + node = astroid.extract_node(''' def test(*, b=__(c)): pass ''') @@ -690,7 +690,7 @@ class ScopeTest(unittest.TestCase): @test_utils.require_version(minver='3.0') def test_scope_of_annotations(self): - ast_nodes = test_utils.extract_node(''' + ast_nodes = astroid.extract_node(''' def test(a: __(b), *args:__(f), c:__(d)=4, **kwargs: _(l))->__(x): pass ''') @@ -699,7 +699,7 @@ class ScopeTest(unittest.TestCase): self.assertIsInstance(scope, nodes.Module) def test_scope_of_list_comprehension_target_composite_nodes(self): - ast_node = test_utils.extract_node(''' + ast_node = astroid.extract_node(''' [i for data in __([DATA1, DATA2]) for i in data] ''') node = ast_node.elts[0] @@ -707,7 +707,7 @@ class ScopeTest(unittest.TestCase): self.assertIsInstance(scope, nodes.Module) def test_scope_of_nested_list_comprehensions(self): - ast_node = test_utils.extract_node(''' + ast_node = astroid.extract_node(''' [1 for data in DATA for x in __(data)] ''') scope = ast_node.scope() @@ -717,7 +717,7 @@ class ScopeTest(unittest.TestCase): self.assertIsInstance(scope, nodes.ListComp) def test_scope_of_list_comprehension_targets(self): - ast_node = test_utils.extract_node(''' + ast_node = astroid.extract_node(''' [1 for data in DATA] ''') # target is `data` from the list comprehension @@ -729,7 +729,7 @@ class ScopeTest(unittest.TestCase): self.assertIsInstance(scope, nodes.ListComp) def test_scope_of_list_comprehension_value(self): - ast_node = test_utils.extract_node(''' + ast_node = astroid.extract_node(''' [__(i) for i in DATA] ''') scope = ast_node.scope() @@ -739,7 +739,7 @@ class ScopeTest(unittest.TestCase): self.assertIsInstance(scope, nodes.Module) def test_scope_of_dict_comprehension(self): - ast_nodes = test_utils.extract_node(''' + ast_nodes = astroid.extract_node(''' {i: __(j) for (i, j) in DATA} {i:j for (i, j) in __(DATA)} ''') @@ -748,14 +748,14 @@ class ScopeTest(unittest.TestCase): iter_scope = ast_nodes[1].scope() self.assertIsInstance(iter_scope, nodes.Module) - ast_node = test_utils.extract_node(''' + ast_node = astroid.extract_node(''' {i:1 for i in DATA}''') target = ast_node.generators[0].target target_scope = target.scope() self.assertIsInstance(target_scope, nodes.DictComp) def test_scope_elt_of_generator_exp(self): - ast_node = test_utils.extract_node(''' + ast_node = astroid.extract_node(''' list(__(i) for i in range(10)) ''') scope = ast_node.scope() @@ -765,47 +765,47 @@ class ScopeTest(unittest.TestCase): class ContextTest(unittest.TestCase): def test_subscript_load(self): - node = test_utils.extract_node('f[1]') + node = astroid.extract_node('f[1]') self.assertIs(node.ctx, astroid.Load) def test_subscript_del(self): - node = test_utils.extract_node('del f[1]') + node = astroid.extract_node('del f[1]') self.assertIs(node.targets[0].ctx, astroid.Del) def test_subscript_store(self): - node = test_utils.extract_node('f[1] = 2') + node = astroid.extract_node('f[1] = 2') subscript = node.targets[0] self.assertIs(subscript.ctx, astroid.Store) def test_list_load(self): - node = test_utils.extract_node('[]') + node = astroid.extract_node('[]') self.assertIs(node.ctx, astroid.Load) def test_list_del(self): - node = test_utils.extract_node('del []') + node = astroid.extract_node('del []') self.assertIs(node.targets[0].ctx, astroid.Del) def test_list_store(self): with self.assertRaises(exceptions.AstroidSyntaxError): - test_utils.extract_node('[0] = 2') + astroid.extract_node('[0] = 2') def test_tuple_load(self): - node = test_utils.extract_node('(1, )') + node = astroid.extract_node('(1, )') self.assertIs(node.ctx, astroid.Load) def test_tuple_store(self): with self.assertRaises(exceptions.AstroidSyntaxError): - test_utils.extract_node('(1, ) = 3') + astroid.extract_node('(1, ) = 3') @test_utils.require_version(minver='3.5') def test_starred_load(self): - node = test_utils.extract_node('a = *b') + node = astroid.extract_node('a = *b') starred = node.value self.assertIs(starred.ctx, astroid.Load) @test_utils.require_version(minver='3.0') def test_starred_store(self): - node = test_utils.extract_node('a, *b = 1, 2') + node = astroid.extract_node('a, *b = 1, 2') starred = node.targets[0].elts[1] self.assertIs(starred.ctx, astroid.Store) @@ -813,7 +813,7 @@ class ContextTest(unittest.TestCase): class FunctionTest(unittest.TestCase): def test_function_not_on_top_of_lambda(self): - lambda_, function_ = test_utils.extract_node(''' + lambda_, function_ = astroid.extract_node(''' lambda x: x #@ def func(): pass #@ ''') @@ -824,7 +824,7 @@ class FunctionTest(unittest.TestCase): class DictTest(unittest.TestCase): def test_keys_values_items(self): - node = test_utils.extract_node(''' + node = astroid.extract_node(''' {1: 2, 2:3} ''') self.assertEqual([key.value for key in node.keys], [1, 2]) @@ -837,7 +837,7 @@ class ParameterTest(unittest.TestCase): def _variadics_test_helper(self, vararg_lineno, vararg_col_offset, kwarg_lineno, kwarg_col_offset): - node = test_utils.extract_node(''' + node = astroid.extract_node(''' def test(*args, **kwargs): pass ''') args = node.args diff --git a/astroid/tests/unittest_object_model.py b/astroid/tests/unittest_object_model.py index 5d7635e6..369771aa 100644 --- a/astroid/tests/unittest_object_model.py +++ b/astroid/tests/unittest_object_model.py @@ -25,7 +25,7 @@ BUILTINS = MANAGER.builtins() class InstanceModelTest(unittest.TestCase): def test_instance_special_model(self): - ast_nodes = test_utils.extract_node(''' + ast_nodes = astroid.extract_node(''' class A: "test" def __init__(self): @@ -58,7 +58,7 @@ class InstanceModelTest(unittest.TestCase): @unittest.expectedFailure def test_instance_local_attributes_overrides_object_model(self): # The instance lookup needs to be changed in order for this to work. - ast_node = test_utils.extract_node(''' + ast_node = astroid.extract_node(''' class A: @property def __dict__(self): @@ -73,7 +73,7 @@ class InstanceModelTest(unittest.TestCase): class BoundMethodModelTest(unittest.TestCase): def test_bound_method_model(self): - ast_nodes = test_utils.extract_node(''' + ast_nodes = astroid.extract_node(''' class A: def test(self): pass a = A() @@ -94,7 +94,7 @@ class UnboundMethodModelTest(unittest.TestCase): @unittest.skipUnless(six.PY2, "Unbound methods are available in Python 2 only.") def test_unbound_method_model(self): - ast_nodes = test_utils.extract_node(''' + ast_nodes = astroid.extract_node(''' class A: def test(self): pass t = A.test @@ -127,7 +127,7 @@ class UnboundMethodModelTest(unittest.TestCase): class ClassModelTest(unittest.TestCase): def test_priority_to_local_defined_values(self): - ast_node = test_utils.extract_node(''' + ast_node = astroid.extract_node(''' class A: __doc__ = "first" A.__doc__ #@ @@ -138,7 +138,7 @@ class ClassModelTest(unittest.TestCase): @test_utils.require_version(maxver='3.0') def test__mro__old_style(self): - ast_node = test_utils.extract_node(''' + ast_node = astroid.extract_node(''' class A: pass A.__mro__ @@ -148,7 +148,7 @@ class ClassModelTest(unittest.TestCase): @test_utils.require_version(maxver='3.0') def test__subclasses__old_style(self): - ast_node = test_utils.extract_node(''' + ast_node = astroid.extract_node(''' class A: pass A.__subclasses__ @@ -157,7 +157,7 @@ class ClassModelTest(unittest.TestCase): next(ast_node.infer()) def test_class_model_correct_mro_subclasses_proxied(self): - ast_nodes = test_utils.extract_node(''' + ast_nodes = astroid.extract_node(''' class A(object): pass A.mro #@ @@ -172,7 +172,7 @@ class ClassModelTest(unittest.TestCase): @unittest.skipUnless(six.PY2, "Needs old style classes") def test_old_style_classes_no_mro(self): - ast_node = test_utils.extract_node(''' + ast_node = astroid.extract_node(''' class A: pass A.mro #@ @@ -181,7 +181,7 @@ class ClassModelTest(unittest.TestCase): next(ast_node.infer()) def test_class_model(self): - ast_nodes = test_utils.extract_node(''' + ast_nodes = astroid.extract_node(''' class A(object): "test" @@ -252,7 +252,7 @@ class ModuleModelTest(unittest.TestCase): self.assertEqual(file_value.value, "mine") def test__path__not_a_package(self): - ast_node = test_utils.extract_node(''' + ast_node = astroid.extract_node(''' import sys sys.__path__ #@ ''') @@ -260,7 +260,7 @@ class ModuleModelTest(unittest.TestCase): next(ast_node.infer()) def test_module_model(self): - ast_nodes = test_utils.extract_node(''' + ast_nodes = astroid.extract_node(''' import xml xml.__path__ #@ xml.__name__ #@ @@ -305,7 +305,7 @@ class ModuleModelTest(unittest.TestCase): class FunctionModelTest(unittest.TestCase): def test_partial_descriptor_support(self): - bound, result = test_utils.extract_node(''' + bound, result = astroid.extract_node(''' class A(object): pass def test(self): return 42 f = test.__get__(A(), A) @@ -323,7 +323,7 @@ class FunctionModelTest(unittest.TestCase): def test_descriptor_not_inferrring_self(self): # We can't infer __get__(X, Y)() when the bounded function # uses self, because of the tree's parent not being propagating good enough. - result = test_utils.extract_node(''' + result = astroid.extract_node(''' class A(object): x = 42 def test(self): return self.x @@ -335,7 +335,7 @@ class FunctionModelTest(unittest.TestCase): self.assertEqual(result.value, 42) def test_descriptors_binding_invalid(self): - ast_nodes = test_utils.extract_node(''' + ast_nodes = astroid.extract_node(''' class A: pass def test(self): return 42 test.__get__()() #@ @@ -347,7 +347,7 @@ class FunctionModelTest(unittest.TestCase): next(node.infer()) def test_function_model(self): - ast_nodes = test_utils.extract_node(''' + ast_nodes = astroid.extract_node(''' def func(a=1, b=2): """test""" func.__name__ #@ @@ -392,7 +392,7 @@ class FunctionModelTest(unittest.TestCase): @test_utils.require_version(minver='3.0') def test_empty_return_annotation(self): - ast_node = test_utils.extract_node(''' + ast_node = astroid.extract_node(''' def test(): pass test.__annotations__ ''') @@ -402,7 +402,7 @@ class FunctionModelTest(unittest.TestCase): @test_utils.require_version(minver='3.0') def test_annotations_kwdefaults(self): - ast_node = test_utils.extract_node(''' + ast_node = astroid.extract_node(''' def test(a: 1, *args: 2, f:4='lala', **kwarg:3)->2: pass test.__annotations__ #@ test.__kwdefaults__ #@ @@ -423,7 +423,7 @@ class FunctionModelTest(unittest.TestCase): @test_utils.require_version(maxver='3.0') def test_function_model_for_python2(self): - ast_nodes = test_utils.extract_node(''' + ast_nodes = astroid.extract_node(''' def test(a=1): "a" @@ -454,7 +454,7 @@ class FunctionModelTest(unittest.TestCase): class GeneratorModelTest(unittest.TestCase): def test_model(self): - ast_nodes = test_utils.extract_node(''' + ast_nodes = astroid.extract_node(''' def test(): "a" yield @@ -489,7 +489,7 @@ class ExceptionModelTest(unittest.TestCase): @unittest.skipIf(six.PY2, "needs Python 3") def test_model_py3(self): - ast_nodes = test_utils.extract_node(''' + ast_nodes = astroid.extract_node(''' try: x[42] except ValueError as err: @@ -509,7 +509,7 @@ class ExceptionModelTest(unittest.TestCase): @unittest.skipUnless(six.PY2, "needs Python 2") def test_model_py3(self): - ast_nodes = test_utils.extract_node(''' + ast_nodes = astroid.extract_node(''' try: x[42] except ValueError as err: @@ -530,13 +530,13 @@ class ExceptionModelTest(unittest.TestCase): class DictObjectModelTest(unittest.TestCase): def test__class__(self): - ast_node = test_utils.extract_node('{}.__class__') + ast_node = astroid.extract_node('{}.__class__') inferred = next(ast_node.infer()) self.assertIsInstance(inferred, astroid.ClassDef) self.assertEqual(inferred.name, 'dict') def test_attributes_inferred_as_methods(self): - ast_nodes = test_utils.extract_node(''' + ast_nodes = astroid.extract_node(''' {}.values #@ {}.items #@ {}.keys #@ @@ -547,7 +547,7 @@ class DictObjectModelTest(unittest.TestCase): @unittest.skipUnless(six.PY2, "needs Python 2") def test_concrete_objects_for_dict_methods(self): - ast_nodes = test_utils.extract_node(''' + ast_nodes = astroid.extract_node(''' {1:1, 2:3}.values() #@ {1:1, 2:3}.keys() #@ {1:1, 2:3}.items() #@ @@ -568,7 +568,7 @@ class DictObjectModelTest(unittest.TestCase): @unittest.skipIf(six.PY2, "needs Python 3") def test_wrapper_objects_for_dict_methods_python3(self): - ast_nodes = test_utils.extract_node(''' + ast_nodes = astroid.extract_node(''' {1:1, 2:3}.values() #@ {1:1, 2:3}.keys() #@ {1:1, 2:3}.items() #@ @@ -590,7 +590,7 @@ class LruCacheModelTest(unittest.TestCase): @unittest.skipIf(six.PY2, "needs Python 3") def test_lru_cache(self): - ast_nodes = test_utils.extract_node(''' + ast_nodes = astroid.extract_node(''' import functools class Foo(object): @functools.lru_cache() diff --git a/astroid/tests/unittest_objects.py b/astroid/tests/unittest_objects.py index f5c70c0f..d78301f9 100644 --- a/astroid/tests/unittest_objects.py +++ b/astroid/tests/unittest_objects.py @@ -6,6 +6,7 @@ import unittest import six +import astroid from astroid import exceptions from astroid import nodes from astroid.interpreter import objects @@ -18,7 +19,7 @@ BUILTINS = six.moves.builtins.__name__ class ObjectsTest(unittest.TestCase): def test_frozenset(self): - node = test_utils.extract_node(""" + node = astroid.extract_node(""" frozenset({1: 2, 2: 3}) #@ """) inferred = next(node.infer()) @@ -39,7 +40,7 @@ class ObjectsTest(unittest.TestCase): class SuperTests(unittest.TestCase): def test_inferring_super_outside_methods(self): - ast_nodes = test_utils.extract_node(''' + ast_nodes = astroid.extract_node(''' class Module(object): pass class StaticMethod(object): @@ -65,7 +66,7 @@ class SuperTests(unittest.TestCase): self.assertEqual(no_arguments.qname(), "%s.super" % BUILTINS) def test_inferring_unbound_super_doesnt_work(self): - node = test_utils.extract_node(''' + node = astroid.extract_node(''' class Test(object): def __init__(self): super(Test) #@ @@ -75,7 +76,7 @@ class SuperTests(unittest.TestCase): self.assertEqual(unbounded.qname(), "%s.super" % BUILTINS) def test_use_default_inference_on_not_inferring_args(self): - ast_nodes = test_utils.extract_node(''' + ast_nodes = astroid.extract_node(''' class Test(object): def __init__(self): super(Lala, self) #@ @@ -94,7 +95,7 @@ class SuperTests(unittest.TestCase): # super doesn't work on old style class, but leave # that as an error for pylint. We'll infer Super objects, # but every call will result in a failure at some point. - node = test_utils.extract_node(''' + node = astroid.extract_node(''' class OldStyle: def __init__(self): super(OldStyle, self) #@ @@ -110,7 +111,7 @@ class SuperTests(unittest.TestCase): @test_utils.require_version(minver='3.0') def test_no_arguments_super(self): - ast_nodes = test_utils.extract_node(''' + ast_nodes = astroid.extract_node(''' class First(object): pass class Second(First): def test(self): @@ -134,7 +135,7 @@ class SuperTests(unittest.TestCase): self.assertEqual(second.mro_pointer.name, 'Second') def test_super_simple_cases(self): - ast_nodes = test_utils.extract_node(''' + ast_nodes = astroid.extract_node(''' class First(object): pass class Second(First): pass class Third(First): @@ -196,7 +197,7 @@ class SuperTests(unittest.TestCase): self.assertEqual(fifth.mro_pointer.name, 'Fourth') def test_super_infer(self): - node = test_utils.extract_node(''' + node = astroid.extract_node(''' class Super(object): def __init__(self): super(Super, self) #@ @@ -208,7 +209,7 @@ class SuperTests(unittest.TestCase): self.assertIs(inferred, reinferred) def test_inferring_invalid_supers(self): - ast_nodes = test_utils.extract_node(''' + ast_nodes = astroid.extract_node(''' class Super(object): def __init__(self): # MRO pointer is not a type @@ -235,7 +236,7 @@ class SuperTests(unittest.TestCase): self.assertIsInstance(cm.exception.super_.type, invalid_type) def test_proxied(self): - node = test_utils.extract_node(''' + node = astroid.extract_node(''' class Super(object): def __init__(self): super(Super, self) #@ @@ -246,7 +247,7 @@ class SuperTests(unittest.TestCase): self.assertIsInstance(proxied, nodes.ClassDef) def test_super_bound_model(self): - ast_nodes = test_utils.extract_node(''' + ast_nodes = astroid.extract_node(''' class First(object): def method(self): pass @@ -299,7 +300,7 @@ class SuperTests(unittest.TestCase): self.assertEqual(sixth.type, 'classmethod') def test_super_getattr_single_inheritance(self): - ast_nodes = test_utils.extract_node(''' + ast_nodes = astroid.extract_node(''' class First(object): def test(self): pass class Second(First): @@ -345,7 +346,7 @@ class SuperTests(unittest.TestCase): self.assertEqual(second_unbound.parent.name, 'First') def test_super_invalid_mro(self): - node = test_utils.extract_node(''' + node = astroid.extract_node(''' class A(object): test = 42 class Super(A, A): @@ -357,7 +358,7 @@ class SuperTests(unittest.TestCase): next(inferred.getattr('test')) def test_super_complex_mro(self): - ast_nodes = test_utils.extract_node(''' + ast_nodes = astroid.extract_node(''' class A(object): def spam(self): return "A" def foo(self): return "A" @@ -392,7 +393,7 @@ class SuperTests(unittest.TestCase): self.assertEqual(static.parent.scope().name, 'A') def test_super_data_model(self): - ast_nodes = test_utils.extract_node(''' + ast_nodes = astroid.extract_node(''' class X(object): pass class A(X): def __init__(self): @@ -432,7 +433,7 @@ class SuperTests(unittest.TestCase): expected_mro) def test_super_mro(self): - ast_nodes = test_utils.extract_node(''' + ast_nodes = astroid.extract_node(''' class A(object): pass class B(A): pass class C(A): pass @@ -460,7 +461,7 @@ class SuperTests(unittest.TestCase): fifth.super_mro() def test_super_yes_objects(self): - ast_nodes = test_utils.extract_node(''' + ast_nodes = astroid.extract_node(''' from collections import Missing class A(object): def __init__(self): @@ -473,7 +474,7 @@ class SuperTests(unittest.TestCase): self.assertIsInstance(second, objects.Instance) def test_super_invalid_types(self): - node = test_utils.extract_node(''' + node = astroid.extract_node(''' import collections class A(object): def __init__(self): @@ -486,7 +487,7 @@ class SuperTests(unittest.TestCase): inferred.super_mro() def test_super_properties(self): - node = test_utils.extract_node(''' + node = astroid.extract_node(''' class Foo(object): @property def dict(self): @@ -507,7 +508,7 @@ class SuperTests(unittest.TestCase): class MethodTest(unittest.TestCase): def test_unbound_function_method_difference(self): - node = test_utils.extract_node(''' + node = astroid.extract_node(''' class A: def test(self): pass A.test @@ -519,7 +520,7 @@ class MethodTest(unittest.TestCase): self.assertIsInstance(inferred, nodes.FunctionDef) def test_unbound_function_from_classmethods(self): - node = test_utils.extract_node(''' + node = astroid.extract_node(''' class A: @classmethod def test(cls): return cls.b @@ -533,7 +534,7 @@ class MethodTest(unittest.TestCase): self.assertIsInstance(inferred, nodes.FunctionDef) def test_static_method(self): - node = test_utils.extract_node(''' + node = astroid.extract_node(''' class A: @staticmethod def test(self): pass @@ -544,7 +545,7 @@ class MethodTest(unittest.TestCase): @unittest.skipUnless(six.PY2, "Unbound methods are available only on Python 2") def test_underlying_proxied_unbound_method(self): - node = test_utils.extract_node(''' + node = astroid.extract_node(''' class A: def test(self): pass A.test @@ -553,7 +554,7 @@ class MethodTest(unittest.TestCase): self.assertIsInstance(inferred._proxied, nodes.FunctionDef) def test_underlying_proxied_bound_method(self): - node = test_utils.extract_node(''' + node = astroid.extract_node(''' class A: def test(self): pass A().test diff --git a/astroid/tests/unittest_protocols.py b/astroid/tests/unittest_protocols.py index 75b8d65b..82d01c7d 100644 --- a/astroid/tests/unittest_protocols.py +++ b/astroid/tests/unittest_protocols.py @@ -6,11 +6,12 @@ import contextlib import unittest import astroid -from astroid.test_utils import extract_node, require_version +from astroid.test_utils import require_version from astroid import InferenceError from astroid import nodes from astroid import util from astroid.tree.node_classes import AssignName, Const, Name, Starred +from astroid import extract_node @contextlib.contextmanager diff --git a/astroid/tests/unittest_python3.py b/astroid/tests/unittest_python3.py index d37f73dd..e5586078 100644 --- a/astroid/tests/unittest_python3.py +++ b/astroid/tests/unittest_python3.py @@ -7,9 +7,9 @@ import unittest from astroid import nodes from astroid.tree.node_classes import Assign, Expr, YieldFrom, Name, Const from astroid import raw_building -from astroid.builder import AstroidBuilder +from astroid.builder import AstroidBuilder, extract_node from astroid.tree.scoped_nodes import ClassDef, FunctionDef -from astroid.test_utils import require_version, extract_node +from astroid.test_utils import require_version class Python3TC(unittest.TestCase): diff --git a/astroid/tests/unittest_raw_building.py b/astroid/tests/unittest_raw_building.py index bf4b762a..83cffb6b 100644 --- a/astroid/tests/unittest_raw_building.py +++ b/astroid/tests/unittest_raw_building.py @@ -7,7 +7,7 @@ import unittest from six.moves import builtins # pylint: disable=import-error -from astroid.builder import AstroidBuilder +from astroid.builder import AstroidBuilder, extract_node from astroid import nodes from astroid import raw_building from astroid import test_utils @@ -37,7 +37,7 @@ class RawBuildingTC(unittest.TestCase): for name, _ in inspect.getmembers(builtins, predicate=inspect.isbuiltin): if name == 'print': continue - node = test_utils.extract_node('{0} #@'.format(name)) + node = extract_node('{0} #@'.format(name)) inferred = next(node.infer()) self.assertIsInstance(inferred, nodes.FunctionDef, name) self.assertEqual(inferred.root().name, BUILTINS, name) diff --git a/astroid/tests/unittest_regrtest.py b/astroid/tests/unittest_regrtest.py index deaad3c3..ff957dc9 100644 --- a/astroid/tests/unittest_regrtest.py +++ b/astroid/tests/unittest_regrtest.py @@ -8,12 +8,12 @@ import textwrap import six from astroid import MANAGER, Instance, nodes -from astroid.builder import AstroidBuilder +from astroid.builder import AstroidBuilder, extract_node from astroid import exceptions from astroid.interpreter import lookup from astroid.manager import AstroidManager from astroid import raw_building -from astroid.test_utils import require_version, extract_node, bootstrap +from astroid.test_utils import require_version, bootstrap from astroid.tests import resources from astroid import transforms diff --git a/astroid/tests/unittest_scoped_nodes.py b/astroid/tests/unittest_scoped_nodes.py index 6a8012f8..0fbc099a 100644 --- a/astroid/tests/unittest_scoped_nodes.py +++ b/astroid/tests/unittest_scoped_nodes.py @@ -318,7 +318,7 @@ class FunctionNodeTest(ModuleLoader, unittest.TestCase): self.assertFalse(func.is_abstract(pass_is_abstract=False)) def test_is_abstract_decorated(self): - methods = test_utils.extract_node(""" + methods = builder.extract_node(""" import abc class Klass(object): @@ -540,7 +540,7 @@ class FunctionNodeTest(ModuleLoader, unittest.TestCase): 'classmethod') def test_igetattr(self): - func = test_utils.extract_node(''' + func = builder.extract_node(''' def test(): pass ''') @@ -576,7 +576,7 @@ class ClassNodeTest(ModuleLoader, unittest.TestCase): if not cls.newstyle: self.assertRaises(AttributeInferenceError, cls.getattr, '__mro__') - ast_list = test_utils.extract_node("[1]") + ast_list = builder.extract_node("[1]") for cls in (ast_list._proxied, nodes.Const(1)._proxied): self.assertEqual(len(cls.getattr('__bases__')), 1) @@ -588,7 +588,7 @@ class ClassNodeTest(ModuleLoader, unittest.TestCase): self.assertEqual(len(cls.getattr('__mro__')), 1) def test__mro__attribute(self): - node = test_utils.extract_node(''' + node = builder.extract_node(''' class A(object): pass class B(object): pass class C(A, B): pass @@ -598,7 +598,7 @@ class ClassNodeTest(ModuleLoader, unittest.TestCase): self.assertEqual(mro.elts, node.mro()) def test__bases__attribute(self): - node = test_utils.extract_node(''' + node = builder.extract_node(''' class A(object): pass class B(object): pass class C(A, B): pass @@ -1037,7 +1037,7 @@ class ClassNodeTest(ModuleLoader, unittest.TestCase): self.assertEqual(meta.name, metaclass) def test_metaclass_type(self): - klass = test_utils.extract_node(""" + klass = builder.extract_node(""" def with_metaclass(meta, base=object): return meta("NewBase", (base, ), {}) @@ -1049,7 +1049,7 @@ class ClassNodeTest(ModuleLoader, unittest.TestCase): [base.name for base in klass.ancestors()]) def test_no_infinite_metaclass_loop(self): - klass = test_utils.extract_node(""" + klass = builder.extract_node(""" class SSS(object): class JJJ(object): @@ -1071,7 +1071,7 @@ class ClassNodeTest(ModuleLoader, unittest.TestCase): self.assertIn('JJJ', ancestors) def test_no_infinite_metaclass_loop_with_redefine(self): - nodes = test_utils.extract_node(""" + nodes = builder.extract_node(""" import datetime class A(datetime.date): #@ @@ -1089,7 +1089,7 @@ class ClassNodeTest(ModuleLoader, unittest.TestCase): self.assertEqual(None, klass.metaclass()) def test_metaclass_generator_hack(self): - klass = test_utils.extract_node(""" + klass = builder.extract_node(""" import six class WithMeta(six.with_metaclass(type, object)): #@ @@ -1102,7 +1102,7 @@ class ClassNodeTest(ModuleLoader, unittest.TestCase): 'type', klass.metaclass().name) def test_using_six_add_metaclass(self): - klass = test_utils.extract_node(''' + klass = builder.extract_node(''' import six import abc @@ -1116,7 +1116,7 @@ class ClassNodeTest(ModuleLoader, unittest.TestCase): self.assertEqual(metaclass.qname(), 'abc.ABCMeta') def test_using_invalid_six_add_metaclass_call(self): - klass = test_utils.extract_node(''' + klass = builder.extract_node(''' import six @six.add_metaclass() class Invalid(object): @@ -1265,7 +1265,7 @@ class ClassNodeTest(ModuleLoader, unittest.TestCase): @test_utils.require_version(maxver='3.0') def test_no_mro_for_old_style(self): - node = test_utils.extract_node(""" + node = builder.extract_node(""" class Old: pass""") with self.assertRaises(TypeError) as cm: node.mro() @@ -1274,7 +1274,7 @@ class ClassNodeTest(ModuleLoader, unittest.TestCase): @test_utils.require_version(maxver='3.0') def test_combined_newstyle_oldstyle_in_mro(self): - node = test_utils.extract_node(''' + node = builder.extract_node(''' class Old: pass class New(object): @@ -1380,7 +1380,7 @@ class ClassNodeTest(ModuleLoader, unittest.TestCase): self.assertIsInstance(cm.exception, ResolveError) def test_generator_from_infer_call_result_parent(self): - func = test_utils.extract_node(""" + func = builder.extract_node(""" import contextlib @contextlib.contextmanager @@ -1392,7 +1392,7 @@ class ClassNodeTest(ModuleLoader, unittest.TestCase): self.assertEqual(result.parent, func) def test_type_three_arguments(self): - classes = test_utils.extract_node(""" + classes = builder.extract_node(""" type('A', (object, ), {"a": 1, "b": 2, missing: 3}) #@ """) first = next(classes.infer()) @@ -1409,7 +1409,7 @@ class ClassNodeTest(ModuleLoader, unittest.TestCase): first.getattr("missing") def test_implicit_metaclass(self): - cls = test_utils.extract_node(""" + cls = builder.extract_node(""" class A(object): pass """) @@ -1417,7 +1417,7 @@ class ClassNodeTest(ModuleLoader, unittest.TestCase): self.assertEqual(cls.implicit_metaclass(), type_cls) def test_implicit_metaclass_lookup(self): - cls = test_utils.extract_node(''' + cls = builder.extract_node(''' class A(object): pass ''') @@ -1428,7 +1428,7 @@ class ClassNodeTest(ModuleLoader, unittest.TestCase): def test_metaclass_lookup_using_same_class(self): # Check that we don't have recursive attribute access for metaclass - cls = test_utils.extract_node(''' + cls = builder.extract_node(''' class A(object): pass ''') self.assertEqual(len(cls.getattr('mro')), 1) @@ -1499,13 +1499,13 @@ class ClassNodeTest(ModuleLoader, unittest.TestCase): @test_utils.require_version(maxver='3.0') def test_implicit_metaclass_is_none(self): - cls = test_utils.extract_node(""" + cls = builder.extract_node(""" class A: pass """) self.assertIsNone(cls.implicit_metaclass()) def test_local_attr_invalid_mro(self): - cls = test_utils.extract_node(""" + cls = builder.extract_node(""" # A has an invalid MRO, local_attr should fallback # to using .ancestors. class A(object, object): @@ -1554,7 +1554,7 @@ class ClassNodeTest(ModuleLoader, unittest.TestCase): self.assertRaises(DuplicateBasesError, module['B'].mro) def test_instance_bound_method_lambdas(self): - ast_nodes = test_utils.extract_node(''' + ast_nodes = builder.extract_node(''' class Test(object): #@ lam = lambda self: self not_method = lambda xargs: xargs @@ -1571,7 +1571,7 @@ class ClassNodeTest(ModuleLoader, unittest.TestCase): self.assertIsInstance(not_method, scoped_nodes.Lambda) def test_class_extra_decorators_frame_is_not_class(self): - ast_node = test_utils.extract_node(''' + ast_node = builder.extract_node(''' def ala(): def bala(): #@ func = 42 @@ -1579,7 +1579,7 @@ class ClassNodeTest(ModuleLoader, unittest.TestCase): self.assertEqual(ast_node.extra_decorators, []) def test_class_extra_decorators_only_callfunc_are_considered(self): - ast_node = test_utils.extract_node(''' + ast_node = builder.extract_node(''' class Ala(object): def func(self): #@ pass @@ -1588,7 +1588,7 @@ class ClassNodeTest(ModuleLoader, unittest.TestCase): self.assertEqual(ast_node.extra_decorators, []) def test_class_extra_decorators_only_assignment_names_are_considered(self): - ast_node = test_utils.extract_node(''' + ast_node = builder.extract_node(''' class Ala(object): def func(self): #@ pass @@ -1599,7 +1599,7 @@ class ClassNodeTest(ModuleLoader, unittest.TestCase): self.assertEqual(ast_node.extra_decorators, []) def test_class_extra_decorators_only_same_name_considered(self): - ast_node = test_utils.extract_node(''' + ast_node = builder.extract_node(''' class Ala(object): def func(self): #@ pass @@ -1609,7 +1609,7 @@ class ClassNodeTest(ModuleLoader, unittest.TestCase): self.assertEqual(ast_node.type, 'method') def test_class_extra_decorators(self): - static_method, clsmethod = test_utils.extract_node(''' + static_method, clsmethod = builder.extract_node(''' class Ala(object): def static(self): #@ pass @@ -1624,7 +1624,7 @@ class ClassNodeTest(ModuleLoader, unittest.TestCase): self.assertEqual(static_method.type, 'staticmethod') def test_extra_decorators_only_class_level_assignments(self): - node = test_utils.extract_node(''' + node = builder.extract_node(''' def _bind(arg): return arg.bind @@ -1658,7 +1658,7 @@ class CallSiteTest(unittest.TestCase): return scoped_nodes.CallSite(func, call.args, keywords) def _call_site_from_code(self, code): - call = test_utils.extract_node(code) + call = builder.extract_node(code) return self._inferred_call_site_from_call(call) def _inferred_call_site_from_call(self, call): @@ -1666,7 +1666,7 @@ class CallSiteTest(unittest.TestCase): return self._call_site_from_call(call, inferred) def _test_call_site_pair(self, code, expected_args, expected_keywords): - ast_node = test_utils.extract_node(code) + ast_node = builder.extract_node(code) call_site = self._call_site_from_call(ast_node, ast_node.func) self.assertEqual(len(call_site.positional_arguments), len(expected_args)) self.assertEqual([arg.value for arg in call_site.positional_arguments], @@ -1716,7 +1716,7 @@ class CallSiteTest(unittest.TestCase): def _test_call_site_valid_arguments(self, values, invalid): for value in values: - ast_node = test_utils.extract_node(value) + ast_node = builder.extract_node(value) call_site = self._call_site_from_call(ast_node, ast_node.func) self.assertEqual(call_site.has_invalid_arguments(), invalid) @@ -1731,7 +1731,7 @@ class CallSiteTest(unittest.TestCase): self._test_call_site_valid_arguments(values, invalid=False) def test_duplicated_keyword_arguments(self): - ast_node = test_utils.extract_node('f(f=24, **{"f": 25})') + ast_node = builder.extract_node('f(f=24, **{"f": 25})') site = self._call_site_from_call(ast_node, ast_node.func) self.assertIn('f', site.duplicated_keywords) diff --git a/astroid/tests/unittest_utils.py b/astroid/tests/unittest_utils.py index 8cef049c..2282575f 100644 --- a/astroid/tests/unittest_utils.py +++ b/astroid/tests/unittest_utils.py @@ -7,7 +7,6 @@ from astroid import builder from astroid import InferenceError from astroid import nodes from astroid.interpreter import util -from astroid import test_utils from astroid import util as astroid_util @@ -86,7 +85,7 @@ class InferenceUtil(unittest.TestCase): self.assertEqual(util.are_exclusive(f4, f2), True) def test_unpack_infer_uninferable_nodes(self): - node = test_utils.extract_node(''' + node = builder.extract_node(''' x = [A] * 1 f = [x, [A] * 2] f @@ -98,7 +97,7 @@ class InferenceUtil(unittest.TestCase): for elt in unpacked)) def test_unpack_infer_empty_tuple(self): - node = test_utils.extract_node(''' + node = builder.extract_node(''' () ''') inferred = next(node.infer()) |
