def visit_ImportFrom(self, node): from ir import Import for name in node.names: assert name.asname is None, "Cannot yet handle as name" self.value.append(Import(node.module, [x.name for x in node.names]))
def test_import(): ir = transform(""" def test_import(): import foo, bar a = foo.GLOBAL """) assert ir == [ AutowrapTest("test_import", [ Import("foo", []), Import("bar", []), Assignment( 'a', Attribute('foo', 'GLOBAL'), ) ]) ]
def visit_Import(self, node): from ir import Import if any(x.asname is not None for x in node.names): raise Exception("Cannot yet handle as name") modules = [x.name for x in node.names] for module in modules: self.value.append(Import(module, []))
def test_with_raises_exception(): ir = transform(""" def test_raises(): with pytest.raises(ImportError): from bar import foo """) assert ir == [ AutowrapTest("test_raises", [ShouldThrow("ImportError", [Import('bar', ['foo'])])]) ]
def test_import_from(): ir = transform(""" def test_import_from(): from foo import bar, baz """) assert ir == [ AutowrapTest("test_import_from", [ Import("foo", ["bar", "baz"]), ]) ]
def test_assertion_call_function(): ir = transform(""" def test_func(): from mod import func assert func(7) == 42 """) assert ir == [ AutowrapTest("test_func", [ Import('mod', ['func']), Assertion( FunctionCall("func", [NumLiteral(7)]), NumLiteral(42), ), ]) ]
def test_assertion_call_method_on_rvalue(): ir = transform(""" def test_func(): from mod import Klass Klass('ctor_arg').func(42, 3.3) """) assert ir == [ AutowrapTest("test_func", [ Import('mod', ['Klass']), FunctionCall( Attribute( FunctionCall('Klass', [StringLiteral('ctor_arg')]), 'func', ), [NumLiteral(42), NumLiteral(3.3)], ), ]) ]