1
+ from __future__ import annotations
2
+
1
3
import argparse
2
4
import ast
3
5
import contextlib
4
6
import sys
5
7
from typing import Any
6
8
from typing import Generator
7
- from typing import Optional
8
9
from typing import Sequence
9
- from typing import Tuple
10
- from typing import Type
11
10
from typing import TYPE_CHECKING
12
11
from typing import Union
13
12
16
15
from typed_ast import ast3
17
16
ASTType = Union [ast .AST , ast27 .AST , ast3 .AST ]
18
17
19
- AST : Tuple [ Type [Any ], ...] = (ast .AST ,)
20
- expr_context : Tuple [ Type [Any ], ...] = (ast .expr_context ,)
18
+ AST : tuple [ type [Any ], ...] = (ast .AST ,)
19
+ expr_context : tuple [ type [Any ], ...] = (ast .expr_context ,)
21
20
try : # pragma: no cover (with typed-ast)
22
21
from typed_ast import ast27
23
22
from typed_ast import ast3
@@ -33,7 +32,7 @@ def _is_sub_node(node: object) -> bool:
33
32
return isinstance (node , AST ) and not isinstance (node , expr_context )
34
33
35
34
36
- def _is_leaf (node : ' ASTType' ) -> bool :
35
+ def _is_leaf (node : ASTType ) -> bool :
37
36
for field in node ._fields :
38
37
attr = getattr (node , field )
39
38
if _is_sub_node (attr ):
@@ -46,14 +45,14 @@ def _is_leaf(node: 'ASTType') -> bool:
46
45
return True
47
46
48
47
49
- def _fields (n : ' ASTType' , show_offsets : bool = True ) -> Tuple [str , ...]:
48
+ def _fields (n : ASTType , show_offsets : bool = True ) -> tuple [str , ...]:
50
49
if show_offsets :
51
50
return n ._attributes + n ._fields
52
51
else :
53
52
return n ._fields
54
53
55
54
56
- def _leaf (node : ' ASTType' , show_offsets : bool = True ) -> str :
55
+ def _leaf (node : ASTType , show_offsets : bool = True ) -> str :
57
56
if isinstance (node , AST ):
58
57
return '{}({})' .format (
59
58
type (node ).__name__ ,
@@ -74,8 +73,8 @@ def _leaf(node: 'ASTType', show_offsets: bool = True) -> str:
74
73
75
74
76
75
def pformat (
77
- node : Union [ ' ASTType' , None , str ] ,
78
- indent : Union [ str , int ] = ' ' ,
76
+ node : ASTType | None | str ,
77
+ indent : str | int = ' ' ,
79
78
show_offsets : bool = True ,
80
79
_indent : int = 0 ,
81
80
) -> str :
@@ -103,7 +102,7 @@ def indented() -> Generator[None, None, None]:
103
102
def indentstr () -> str :
104
103
return state .indent * indent_s
105
104
106
- def _pformat (el : Union [ ' ASTType' , None , str ] , _indent : int = 0 ) -> str :
105
+ def _pformat (el : ASTType | None | str , _indent : int = 0 ) -> str :
107
106
return pformat (
108
107
el , indent = indent , show_offsets = show_offsets ,
109
108
_indent = _indent ,
@@ -143,7 +142,7 @@ def pprint(*args: Any, **kwargs: Any) -> None:
143
142
print (pformat (* args , ** kwargs ))
144
143
145
144
146
- def main (argv : Optional [ Sequence [str ]] = None ) -> int :
145
+ def main (argv : Sequence [str ] | None = None ) -> int :
147
146
parser = argparse .ArgumentParser ()
148
147
parser .add_argument ('filename' )
149
148
parser .add_argument (
0 commit comments