Skip to content

Commit efc045e

Browse files
authored
Restrict type of AssignmentExpr.target to NameExpr (#18714)
Assignment expression targets can only be identifiers. From the [grammar](https://docs.python.org/3.14/reference/grammar.html): ``` assignment_expression: | NAME ':=' ~ expression ``` This [corresponds](https://github.com/python/typeshed/blob/ac8f2632ec37bb4a82ade0906e6ce9bdb33883d3/stdlib/ast.pyi#L834-L837) to the standard library AST node's `target` type: ```python class NamedExpr(expr): if sys.version_info >= (3, 10): __match_args__ = ("target", "value") target: Name ```
1 parent b1ac028 commit efc045e

File tree

2 files changed

+2
-2
lines changed

2 files changed

+2
-2
lines changed

mypy/nodes.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2075,7 +2075,7 @@ class AssignmentExpr(Expression):
20752075

20762076
__match_args__ = ("target", "value")
20772077

2078-
def __init__(self, target: Expression, value: Expression) -> None:
2078+
def __init__(self, target: NameExpr, value: Expression) -> None:
20792079
super().__init__()
20802080
self.target = target
20812081
self.value = value

mypy/treetransform.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -559,7 +559,7 @@ def visit_super_expr(self, node: SuperExpr) -> SuperExpr:
559559
return new
560560

561561
def visit_assignment_expr(self, node: AssignmentExpr) -> AssignmentExpr:
562-
return AssignmentExpr(self.expr(node.target), self.expr(node.value))
562+
return AssignmentExpr(self.duplicate_name(node.target), self.expr(node.value))
563563

564564
def visit_unary_expr(self, node: UnaryExpr) -> UnaryExpr:
565565
new = UnaryExpr(node.op, self.expr(node.expr))

0 commit comments

Comments
 (0)