11"""
22AST analyzer for `ast.expr` records.
33"""
4- from typing import Dict , List , Type
54
65import handsdown .ast_parser .smart_ast as ast
76from handsdown .ast_parser .analyzers .base_analyzer import BaseAnalyzer
@@ -20,13 +19,13 @@ class ExpressionAnalyzer(BaseAnalyzer):
2019 def __init__ (self ) -> None :
2120 super ().__init__ ()
2221 self ._logger = get_logger ()
23- self .parts : List [Node ] = []
22+ self .parts : list [Node ] = []
2423
2524 # dummy value to replace unknown nodes and operators
2625 UNKNOWN = "..."
2726
2827 # representation map for binary operators
29- BINOP_SYMBOLS : Dict [ Type [ast .AST ], str ] = {
28+ BINOP_SYMBOLS : dict [ type [ast .AST ], str ] = {
3029 ast .Add : "+" ,
3130 ast .Sub : "-" ,
3231 ast .Mult : "*" ,
@@ -42,10 +41,10 @@ def __init__(self) -> None:
4241 }
4342
4443 # representation map for boolean operators
45- BOOLOP_SYMBOLS : Dict [ Type [ast .AST ], str ] = {ast .And : "and" , ast .Or : "or" }
44+ BOOLOP_SYMBOLS : dict [ type [ast .AST ], str ] = {ast .And : "and" , ast .Or : "or" }
4645
4746 # representation map for comparison operators
48- CMPOP_SYMBOLS : Dict [ Type [ast .AST ], str ] = {
47+ CMPOP_SYMBOLS : dict [ type [ast .AST ], str ] = {
4948 ast .Eq : "==" ,
5049 ast .NotEq : "!=" ,
5150 ast .Lt : "<" ,
@@ -59,14 +58,14 @@ def __init__(self) -> None:
5958 }
6059
6160 # representation map for unary operators
62- UNARYOP_SYMBOLS : Dict [ Type [ast .AST ], str ] = {
61+ UNARYOP_SYMBOLS : dict [ type [ast .AST ], str ] = {
6362 ast .Invert : "~" ,
6463 ast .Not : "not" ,
6564 ast .UAdd : "+" ,
6665 ast .USub : "-" ,
6766 }
6867
69- def visit_Str (self , node : ast .Str ) -> None :
68+ def visit_Constant (self , node : ast .Constant ) -> None :
7069 """
7170 Parse info from `ast.Str` node and put it to `parts`.
7271
@@ -77,40 +76,11 @@ def visit_Str(self, node: ast.Str) -> None:
7776 Arguments:
7877 node -- AST node.
7978 """
80- value = node .s
79+ value = node .value
8180 if isinstance (value , bytes ):
8281 value = value .decode (ENCODING )
8382 self .parts .append (repr (value ))
8483
85- def visit_Bytes (self , node : ast .Bytes ) -> None :
86- """
87- Parse info from `ast.Bytes` node and put it to `parts`.
88-
89- Examples::
90-
91- b"my_string"
92-
93- Arguments:
94- node -- AST node.
95- """
96- value = node .s
97- self .parts .append (repr (value ))
98-
99- def visit_Num (self , node : ast .Num ) -> None :
100- """
101- Parse info from `ast.Num` node and put it to `parts`.
102-
103- Examples::
104-
105- 123
106- 123.456
107-
108- Arguments:
109- node -- AST node.
110- """
111- value = node .n
112- self .parts .append (repr (value ))
113-
11484 def visit_Name (self , node : ast .Name ) -> None :
11585 """
11686 Parse info from `ast.Name` node and put it to `parts`.
@@ -125,20 +95,6 @@ def visit_Name(self, node: ast.Name) -> None:
12595 self .parts .append (node .id )
12696 self .related_names .append (node .id )
12797
128- def visit_NameConstant (self , node : ast .NameConstant ) -> None :
129- """
130- Parse info from `ast.NameConstant` node and put it to `parts`.
131-
132- Examples::
133-
134- None
135- True
136-
137- Arguments:
138- node -- AST node.
139- """
140- self .parts .append (repr (node .value ))
141-
14298 def visit_Subscript (self , node : ast .Subscript ) -> None :
14399 """
144100 Parse info from `ast.Subscript` node and put it to `parts`.
@@ -522,19 +478,6 @@ def visit_Index(self, node: ast.Index) -> None:
522478 return
523479 self .parts .append (node .value ) # type: ignore
524480
525- def visit_Ellipsis (self , node : ast .ASTEllipsis ) -> None :
526- """
527- Parse info from `ast.Ellipsis` node and put it to `parts`.
528-
529- Examples::
530-
531- ...
532-
533- Arguments:
534- node -- AST node.
535- """
536- self .parts .append ("..." )
537-
538481 def visit_Slice (self , node : ast .Slice ) -> None :
539482 """
540483 Parse info from `ast.Slice` node and put it to `parts`.
@@ -572,8 +515,8 @@ def visit_JoinedStr(self, node: ast.JoinedStr) -> None:
572515 """
573516 self .parts .append ("f'" )
574517 for value in node .values :
575- if isinstance (value , ( ast .Str , ast . Constant ) ):
576- str_value = value .s
518+ if isinstance (value , ast .Constant ):
519+ str_value = value .value
577520 if isinstance (str_value , bytes ):
578521 str_value = str_value .decode (ENCODING )
579522 self .parts .append (str_value )
@@ -726,7 +669,7 @@ def visit_Yield(self, node: ast.Yield) -> None:
726669
727670 Examples::
728671
729- yield
672+ Yield:
730673 yield value
731674
732675 Arguments:
0 commit comments