1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326
|
import os
import pytest
from typed_ast import _ast3
from typed_ast import _ast27
import typed_ast.conversions
# Lowest and highest supported Python 3 minor version (inclusive)
MIN_VER = 4
MAX_VER = 7
NEXT_VER = MAX_VER + 1
basics = """\
def foo():
# type: () -> int
pass
def bar(): # type: () -> None
pass
"""
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"
redundantdef = """\
def foo(): # type: () -> int
# type: () -> str
return ''
"""
def test_redundantdef():
for version in range(MIN_VER, NEXT_VER):
with pytest.raises(SyntaxError):
t = _ast3._parse(redundantdef, "<redundantdef>", "exec", version)
vardecl = """\
a = 0 # type: int
a # type: 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"
# Curious fact: an expression can have a type comment
# but it is lost in the AST.
forstmt = """\
for a in []: # type: int
pass
"""
def test_forstmt():
for version in range(MIN_VER, NEXT_VER):
tree = _ast3._parse(forstmt, "<forstmt>", "exec", version)
assert tree.body[0].type_comment == "int"
withstmt = """\
with context(): # type: int
pass
"""
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"
# A test function named 'fabvk' would have two positional args, a and b,
# plus a var-arg *v, plus a kw-arg **k. It is verified in test_longargs()
# that it has exactly these arguments, no more, no fewer.
longargs = """\
def fa(
a = 1, # type: A
):
pass
def fa(
a = 1 # type: A
):
pass
def fab(
a, # type: A
b, # type: B
):
pass
def fab(
a, # type: A
b # type: B
):
pass
def fv(
*v, # type: V
):
pass
def fv(
*v # type: V
):
pass
def fk(
**k, # type: K
):
pass
def fk(
**k # type: K
):
pass
def fvk(
*v, # type: V
**k, # type: K
):
pass
def fvk(
*v, # type: V
**k # type: K
):
pass
def fav(
a, # type: A
*v, # type: V
):
pass
def fav(
a, # type: A
*v # type: V
):
pass
def fak(
a, # type: A
**k, # type: K
):
pass
def fak(
a, # type: A
**k # type: K
):
pass
def favk(
a, # type: A
*v, # type: V
**k, # type: K
):
pass
def favk(
a, # type: A
*v, # type: V
**k # type: K
):
pass
"""
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
ignores = """\
def foo():
pass # type: ignore
def bar():
x = 1 # type: ignore
def baz():
pass # type: ignore[excuse]
pass # type: ignore=excuse
pass # type: ignore [excuse]
x = 1 # type: ignore whatever
"""
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")
asyncfunc = """\
async def foo():
# type: () -> int
return await bar()
"""
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"
asyncvar = """\
async = 12
await = 13
"""
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)
asynccomp = """\
async def foo(xs):
[x async for x in xs]
"""
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)
matmul = """\
a = b @ c
"""
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)
strkind = """\
plain = 'abc'
raw = r'abc'
plain_bytes = b'abc'
raw_bytes = br'abc'
"""
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"
basic_py2 = """\
a = 'hello'
b = u'hello'
c = b'hello'
"""
def test_convert_strs():
ast = _ast27.parse(basic_py2, "<basic_py2>", "exec")
tree = typed_ast.conversions.py2to3(ast)
assert tree.body[0].value.kind == ""
assert tree.body[1].value.kind == "u"
assert tree.body[2].value.kind == "b"
simple_fstring = """\
f'{5}'
"""
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)
# Test the interaction between versions and f strings
await_fstring = """\
f'1 + {f"{await}"}'
"""
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)
|