@@ -73,38 +73,50 @@ def _execute_code_match(self, source, asource):
73
73
.replace ("ASYNC_" , "" )
74
74
)
75
75
asource = re .sub (" *?# type: ignore" , "" , asource )
76
+ if source != asource :
77
+ pass
76
78
self .assertEqual (source , asource )
77
79
78
80
def test_code_match_for_async_methods (self ):
79
81
for fpath in (pathlib .Path (__file__ ).parent .parent / "google" ).rglob ("*.py" ):
82
+ print (fpath )
80
83
if fpath .name in EXEMPT_FILES or any ([d in fpath .parts for d in EXEMPT_DIRS ]):
81
84
continue
82
85
# print(f"Checking {fpath.absolute()}")
83
- code_match_funcs : dict [str , ast .AST ] = {}
86
+ funcs : dict [str , ast .AST ] = {}
87
+ async_funcs : dict [str , ast .AST ] = {}
84
88
source = fpath .read_text ()
85
89
source_nodes = ast .parse (source )
86
90
87
91
for node in ast .walk (source_nodes ):
88
- if isinstance (
89
- node , (ast .FunctionDef , ast .AsyncFunctionDef )
90
- ) and not node .name .startswith ("__" ):
91
- name = node .name [:- 6 ] if node .name .endswith ("_async" ) else node .name
92
- if name in EXEMPT_FUNCTIONS or self ._inspect_decorator_exemption (node , fpath ):
93
- continue
94
- # print(f"Checking {node.name}")
95
-
96
- if func_name := code_match_funcs .pop (name , None ):
97
- snode , anode = (
98
- (func_name , node )
99
- if isinstance (node , ast .AsyncFunctionDef )
100
- else (node , func_name )
101
- )
102
- func_source = self ._maybe_trim_docstring (snode )
103
- func_asource = self ._maybe_trim_docstring (anode )
104
- self ._execute_code_match (func_source , func_asource )
105
- # print(f"Matched {node.name}")
92
+
93
+
94
+ if not isinstance (node , (ast .FunctionDef , ast .AsyncFunctionDef )):
95
+ continue
96
+ if node .name .startswith ("__" ):
97
+ continue
98
+
99
+ if isinstance (node , ast .AsyncFunctionDef ):
100
+ is_async = True
101
+ code_match_funcs = funcs
102
+ else :
103
+ is_async = False
104
+ code_match_funcs = async_funcs
105
+
106
+ name = node .name .removesuffix ("_async" )
107
+ if name in EXEMPT_FUNCTIONS or self ._inspect_decorator_exemption (node , fpath ):
108
+ continue
109
+
110
+ if matched_node := code_match_funcs .pop (name , None ):
111
+ if is_async :
112
+ snode , anode = (matched_node , node )
106
113
else :
107
- code_match_funcs [node .name ] = node
114
+ anode , snode = (matched_node , node )
115
+ func_source = self ._maybe_trim_docstring (snode )
116
+ func_asource = self ._maybe_trim_docstring (anode )
117
+ self ._execute_code_match (func_source , func_asource )
118
+ else :
119
+ code_match_funcs [node .name ] = node
108
120
109
121
110
122
if __name__ == "__main__" :
0 commit comments