-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Annotation for target attribute in ast.Assign and similar nodes is too broad #9287
Comments
I believe you are correct. I'd certainly be interested to see a PR proposing this change, so that we can see what the Note that the restrictions on the walrus operator ( C:\Users\alexw\coding>python
Python 3.10.7 (tags/v3.10.7:6cc6b13, Sep 5 2022, 14:08:36) [MSC v.1933 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> x = [1, 2, 3]
>>> x[0] := 4
File "<stdin>", line 1
x[0] := 4
^^^^
SyntaxError: cannot use assignment expressions with subscript
>>> class Foo: ...
...
>>> Foo.bar := 42
File "<stdin>", line 1
Foo.bar := 42
^^^^^^^
SyntaxError: cannot use assignment expressions with attribute There may be other differences between the nodes, so we need to be careful. |
Thanks Alex! I will start working on it then :) |
The LHS of the walrus operator can only be Your original message suggest adding something called I'm a little hesitant to narrow the typeshed types more than the types in the official AST definition (https://docs.python.org/3.11/library/ast.html), but it's probably harmless. |
You are correct, For ast.Assign:This now indeed looks like a change that would benefit from a change on the CPython side and therefore in the official docs that you linked. Especially since they are inconsistent with the docs I linked in the issue desciption. I think doing what I suggested directly in typeshed might be a bandaid fix that would occlude an underlying issue. Especially, since now that you mentioned it, if we include the class Tuple(expr):
elts: list[expr]
ctx: expr_context Then, at current stage, best we could do for class Assign(stmt):
targets: list[Union[ast.Tuple, ast.Name, ast.Attribute, ast.Subscript]]
value: expr Therefore, we would probably need a new node called class AssignTargetGroup(expr):
elts: list[ast.Name, ast.Attribute, ast.Subscript] Am I making sense and should I file a ticket under CPython with a request to change how ast.Assign works? :) Remaining assign-like nodes:As for other nodes ( |
Hmm, are they? Before the description of the semantics when assigning an object to a single target, the docs state:
That seems to cover assigning to a tuple or list to me?
I'm not sure I fully understand what you're proposing, but CPython would need carefully about backwards-compatibility considerations, if there are any. (Jelle and I are both also CPython core devs BTW, but I for one don't consider myself an AST expert.) Anyway, I agree that there seems to be much less value in changing |
The problem you see with It's unlikely we'll change the actual types in the |
Currently, the
targets
attribute forast.Assign
, and other nodes where assignment happens is likely too broad - it isast.expr
.I have a suspicion that, this is likely too generic and should be narrowed down, to correct set of types that can truly serve the role of an assignment target.
From a quick look at the official docs for the assignment statement, looks like the correct type should roughly be the union of the following types: ast.Name, ast.Attribute, ast.Subscript.
If I get a confirmation that indeed this could be improved, then I'd love to work on this change. In general in the PR I would take a look at all nodes that cause assignment of value to a target (Assign, AnnAsign, AugAssign, withitem, NamedExpr) and introduce a new type called
ast.assign_target
.The text was updated successfully, but these errors were encountered: