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

Remove #[allow(unused_variables)] from visitor methods #8828

Merged
merged 17 commits into from
Nov 25, 2023

Conversation

dhruvmanila
Copy link
Member

Small follow-up to remove #[allow(unused_variables)] from visitor methods and use underscore prefix for unused variables instead.

This commit implements the AST design to account for implicit
concatenation in string nodes, specifically the `ExprFString`,
`ExprStringLiteral`, and `ExprBytesLiteral` nodes.
This commit adds the new variants for the string parts to `AnyNode` and
`AnyNodeRef` enums. These parts are `StringLiteral` (part of
`ExprStringLiteral`), `BytesLiteral` (part of `ExprBytesLiteral`), and
`FString` (part of `ExprFString`).

The reason for this is to add visitor methods for these parts. This is
done in the following commit. So, the visitor would visit the string as
a whole first and then visit each part.

```
ExprStringLiteral - "foo" "bar"
|- StringLiteral - "foo"
|- StringLiteral - "bar"
```

The above tree helps understand the way visitor would work.
The visitor implementations are updated to visit each part nodes for the
respective string nodes.

The following example better highlights this:
```
ExprStringLiteral - "foo" "bar"
|- StringLiteral - "foo"
|- StringLiteral - "bar"
```

The `visit_expr` method would be use to visit the `ExprStringLiteral`
while the `visit_string_literal` method would be use for the
`StringLiteral` node. Similar methods are added for bytes and f-strings.
The generator is basically improved. Earlier, for an implicitly
concatenated string we would produce the joined form. So,

```python
"foo" "bar" "baz"
```

For the above example, the generator would give us:
```python
"foobarbaz"
```

Now, as we have the information for each part, we will be producing the
exact code back.
`Expr` is a general type for all expressions while
`LiteralExpressionRef` is a type which includes only the literal
expressions. The method is suited more for this type instead.

This will also help in the formatter change.
As highlighted in the review:

> If you have two `ConcatenatedStringLiteral` values where both have
> equivalent values for `strings` but where one has `value` initialized
> and the other does not, would you expect them to compare equal?
> Semantically I think I would, since the alternative is that equality is
> dependent on whether `as_str()` has been called, which seems incidental.

#7927 (comment)
@dhruvmanila dhruvmanila added the internal An internal refactor or improvement label Nov 23, 2023
Copy link
Contributor

github-actions bot commented Nov 23, 2023

ruff-ecosystem results

Linter (stable)

✅ ecosystem check detected no linter changes.

Linter (preview)

✅ ecosystem check detected no linter changes.

Formatter (stable)

✅ ecosystem check detected no format changes.

Formatter (preview)

✅ ecosystem check detected no format changes.

Base automatically changed from dhruv/custom-methods to main November 25, 2023 00:04
@dhruvmanila dhruvmanila enabled auto-merge (squash) November 25, 2023 00:05
@dhruvmanila dhruvmanila merged commit 501cca8 into main Nov 25, 2023
16 checks passed
@dhruvmanila dhruvmanila deleted the dhruv/remove-allow branch November 25, 2023 00:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
internal An internal refactor or improvement
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants