How to use the astroid.builder.parse 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_scoped_nodes.py View on Github external
def test_simple_metaclass(self):
        astroid = builder.parse(
            """
            class Test(object):
                __metaclass__ = type
        """
        )
        klass = astroid["Test"]
        metaclass = klass.metaclass()
        self.assertIsInstance(metaclass, scoped_nodes.ClassDef)
        self.assertEqual(metaclass.name, "type")
github PyCQA / astroid / tests / unittest_scoped_nodes.py View on Github external
def test_nonregr_infer_callresult(self):
        astroid = builder.parse(
            """
            class Delegate(object):
                def __get__(self, obj, cls):
                    return getattr(obj._subject, self.attribute)

            class CompositeBuilder(object):
                __call__ = Delegate()

            builder = CompositeBuilder(result, composite)
            tgts = builder()
        """
        )
        instance = astroid["tgts"]
        # used to raise "'_Yes' object is not iterable", see
        # https://bitbucket.org/logilab/astroid/issue/17
        self.assertEqual(list(instance.infer()), [util.Uninferable])
github PyCQA / astroid / tests / unittest_nodes.py View on Github external
def check_as_string_ast_equality(code):
        """
        Check that as_string produces source code with exactly the same
        semantics as the source it was originally parsed from
        """
        pre = builder.parse(code)
        post = builder.parse(pre.as_string())

        pre_repr = pre.repr_tree()
        post_repr = post.repr_tree()

        assert pre_repr == post_repr
        assert pre.as_string().strip() == code.strip()
github PyCQA / astroid / tests / unittest_builder.py View on Github external
"""
            for a in range(4):
              print (a)
              break
            else:
              print ("bouh")
            """,
            """
            while a:
              print (a)
              break
            else:
              print ("bouh")
            """,
        ):
            astroid = builder.parse(code, __name__)
            stmt = astroid.body[0]
            self.assertEqual(stmt.fromlineno, 2)
            self.assertEqual(stmt.tolineno, 6)
            self.assertEqual(stmt.blockstart_tolineno, 2)
            self.assertEqual(stmt.orelse[0].fromlineno, 6)  # XXX
            self.assertEqual(stmt.orelse[0].tolineno, 6)
github PyCQA / astroid / tests / unittest_scoped_nodes.py View on Github external
def test_kite_graph(self):
        data = """
        A = type('A', (object,), {})

        class B1(A): pass

        class B2(A): pass

        class C(B1, B2): pass

        class D(C):
            def update(self):
                self.hello = 'hello'
        """
        # Should not crash
        builder.parse(data)
github PyCQA / astroid / tests / unittest_brain.py View on Github external
def test_int_enum(self):
        module = builder.parse(
            """
        import enum

        class MyEnum(enum.IntEnum):
            one = 1
        """
        )

        enumeration = next(module["MyEnum"].infer())
        one = enumeration["one"]

        clazz = one.getattr("__class__")[0]
        int_type = "{}.{}".format(bases.BUILTINS, "int")
        self.assertTrue(
            clazz.is_subtype_of(int_type),
            "IntEnum based enums should be a subtype of int",
github Shopify / shopify_python / tests / shopify_python / test_google_styleguide.py View on Github external
def test_unary_lambda_func_with_complex_operand_allowed(self):
        unary_root = astroid.builder.parse("""
        def unaryfnc():
            unary_pass = map(lambda x: not (x+3), [1, 2, 3, 4])
        """)
        with self.assertNoMessages():
            self.walk(unary_root)
github PyCQA / astroid / tests / unittest_brain.py View on Github external
def test_enum_multiple_base_classes(self):
        module = builder.parse(
            """
        import enum

        class Mixin:
            pass

        class MyEnum(Mixin, enum.Enum):
            one = 1
        """
        )
        enumeration = next(module["MyEnum"].infer())
        one = enumeration["one"]

        clazz = one.getattr("__class__")[0]
        self.assertTrue(
            clazz.is_subtype_of(".Mixin"),
github Shopify / shopify_python / tests / shopify_python / test_google_styleguide.py View on Github external
def test_use_simple_list_comps(self):
        root = astroid.builder.parse("""
        def fnc():
            good = [x for x in range(0,10)]
            bad = [(x, y) for x in range(0,10) for y in range(0,10)]
        """)
        fnc = root.body[0]
        bad_list_comp = fnc.body[1].value
        message = pylint.testutils.Message('complex-list-comp', node=bad_list_comp)
        with self.assertAddsMessages(message):
            self.walk(root)
github PyCQA / astroid / tests / unittest_scoped_nodes.py View on Github external
def test_with_metaclass_mro(self):
        astroid = builder.parse(
            """
        import six

        class C(object):
            pass
        class B(C):
            pass
        class A(six.with_metaclass(type, B)):
            pass
        """
        )
        self.assertEqualMro(astroid["A"], ["A", "B", "C", "object"])