def test_asyncfunc(): for version in range(3, 5): with pytest.raises(SyntaxError): _ast3._parse(asyncfunc, "<asyncfunc>", "exec", version) for version in range(5, NEXT_VER): tree = _ast3._parse(asyncfunc, "<asyncfunc>", "exec", version) assert tree.body[0].type_comment == "() -> int"
def test_strkind(): # Test that Str() objects have a kind argument/attribute. node = _ast3.Str("foo", "r") assert node.s == "foo" assert node.kind == "r" for version in range(MIN_VER, NEXT_VER): tree = _ast3._parse(strkind, "<strkind>", "exec", version) assert tree.body[0].value.kind == "" assert tree.body[1].value.kind == "r" assert tree.body[2].value.kind == "b" assert tree.body[3].value.kind == "br"
def test_ignores(): expected = [ (2, ''), (5, ''), (8, '[excuse]'), (9, '=excuse'), (10, ' [excuse]'), (11, ' whatever'), ] for version in range(MIN_VER, NEXT_VER): tree = _ast3._parse(ignores, "<ignores>", "exec", version) assert [(ti.lineno, ti.tag) for ti in tree.type_ignores] == expected with pytest.raises(SyntaxError): _ast3._parse("pass # type: ignoreƩ\n", "<ignores>", "exec", version) tree = _ast27.parse(ignores, "<ignores>", "exec") assert [(ti.lineno, ti.tag) for ti in tree.type_ignores] == expected with pytest.raises(SyntaxError): _ast27.parse("pass # type: ignoreƩ\n", "<ignores>", "exec")
def test_longargs(): for version in range(MIN_VER, NEXT_VER): tree = _ast3._parse(longargs, "<longargs>", "exec", version) for t in tree.body: # The expected args are encoded in the function name todo = set(t.name[1:]) assert len(t.args.args) == len(todo) - bool(t.args.vararg) - bool( t.args.kwarg) assert t.name.startswith('f') for c in t.name[1:]: todo.remove(c) if c == 'v': arg = t.args.vararg elif c == 'k': arg = t.args.kwarg else: assert 0 <= ord(c) - ord('a') < len(t.args.args) arg = t.args.args[ord(c) - ord('a')] assert arg.arg == c # That's the argument name assert arg.type_comment == arg.arg.upper() assert not todo
def parse(source, filename='<unknown>', mode='exec', feature_version=LATEST_MINOR_VERSION): """ Parse the source into an AST node including type comments. Similar to compile(source, filename, mode, PyCF_ONLY_AST). Set feature_version to limit the syntax parsed to that minor version of Python 3. For example, feature_version=5 will prevent new syntax features from Python 3.6+ from being used, such as fstrings. Currently only fully supported for Python 3.5+ with partial support for Python 3.4. So, feature_version=3 or less are all equivalent to feature_version=4. When feature_version=4, the parser will forbid the use of the async/await keywords and the '@' operator, but will not forbid the use of PEP 448 additional unpacking generalizations, which were also added in Python 3.5. When feature_version>=7, 'async' and 'await' are always keywords. """ return _ast3._parse(source, filename, mode, feature_version)
def test_withstmt(): for version in range(MIN_VER, NEXT_VER): tree = _ast3._parse(withstmt, "<withstmt>", "exec", version) assert tree.body[0].type_comment == "int"
def test_vardecl(): for version in range(MIN_VER, NEXT_VER): tree = _ast3._parse(vardecl, "<vardecl>", "exec", version) assert tree.body[0].type_comment == "int"
def test_redundantdef(): for version in range(MIN_VER, NEXT_VER): with pytest.raises(SyntaxError): t = _ast3._parse(redundantdef, "<redundantdef>", "exec", version)
def test_await_fstring(): # Should work on 6 but fail on 7 _ast3._parse(await_fstring, "<bad-f-string>", "exec", 6) with pytest.raises(SyntaxError): _ast3._parse(await_fstring, "<bad-f-string>", "exec", 7)
def test_simple_fstring(): for version in range(6, NEXT_VER): tree = _ast3._parse(simple_fstring, "<fstring>", "exec", version) assert isinstance(tree.body[0].value, _ast3.JoinedStr) assert isinstance(tree.body[0].value.values[0].value, _ast3.Num)
def test_matmul(): for version in range(3, 5): with pytest.raises(SyntaxError): tree = _ast3._parse(matmul, "<matmul>", "exec", version) for version in range(5, NEXT_VER): tree = _ast3._parse(matmul, "<matmul>", "exec", version)
def test_asynccomp(): for version in range(3, 6): with pytest.raises(SyntaxError): tree = _ast3._parse(asynccomp, "<asynccomp>", "exec", version) for version in range(6, NEXT_VER): _ast3._parse(asynccomp, "<asynccomp>", "exec", version)
def test_asyncvar(): for version in range(3, 7): tree = _ast3._parse(asyncvar, "<asyncvar>", "exec", version) for version in range(7, NEXT_VER): with pytest.raises(SyntaxError): _ast3._parse(asyncvar, "<asyncvar>", "exec", version)
def test_basics(): for version in range(MIN_VER, NEXT_VER): tree = _ast3._parse(basics, "<basics>", "exec", version) assert tree.body[0].type_comment == "() -> int" assert tree.body[1].type_comment == "() -> None"
def test_ignores(): for version in range(MIN_VER, NEXT_VER): tree = _ast3._parse(ignores, "<ignores>", "exec", version) assert [ti.lineno for ti in tree.type_ignores] == [2, 5]