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

Failed to create fix for StringDotFormatExtraNamedArguments: Failed to extract expression from source #6720

Closed
qarmin opened this issue Aug 21, 2023 · 1 comment · Fixed by #6742
Assignees
Labels
bug Something isn't working fuzzer Surfaced via fuzzing.

Comments

@qarmin
Copy link

qarmin commented Aug 21, 2023

Ruff 0.0.285

ruff  *.py --select ALL --no-cache

file content:

def list_test_params(param_input):
        option = (
            '{name}={value}'
            .format(nam=name, value=value)
        )

error:

error: Failed to create fix for StringDotFormatExtraNamedArguments: Failed to extract expression from source

PY_FILE_TEST_14854806573.py.zip

@charliermarsh charliermarsh added bug Something isn't working fuzzer Surfaced via fuzzing. labels Aug 21, 2023
@charliermarsh
Copy link
Member

Probably requires "fake" parentheses around the expression to support multi-line parsing.

@charliermarsh charliermarsh self-assigned this Aug 21, 2023
charliermarsh added a commit that referenced this issue Aug 22, 2023
<!--
Thank you for contributing to Ruff! To help us out with reviewing,
please consider the following:

- Does this pull request include a summary of the change? (See below.)
- Does this pull request include a descriptive title?
- Does this pull request include references to any relevant issues?
-->

## Summary

This PR adds a utility for transforming expressions via LibCST that
automatically wraps the expression in parentheses, applies a
user-provided transformation, then strips the parentheses from the
generated code. LibCST can't parse arbitrary expression ranges, since
some expressions may require parenthesization in order to be parsed
properly. For example:

```python
option = (
    '{name}={value}'
    .format(nam=name, value=value)
)
```

In this case, the expression range is:

```python
'{name}={value}'
    .format(nam=name, value=value)
```

Which isn't valid on its own. So, instead, we add "fake" parentheses
around the expression.

We were already doing this in a few places, so this is mostly
formalizing and DRYing up that pattern.

Closes #6720.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working fuzzer Surfaced via fuzzing.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants