How to use the astroid.builder.extract_node function in astroid

To help you get started, weā€™ve selected a few astroid examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github PyCQA / astroid / tests / unittest_nodes.py View on Github external
def test_async_await_keywords(self):
        async_def, async_for, async_with, await_node = builder.extract_node(
            """
        async def func(): #@
            async for i in range(10): #@
                f = __(await i)
            async with test(): #@
                pass
        """
        )
        self.assertIsInstance(async_def, nodes.AsyncFunctionDef)
        self.assertIsInstance(async_for, nodes.AsyncFor)
        self.assertIsInstance(async_with, nodes.AsyncWith)
        self.assertIsInstance(await_node, nodes.Await)
        self.assertIsInstance(await_node.value, nodes.Name)
github PyCQA / astroid / tests / unittest_scoped_nodes.py View on Github external
def test_no_infinite_metaclass_loop(self):
        klass = builder.extract_node(
            """
            class SSS(object):

                class JJJ(object):
                    pass

                @classmethod
                def Init(cls):
                    cls.JJJ = type('JJJ', (cls.JJJ,), {})

            class AAA(SSS):
                pass

            class BBB(AAA.JJJ):
                pass
        """
github PyCQA / astroid / tests / unittest_brain.py View on Github external
def test_namedtuple_class_form(self):
        result = builder.extract_node(
            """
        from typing import NamedTuple

        class Example(NamedTuple):
            CLASS_ATTR = "class_attr"
            mything: int

        Example(mything=1)
        """
        )
        inferred = next(result.infer())
        self.assertIsInstance(inferred, astroid.Instance)

        class_attr = inferred.getattr("CLASS_ATTR")[0]
        self.assertIsInstance(class_attr, astroid.AssignName)
        const = next(class_attr.infer())
github PyCQA / astroid / tests / unittest_brain_numpy_core_umath.py View on Github external
def _inferred_numpy_attribute(self, func_name):
        node = builder.extract_node(
            """
        import numpy.core.umath as tested_module
        func = tested_module.{:s}
        func""".format(
                func_name
            )
        )
        return next(node.infer())
github PyCQA / astroid / tests / unittest_brain_numpy_core_multiarray.py View on Github external
def _inferred_numpy_func_call(self, func_name, *func_args):
        node = builder.extract_node(
            """
        import numpy as np
        func = np.{:s}
        func({:s})
        """.format(
                func_name, ",".join(func_args)
            )
        )
        return node.infer()
github PyCQA / astroid / tests / unittest_scoped_nodes.py View on Github external
def test_instance_bound_method_lambdas_2(self):
        """
        Test the fact that a method which is a lambda built from
        a factory is well inferred as a bound method (bug pylint 2594)
        """
        ast_nodes = builder.extract_node(
            """
        def lambda_factory():
            return lambda self: print("Hello world")

        class MyClass(object): #@
            f2 = lambda_factory()

        MyClass() #@
        """
        )
        cls = next(ast_nodes[0].infer())
        self.assertIsInstance(next(cls.igetattr("f2")), scoped_nodes.Lambda)

        instance = next(ast_nodes[1].infer())
        f2 = next(instance.igetattr("f2"))
        self.assertIsInstance(f2, BoundMethod)
github PyCQA / astroid / tests / unittest_brain.py View on Github external
def test_namedtuple_func_form_args_and_kwargs(self):
        node = builder.extract_node(
            """
        from collections import namedtuple
        Tuple = namedtuple("Tuple", field_names="a b c", rename=UNINFERABLE)
        Tuple #@
        """
        )
        inferred = next(node.infer())
        self.assertEqual(inferred.name, "Tuple")
        self.assertIn("a", inferred.locals)
        self.assertIn("b", inferred.locals)
        self.assertIn("c", inferred.locals)
github PyCQA / astroid / tests / unittest_scoped_nodes.py View on Github external
def test_local_attr_invalid_mro(self):
        cls = builder.extract_node(
            """
        # A has an invalid MRO, local_attr should fallback
        # to using .ancestors.
        class A(object, object):
            test = 42
        class B(A): #@
            pass
        """
        )
        local = cls.local_attr("test")[0]
        inferred = next(local.infer())
        self.assertIsInstance(inferred, nodes.Const)
        self.assertEqual(inferred.value, 42)
github PyCQA / astroid / tests / unittest_builder.py View on Github external
def test_not_implemented(self):
        node = builder.extract_node(
            """
        NotImplemented #@
        """
        )
        inferred = next(node.infer())
        self.assertIsInstance(inferred, nodes.Const)
        self.assertEqual(inferred.value, NotImplemented)