Skip to content

Incorrect type for ast.For.target field. #94698

@mrolle45

Description

@mrolle45

Documentation

In ast module doc, it says:

class ast.For(target, iter, body, orelse, type_comment)

A for loop. target holds the variable(s) the loop assigns to, as a single Name, Tuple or List node.

Correction

The target can also be a Subscript or Attribute node.

Example:

for x.a in range(4):
     print(x.a)

This parses to:

Module(
    body=[
        For(
            target=Attribute(
                value=Name(id='x', ctx=Load()),
                attr='a',
                ctx=Store()),
            iter=Call(
                func=Name(id='range', ctx=Load()),
                args=[
                    Constant(value=4)],
                keywords=[]),
            body=[
                Expr(
                    value=Call(
                        func=Name(id='print', ctx=Load()),
                        args=[
                            Attribute(
                                value=Name(id='x', ctx=Load()),
                                attr='a',
                                ctx=Load())],
                        keywords=[]))],
            orelse=[])],
    type_ignores=[])

Metadata

Metadata

Assignees

No one assigned

    Labels

    docsDocumentation in the Doc dir

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions