|
| 1 | +import ast |
1 | 2 | import io |
2 | 3 | import os |
3 | 4 | from collections import OrderedDict |
@@ -80,6 +81,7 @@ def __init__( |
80 | 81 | (bytearray, self._format_bytearray), |
81 | 82 | (generator_types, self._format_generator), |
82 | 83 | # put these last as the check can be slow |
| 84 | + (ast.AST, self._format_ast_expression), |
83 | 85 | (LaxMapping, self._format_dict), |
84 | 86 | (DataClassType, self._format_dataclass), |
85 | 87 | (SQLAlchemyClassType, self._format_sqlalchemy_class), |
@@ -240,6 +242,17 @@ def _format_bytearray(self, value: 'Any', _: str, indent_current: int, indent_ne |
240 | 242 | lines = self._wrap_lines(bytes(value), indent_new) |
241 | 243 | self._str_lines(lines, indent_current, indent_new) |
242 | 244 |
|
| 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 | + |
243 | 256 | def _format_dataclass(self, value: 'Any', _: str, indent_current: int, indent_new: int) -> None: |
244 | 257 | self._format_fields(value, value.__dict__.items(), indent_current, indent_new) |
245 | 258 |
|
|
0 commit comments