Skip to content

Commit ee9ea70

Browse files
Fix new typing issues in AST code (#12337)
python/typeshed#11880 adds more precise types for AST nodes. I'm submitting some changes to adapt pytest to these changes.
1 parent 635fbe2 commit ee9ea70

File tree

2 files changed

+7
-6
lines changed

2 files changed

+7
-6
lines changed

src/_pytest/assertion/rewrite.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -835,7 +835,7 @@ def pop_format_context(self, expl_expr: ast.expr) -> ast.Name:
835835
current = self.stack.pop()
836836
if self.stack:
837837
self.explanation_specifiers = self.stack[-1]
838-
keys = [ast.Constant(key) for key in current.keys()]
838+
keys: List[Optional[ast.expr]] = [ast.Constant(key) for key in current.keys()]
839839
format_dict = ast.Dict(keys, list(current.values()))
840840
form = ast.BinOp(expl_expr, ast.Mod(), format_dict)
841841
name = "@py_format" + str(next(self.variable_counter))
@@ -926,13 +926,13 @@ def visit_Assert(self, assert_: ast.Assert) -> List[ast.stmt]:
926926
[*self.expl_stmts, hook_call_pass],
927927
[],
928928
)
929-
statements_pass = [hook_impl_test]
929+
statements_pass: List[ast.stmt] = [hook_impl_test]
930930

931931
# Test for assertion condition
932932
main_test = ast.If(negation, statements_fail, statements_pass)
933933
self.statements.append(main_test)
934934
if self.format_variables:
935-
variables = [
935+
variables: List[ast.expr] = [
936936
ast.Name(name, ast.Store()) for name in self.format_variables
937937
]
938938
clear_format = ast.Assign(variables, ast.Constant(None))
@@ -1114,11 +1114,11 @@ def visit_Compare(self, comp: ast.Compare) -> Tuple[ast.expr, str]:
11141114
if isinstance(comp.left, (ast.Compare, ast.BoolOp)):
11151115
left_expl = f"({left_expl})"
11161116
res_variables = [self.variable() for i in range(len(comp.ops))]
1117-
load_names = [ast.Name(v, ast.Load()) for v in res_variables]
1117+
load_names: List[ast.expr] = [ast.Name(v, ast.Load()) for v in res_variables]
11181118
store_names = [ast.Name(v, ast.Store()) for v in res_variables]
11191119
it = zip(range(len(comp.ops)), comp.ops, comp.comparators)
1120-
expls = []
1121-
syms = []
1120+
expls: List[ast.expr] = []
1121+
syms: List[ast.expr] = []
11221122
results = [left_res]
11231123
for i, op, next_operand in it:
11241124
if (

testing/test_assertrewrite.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ def test_location_is_set(self) -> None:
130130
if isinstance(node, ast.Import):
131131
continue
132132
for n in [node, *ast.iter_child_nodes(node)]:
133+
assert isinstance(n, (ast.stmt, ast.expr))
133134
assert n.lineno == 3
134135
assert n.col_offset == 0
135136
assert n.end_lineno == 6

0 commit comments

Comments
 (0)