-
-
Notifications
You must be signed in to change notification settings - Fork 391
/
Copy pathrouting.py
37 lines (32 loc) · 1.02 KB
/
routing.py
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
import ast
import types
from typing import Final
#: That's how python types and ast types map to each other, copied from ast.
_CONST_NODE_TYPE_NAMES: Final = types.MappingProxyType(
{
bool: 'NameConstant', # should be before int
type(None): 'NameConstant',
int: 'Num',
float: 'Num',
complex: 'Num',
str: 'Str',
bytes: 'Bytes',
type(...): 'Ellipsis',
},
)
def route_visit(self: ast.NodeVisitor, node: ast.AST) -> None:
"""
Custom router for python3.8+ release.
Hacked to make sure that everything we had defined before is working.
"""
if isinstance(node, ast.Constant):
# That's the hack itself, we don't get the name of the node.
# We get the name of wrapped type from it.
type_name = _CONST_NODE_TYPE_NAMES.get(type(node.value))
else:
type_name = node.__class__.__name__
return getattr( # type: ignore[no-any-return]
self,
f'visit_{type_name}',
self.generic_visit,
)(node)