Skip to content

Commit 04a7ba2

Browse files
committed
WIP
Change-Id: I845026993de757ef41db064af74a134ba1066f01
1 parent 8f7f5cb commit 04a7ba2

File tree

1 file changed

+32
-20
lines changed

1 file changed

+32
-20
lines changed

tests/test_async_code_match.py

Lines changed: 32 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -73,38 +73,50 @@ def _execute_code_match(self, source, asource):
7373
.replace("ASYNC_", "")
7474
)
7575
asource = re.sub(" *?# type: ignore", "", asource)
76+
if source != asource:
77+
pass
7678
self.assertEqual(source, asource)
7779

7880
def test_code_match_for_async_methods(self):
7981
for fpath in (pathlib.Path(__file__).parent.parent / "google").rglob("*.py"):
82+
print(fpath)
8083
if fpath.name in EXEMPT_FILES or any([d in fpath.parts for d in EXEMPT_DIRS]):
8184
continue
8285
# 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] = {}
8488
source = fpath.read_text()
8589
source_nodes = ast.parse(source)
8690

8791
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)
106113
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
108120

109121

110122
if __name__ == "__main__":

0 commit comments

Comments
 (0)