Skip to content

Commit f0e0fb2

Browse files
authored
support displaying ast types (#125)
* support displaying ast types * support 3.7 & 3.8
1 parent 3d5ff65 commit f0e0fb2

File tree

4 files changed

+42
-2
lines changed

4 files changed

+42
-2
lines changed

devtools/prettier.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import ast
12
import io
23
import os
34
from collections import OrderedDict
@@ -80,6 +81,7 @@ def __init__(
8081
(bytearray, self._format_bytearray),
8182
(generator_types, self._format_generator),
8283
# put these last as the check can be slow
84+
(ast.AST, self._format_ast_expression),
8385
(LaxMapping, self._format_dict),
8486
(DataClassType, self._format_dataclass),
8587
(SQLAlchemyClassType, self._format_sqlalchemy_class),
@@ -240,6 +242,17 @@ def _format_bytearray(self, value: 'Any', _: str, indent_current: int, indent_ne
240242
lines = self._wrap_lines(bytes(value), indent_new)
241243
self._str_lines(lines, indent_current, indent_new)
242244

245+
def _format_ast_expression(self, value: ast.AST, _: str, indent_current: int, indent_new: int) -> None:
246+
try:
247+
s = ast.dump(value, indent=self._indent_step)
248+
except TypeError:
249+
# no indent before 3.9
250+
s = ast.dump(value)
251+
lines = s.splitlines(True)
252+
self._stream.write(lines[0])
253+
for line in lines[1:]:
254+
self._stream.write(indent_current * self._c + line)
255+
243256
def _format_dataclass(self, value: 'Any', _: str, indent_current: int, indent_new: int) -> None:
244257
self._format_fields(value, value.__dict__.items(), indent_current, indent_new)
245258

requirements/testing.in

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ pytest-mock
55
pytest-pretty
66
# these packages are used in tests so install the latest version
77
pydantic
8-
asyncpg
8+
# no binaries for 3.7
9+
asyncpg; python_version>='3.8'
10+
# no version is compatible with 3.7 and 3.11
911
numpy; python_version>='3.8'
1012
multidict; python_version>='3.8'
1113
sqlalchemy

requirements/testing.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
#
55
# pip-compile --output-file=requirements/testing.txt --resolver=backtracking requirements/testing.in
66
#
7-
asyncpg==0.27.0
7+
asyncpg==0.27.0 ; python_version >= "3.8"
88
# via -r requirements/testing.in
99
attrs==22.2.0
1010
# via pytest

tests/test_prettier.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
import ast
12
import os
23
import string
4+
import sys
35
from collections import Counter, OrderedDict, namedtuple
46
from dataclasses import dataclass
57
from typing import List
@@ -457,3 +459,26 @@ class User(SQLAlchemyBase):
457459
" nickname='test',\n"
458460
")"
459461
)
462+
463+
464+
@pytest.mark.skipif(sys.version_info < (3, 9), reason='no indent on older versions')
465+
def test_ast_expr():
466+
assert pformat(ast.parse('print(1, 2, round(3))', mode='eval')) == (
467+
"Expression("
468+
"\n body=Call("
469+
"\n func=Name(id='print', ctx=Load()),"
470+
"\n args=["
471+
"\n Constant(value=1),"
472+
"\n Constant(value=2),"
473+
"\n Call("
474+
"\n func=Name(id='round', ctx=Load()),"
475+
"\n args=["
476+
"\n Constant(value=3)],"
477+
"\n keywords=[])],"
478+
"\n keywords=[]))"
479+
)
480+
481+
482+
@pytest.mark.skipif(sys.version_info < (3, 9), reason='no indent on older versions')
483+
def test_ast_module():
484+
assert pformat(ast.parse('print(1, 2, round(3))')).startswith('Module(\n body=[')

0 commit comments

Comments
 (0)