Skip to content
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

elt in generator expressions have incorrect type annotations #500

Closed
zsol opened this issue Jul 28, 2021 · 1 comment
Closed

elt in generator expressions have incorrect type annotations #500

zsol opened this issue Jul 28, 2021 · 1 comment
Labels
documentation Improvements or additions to documentation good first issue Good for newcomers parsing Converting source code into CST nodes

Comments

@zsol
Copy link
Member

zsol commented Jul 28, 2021

The fine docs say that elt in the various comprehension nodes are of type BaseAssignTargetExpression. This is a lie. Observe: in the syntactically correct and semantically plausible expression (i ** 2 for i in _), the elt is constructed from i**2 which is a BinaryOperation:

> echo "(i**2 for _ in _)" | python -m libcst.tool print -
Module(
  body=[
    SimpleStatementLine(
      body=[
        Expr(
          value=GeneratorExp(
            elt=BinaryOperation(
              left=Name(
                value='i',
              ),
              operator=Power(),
              right=Integer(
                value='2',
              ),
            ),
            for_in=CompFor(
              target=Name(
                value='i',
              ),
              iter=Name(
                value='_',
              ),
            ),
          ),
        ),
      ],
    ),
  ],
)

But alas, BinaryOperation is not a BaseAssignTargetExpression.

>>> issubclass(cst.BinaryOperation, cst.BaseAssignTargetExpression)
False

What is the correct type for elt here? BaseExpression?

@zsol
Copy link
Member Author

zsol commented Jul 28, 2021

The Python ast docs say it can be any expression.

@zsol zsol added documentation Improvements or additions to documentation good first issue Good for newcomers labels Sep 24, 2021
@zsol zsol added the parsing Converting source code into CST nodes label Jun 16, 2022
zsol added a commit that referenced this issue May 28, 2023
This PR relaxes the accepted types for the `elt` field in `ListComp`, `SetComp`, and `GenExp`, as well as the `key` and `value` fields in `DictComp`.

Fixes #500.
zsol added a commit that referenced this issue May 28, 2023
This PR relaxes the accepted types for the `elt` field in `ListComp`, `SetComp`, and `GenExp`, as well as the `key` and `value` fields in `DictComp`.

Fixes #500.
zsol added a commit that referenced this issue May 28, 2023
This PR relaxes the accepted types for the `elt` field in `ListComp`, `SetComp`, and `GenExp`, as well as the `key` and `value` fields in `DictComp`.

Fixes #500.
@zsol zsol closed this as completed in 2acc293 Jun 7, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation good first issue Good for newcomers parsing Converting source code into CST nodes
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant