Skip to content

Commit 01ccdae

Browse files
committed
drop typed-ast and require python3.8
1 parent 5b68c7e commit 01ccdae

6 files changed

+16
-150
lines changed

astpretty.py

+8-46
Original file line numberDiff line numberDiff line change
@@ -3,36 +3,19 @@
33
import argparse
44
import ast
55
import contextlib
6-
import sys
76
from typing import Any
87
from typing import Generator
98
from typing import Sequence
10-
from typing import TYPE_CHECKING
11-
from typing import Union
12-
13-
if TYPE_CHECKING:
14-
from typed_ast import ast27
15-
from typed_ast import ast3
16-
ASTType = Union[ast.AST, ast27.AST, ast3.AST]
179

1810
AST: tuple[type[Any], ...] = (ast.AST,)
1911
expr_context: tuple[type[Any], ...] = (ast.expr_context,)
20-
try: # pragma: no cover (with typed-ast)
21-
from typed_ast import ast27
22-
from typed_ast import ast3
23-
except ImportError: # pragma: no cover (without typed-ast)
24-
typed_support = False
25-
else: # pragma: no cover (with typed-ast)
26-
AST += (ast27.AST, ast3.AST)
27-
expr_context += (ast27.expr_context, ast3.expr_context)
28-
typed_support = True
2912

3013

3114
def _is_sub_node(node: object) -> bool:
3215
return isinstance(node, AST) and not isinstance(node, expr_context)
3316

3417

35-
def _is_leaf(node: ASTType) -> bool:
18+
def _is_leaf(node: ast.AST) -> bool:
3619
for field in node._fields:
3720
attr = getattr(node, field)
3821
if _is_sub_node(attr):
@@ -45,14 +28,14 @@ def _is_leaf(node: ASTType) -> bool:
4528
return True
4629

4730

48-
def _fields(n: ASTType, show_offsets: bool = True) -> tuple[str, ...]:
31+
def _fields(n: ast.AST, show_offsets: bool = True) -> tuple[str, ...]:
4932
if show_offsets:
5033
return n._attributes + n._fields
5134
else:
5235
return n._fields
5336

5437

55-
def _leaf(node: ASTType, show_offsets: bool = True) -> str:
38+
def _leaf(node: ast.AST, show_offsets: bool = True) -> str:
5639
if isinstance(node, AST):
5740
return '{}({})'.format(
5841
type(node).__name__,
@@ -73,7 +56,7 @@ def _leaf(node: ASTType, show_offsets: bool = True) -> str:
7356

7457

7558
def pformat(
76-
node: ASTType | None | str,
59+
node: ast.AST | None | str,
7760
indent: str | int = ' ',
7861
show_offsets: bool = True,
7962
_indent: int = 0,
@@ -102,7 +85,7 @@ def indented() -> Generator[None, None, None]:
10285
def indentstr() -> str:
10386
return state.indent * indent_s
10487

105-
def _pformat(el: ASTType | None | str, _indent: int = 0) -> str:
88+
def _pformat(el: ast.AST | None | str, _indent: int = 0) -> str:
10689
return pformat(
10790
el, indent=indent, show_offsets=show_offsets,
10891
_indent=_indent,
@@ -149,34 +132,13 @@ def main(argv: Sequence[str] | None = None) -> int:
149132
'--no-show-offsets', dest='show_offsets',
150133
action='store_false',
151134
)
152-
grp = parser.add_mutually_exclusive_group()
153-
grp.add_argument(
154-
'--untyped', default=ast.parse,
155-
dest='parse_func', action='store_const', const=ast.parse,
156-
help='(default) Use the stdlib `ast` parser.',
157-
)
158-
if typed_support: # pragma: no cover (requires typed-ast)
159-
grp.add_argument(
160-
'--typed-27',
161-
dest='parse_func', action='store_const', const=ast27.parse,
162-
help='Use typed_ast.ast27 to parse the ast.',
163-
)
164-
grp.add_argument(
165-
'--typed-3',
166-
dest='parse_func', action='store_const', const=ast3.parse,
167-
help='Use typed_ast.ast3 to parse the ast.',
168-
)
169135
args = parser.parse_args(argv)
170136

171-
type_comments = args.parse_func is ast.parse and sys.version_info >= (3, 8)
172-
if type_comments: # pragma: >=3.8 cover
173-
kwargs = {'type_comments': True}
174-
else: # pragma: <3.8 cover
175-
kwargs = {}
176-
177137
with open(args.filename, 'rb') as f:
178138
contents = f.read()
179-
pprint(args.parse_func(contents, **kwargs), show_offsets=args.show_offsets)
139+
140+
tree = ast.parse(contents, filename=args.filename, type_comments=True)
141+
pprint(tree, show_offsets=args.show_offsets)
180142
return 0
181143

182144

azure-pipelines.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@ resources:
1010
type: github
1111
endpoint: github
1212
name: asottile/azure-pipeline-templates
13-
ref: refs/tags/v2.4.0
13+
ref: refs/tags/v2.4.1
1414

1515
jobs:
1616
- template: job--python-tox.yml@asottile
1717
parameters:
18-
toxenvs: [py37, py38, py39]
18+
toxenvs: [py38, py39, py310]
1919
os: linux

requirements-dev.txt

-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
11
covdefaults>=2.1
22
coverage
33
pytest
4-
typed-ast;implementation_name=="cpython"

setup.cfg

+1-6
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ classifiers =
1313
License :: OSI Approved :: MIT License
1414
Programming Language :: Python :: 3
1515
Programming Language :: Python :: 3 :: Only
16-
Programming Language :: Python :: 3.7
1716
Programming Language :: Python :: 3.8
1817
Programming Language :: Python :: 3.9
1918
Programming Language :: Python :: 3.10
@@ -22,16 +21,12 @@ classifiers =
2221

2322
[options]
2423
py_modules = astpretty
25-
python_requires = >=3.7
24+
python_requires = >=3.8
2625

2726
[options.entry_points]
2827
console_scripts =
2928
astpretty = astpretty:main
3029

31-
[options.extras_require]
32-
typed =
33-
typed-ast
34-
3530
[bdist_wheel]
3631
universal = True
3732

tests/astpretty_test.py

+4-94
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ def test_pformat_node():
5353

5454

5555
def test_pformat_nested_with_offsets():
56-
expected_38 = (
56+
expected = (
5757
'Assign(\n'
5858
' lineno=1,\n'
5959
' col_offset=0,\n'
@@ -64,15 +64,6 @@ def test_pformat_nested_with_offsets():
6464
' type_comment=None,\n'
6565
')'
6666
)
67-
expected_lt38 = (
68-
'Assign(\n'
69-
' lineno=1,\n'
70-
' col_offset=0,\n'
71-
" targets=[Name(lineno=1, col_offset=0, id='x', ctx=Store())],\n"
72-
' value=Num(lineno=1, col_offset=4, n=5),\n'
73-
')'
74-
)
75-
expected = expected_38 if sys.version_info >= (3, 8) else expected_lt38
7667
ret = astpretty.pformat(_to_module_body('x = 5'))
7768
assert ret == expected
7869

@@ -169,22 +160,7 @@ def test_pformat_nested_node_without_line_information():
169160
' ctx=Load(),\n'
170161
')'
171162
)
172-
expected_lt38 = (
173-
'Subscript(\n'
174-
' lineno=1,\n'
175-
' col_offset=0,\n'
176-
" value=Name(lineno=1, col_offset=0, id='a', ctx=Load()),\n"
177-
' slice=Index(\n'
178-
' value=Num(lineno=1, col_offset=2, n=0),\n'
179-
' ),\n'
180-
' ctx=Load(),\n'
181-
')'
182-
)
183-
expected = (
184-
expected_39 if sys.version_info >= (3, 9) else
185-
expected_38 if sys.version_info >= (3, 8) else
186-
expected_lt38
187-
)
163+
expected = expected_39 if sys.version_info >= (3, 9) else expected_38
188164
ret = astpretty.pformat(_to_expr_value('a[0]'))
189165
assert ret == expected
190166

@@ -201,7 +177,7 @@ def test_pprint(capsys):
201177

202178

203179
def test_main_with_offsets(capsys, tmpdir):
204-
expected_38 = '''\
180+
expected = '''\
205181
Module(
206182
body=[
207183
Assign(
@@ -217,19 +193,6 @@ def test_main_with_offsets(capsys, tmpdir):
217193
type_ignores=[],
218194
)
219195
''' # noqa: E501
220-
expected_lt38 = '''\
221-
Module(
222-
body=[
223-
Assign(
224-
lineno=1,
225-
col_offset=0,
226-
targets=[Name(lineno=1, col_offset=0, id='x', ctx=Store())],
227-
value=Name(lineno=1, col_offset=4, id='y', ctx=Load()),
228-
),
229-
],
230-
)
231-
'''
232-
expected = expected_38 if sys.version_info >= (3, 8) else expected_lt38
233196
f = tmpdir.join('test.py')
234197
f.write('x = y\n')
235198
astpretty.main((f.strpath,))
@@ -238,7 +201,7 @@ def test_main_with_offsets(capsys, tmpdir):
238201

239202

240203
def test_main_hide_offsets(capsys, tmpdir):
241-
expected_38 = '''\
204+
expected = '''\
242205
Module(
243206
body=[
244207
Assign(
@@ -250,29 +213,13 @@ def test_main_hide_offsets(capsys, tmpdir):
250213
type_ignores=[],
251214
)
252215
'''
253-
expected_lt38 = '''\
254-
Module(
255-
body=[
256-
Assign(
257-
targets=[Name(id='x', ctx=Store())],
258-
value=Name(id='y', ctx=Load()),
259-
),
260-
],
261-
)
262-
'''
263-
expected = expected_38 if sys.version_info >= (3, 8) else expected_lt38
264216
f = tmpdir.join('test.py')
265217
f.write('x = y\n')
266218
astpretty.main((f.strpath, '--no-show-offsets'))
267219
out, _ = capsys.readouterr()
268220
assert out == expected
269221

270222

271-
def test_typedast_support():
272-
typed = not hasattr(sys, 'pypy_version_info')
273-
assert typed == astpretty.typed_support
274-
275-
276223
TYPED_SRC = 'x = 5 # type: int\nx = "foo" # type: ignore\n'
277224
TYPED27_OUT = '''\
278225
Module(
@@ -354,43 +301,6 @@ def f(
354301
'''
355302

356303

357-
@pytest.mark.xfail(not astpretty.typed_support, reason='needs typed-ast')
358-
def test_typedast_support_27():
359-
expected = TYPED27_OUT.rstrip()
360-
assert astpretty.pformat(astpretty.ast27.parse(TYPED_SRC)) == expected
361-
362-
363-
@pytest.mark.xfail(not astpretty.typed_support, reason='needs typed-ast')
364-
def test_typedast_support_3():
365-
expected = TYPED3_OUT.rstrip()
366-
assert astpretty.pformat(astpretty.ast3.parse(TYPED_SRC)) == expected
367-
368-
369-
@pytest.mark.xfail(not astpretty.typed_support, reason='needs typed-ast')
370-
def test_typedast_ast27_arguments():
371-
expected = FUNC_SRC_TYPED27_OUT.strip()
372-
assert astpretty.pformat(astpretty.ast27.parse(FUNC_SRC)) == expected
373-
374-
375-
@pytest.mark.xfail(not astpretty.typed_support, reason='needs typed-ast')
376-
def test_typedast_support_cmdline_27(tmpdir, capsys): # pragma: no cover
377-
f = tmpdir.join('f.py')
378-
f.write(TYPED_SRC)
379-
assert not astpretty.main((str(f), '--typed-27'))
380-
out, _ = capsys.readouterr()
381-
assert out == TYPED27_OUT
382-
383-
384-
@pytest.mark.xfail(not astpretty.typed_support, reason='needs typed-ast')
385-
def test_typedast_support_cmdline_3(tmpdir, capsys): # pragma: no cover
386-
f = tmpdir.join('f.py')
387-
f.write(TYPED_SRC)
388-
assert not astpretty.main((str(f), '--typed-3'))
389-
out, _ = capsys.readouterr()
390-
assert out == TYPED3_OUT
391-
392-
393-
@pytest.mark.xfail(sys.version_info < (3, 8), reason='py38+ syntax only')
394304
def test_pformat_py38_type_comments(tmpdir, capsys):
395305
expected = '''\
396306
Module(

tox.ini

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[tox]
2-
envlist = py37,py38,pypy3,pre-commit
2+
envlist = py38,pypy3,pre-commit
33

44
[testenv]
55
deps = -rrequirements-dev.txt

0 commit comments

Comments
 (0)