From 8bb493b4f8cc4e009ffb328b92d639cfcb2c0c28 Mon Sep 17 00:00:00 2001 From: Jelle Zijlstra Date: Sat, 18 May 2024 09:52:14 -0400 Subject: [PATCH 1/3] Fix new typing issues in AST code python/typeshed#11880 adds more precise types for AST nodes. I'm submitting some changes to adapt pytest to these changes. --- src/_pytest/assertion/rewrite.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/_pytest/assertion/rewrite.py b/src/_pytest/assertion/rewrite.py index 928c4f5e3b2..1fe1b42f80f 100644 --- a/src/_pytest/assertion/rewrite.py +++ b/src/_pytest/assertion/rewrite.py @@ -835,7 +835,7 @@ def pop_format_context(self, expl_expr: ast.expr) -> ast.Name: current = self.stack.pop() if self.stack: self.explanation_specifiers = self.stack[-1] - keys = [ast.Constant(key) for key in current.keys()] + keys: List[ast.expr] = [ast.Constant(key) for key in current.keys()] format_dict = ast.Dict(keys, list(current.values())) form = ast.BinOp(expl_expr, ast.Mod(), format_dict) name = "@py_format" + str(next(self.variable_counter)) @@ -926,13 +926,13 @@ def visit_Assert(self, assert_: ast.Assert) -> List[ast.stmt]: [*self.expl_stmts, hook_call_pass], [], ) - statements_pass = [hook_impl_test] + statements_pass: List[ast.stmt] = [hook_impl_test] # Test for assertion condition main_test = ast.If(negation, statements_fail, statements_pass) self.statements.append(main_test) if self.format_variables: - variables = [ + variables: List[ast.expr] = [ ast.Name(name, ast.Store()) for name in self.format_variables ] clear_format = ast.Assign(variables, ast.Constant(None)) @@ -959,7 +959,7 @@ def visit_Assert(self, assert_: ast.Assert) -> List[ast.stmt]: # Clear temporary variables by setting them to None. if self.variables: - variables = [ast.Name(name, ast.Store()) for name in self.variables] + variables: List[ast.expr] = [ast.Name(name, ast.Store()) for name in self.variables] clear = ast.Assign(variables, ast.Constant(None)) self.statements.append(clear) # Fix locations (line numbers/column offsets). @@ -1114,11 +1114,11 @@ def visit_Compare(self, comp: ast.Compare) -> Tuple[ast.expr, str]: if isinstance(comp.left, (ast.Compare, ast.BoolOp)): left_expl = f"({left_expl})" res_variables = [self.variable() for i in range(len(comp.ops))] - load_names = [ast.Name(v, ast.Load()) for v in res_variables] + load_names: List[ast.expr] = [ast.Name(v, ast.Load()) for v in res_variables] store_names = [ast.Name(v, ast.Store()) for v in res_variables] it = zip(range(len(comp.ops)), comp.ops, comp.comparators) - expls = [] - syms = [] + expls: List[ast.expr] = [] + syms: List[ast.expr] = [] results = [left_res] for i, op, next_operand in it: if ( From e97aec872eda625d2e2002c7dd72359a632a0667 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sat, 18 May 2024 13:53:02 +0000 Subject: [PATCH 2/3] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- src/_pytest/assertion/rewrite.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/_pytest/assertion/rewrite.py b/src/_pytest/assertion/rewrite.py index 1fe1b42f80f..528c238e41f 100644 --- a/src/_pytest/assertion/rewrite.py +++ b/src/_pytest/assertion/rewrite.py @@ -959,7 +959,9 @@ def visit_Assert(self, assert_: ast.Assert) -> List[ast.stmt]: # Clear temporary variables by setting them to None. if self.variables: - variables: List[ast.expr] = [ast.Name(name, ast.Store()) for name in self.variables] + variables: List[ast.expr] = [ + ast.Name(name, ast.Store()) for name in self.variables + ] clear = ast.Assign(variables, ast.Constant(None)) self.statements.append(clear) # Fix locations (line numbers/column offsets). From 166cc00c7cc7c448072cc6c2c6e3d94a5a3eb12c Mon Sep 17 00:00:00 2001 From: Jelle Zijlstra Date: Sat, 18 May 2024 10:09:10 -0400 Subject: [PATCH 3/3] Fix more mypy issues --- src/_pytest/assertion/rewrite.py | 6 ++---- testing/test_assertrewrite.py | 1 + 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/src/_pytest/assertion/rewrite.py b/src/_pytest/assertion/rewrite.py index 528c238e41f..b29a254f5f7 100644 --- a/src/_pytest/assertion/rewrite.py +++ b/src/_pytest/assertion/rewrite.py @@ -835,7 +835,7 @@ def pop_format_context(self, expl_expr: ast.expr) -> ast.Name: current = self.stack.pop() if self.stack: self.explanation_specifiers = self.stack[-1] - keys: List[ast.expr] = [ast.Constant(key) for key in current.keys()] + keys: List[Optional[ast.expr]] = [ast.Constant(key) for key in current.keys()] format_dict = ast.Dict(keys, list(current.values())) form = ast.BinOp(expl_expr, ast.Mod(), format_dict) name = "@py_format" + str(next(self.variable_counter)) @@ -959,9 +959,7 @@ def visit_Assert(self, assert_: ast.Assert) -> List[ast.stmt]: # Clear temporary variables by setting them to None. if self.variables: - variables: List[ast.expr] = [ - ast.Name(name, ast.Store()) for name in self.variables - ] + variables = [ast.Name(name, ast.Store()) for name in self.variables] clear = ast.Assign(variables, ast.Constant(None)) self.statements.append(clear) # Fix locations (line numbers/column offsets). diff --git a/testing/test_assertrewrite.py b/testing/test_assertrewrite.py index 82c7055b968..8db9dbbe5ff 100644 --- a/testing/test_assertrewrite.py +++ b/testing/test_assertrewrite.py @@ -130,6 +130,7 @@ def test_location_is_set(self) -> None: if isinstance(node, ast.Import): continue for n in [node, *ast.iter_child_nodes(node)]: + assert isinstance(n, (ast.stmt, ast.expr)) assert n.lineno == 3 assert n.col_offset == 0 assert n.end_lineno == 6