-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
[pyupgrade] Automatically rewrite format-strings to f-strings #1905
Conversation
@charliermarsh this pr is ready except for the questions below, so I will move it out of draft. Before this is merged we need to consider or resolve the following items:
This is not supposed to be formatted because of its "weird syntax", but our AST parser ignores the spaces. This means ignoring this is more work for us, and results in a worse user experience. |
} | ||
|
||
/// Returns true if the statement and function call match, and false if not | ||
fn check_with_summary(&self, summary: &FormatSummary) -> bool { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@colin99d - Is this strictly necessary? (Do you know of any case where it would trigger?)
@colin99d - I'm fine to ignore the unicode case. What else needs solving here? The |
That should be the only thing! |
I have one idea I'd like to try, which may let us avoid regexes. |
(As-is, what happens in those cases? We just ignore them?) |
Right now it is ignored in a way. For example: below rust thinks it needs three arguments but only finds 2, so it exits. If we are going to ignore these, we should probably implement some better logic for ignoring them. |
I think we can leverage RustPython's actual string parser and forego regular expressions. Trying it now... |
@colin99d - I was able to use RustPython's own parser, and I think it's properly handling all test-cases now. This might be a useful approach to use in the other PR too? I haven't looked into it deeply, but generally, it's best to use the RustPython parsers where we can. |
[![Mend Renovate](https://app.renovatebot.com/images/banner.svg)](https://renovatebot.com) This PR contains the following updates: | Package | Change | Age | Adoption | Passing | Confidence | |---|---|---|---|---|---| | [ruff](https://github.com/charliermarsh/ruff) | `^0.0.223` -> `^0.0.224` | [![age](https://badges.renovateapi.com/packages/pypi/ruff/0.0.224/age-slim)](https://docs.renovatebot.com/merge-confidence/) | [![adoption](https://badges.renovateapi.com/packages/pypi/ruff/0.0.224/adoption-slim)](https://docs.renovatebot.com/merge-confidence/) | [![passing](https://badges.renovateapi.com/packages/pypi/ruff/0.0.224/compatibility-slim/0.0.223)](https://docs.renovatebot.com/merge-confidence/) | [![confidence](https://badges.renovateapi.com/packages/pypi/ruff/0.0.224/confidence-slim/0.0.223)](https://docs.renovatebot.com/merge-confidence/) | --- ### Release Notes <details> <summary>charliermarsh/ruff</summary> ### [`v0.0.224`](https://github.com/charliermarsh/ruff/releases/tag/v0.0.224) [Compare Source](https://github.com/charliermarsh/ruff/compare/v0.0.223...v0.0.224) #### What's Changed - Re-run benchmark and update documentation by [@​charliermarsh](https://github.com/charliermarsh) in [https://github.com/charliermarsh/ruff/pull/1907](https://github.com/charliermarsh/ruff/pull/1907) - Derive Hash instead of implementing it by hand by [@​not-my-profile](https://github.com/not-my-profile) in [https://github.com/charliermarsh/ruff/pull/1890](https://github.com/charliermarsh/ruff/pull/1890) - Add backticks to B904's message by [@​harupy](https://github.com/harupy) in [https://github.com/charliermarsh/ruff/pull/1914](https://github.com/charliermarsh/ruff/pull/1914) - Refactor `flake8_tidy_imports` by [@​not-my-profile](https://github.com/not-my-profile) in [https://github.com/charliermarsh/ruff/pull/1909](https://github.com/charliermarsh/ruff/pull/1909) - Trigger update to pre-commit mirror after pypi publish by [@​pmbarrett314](https://github.com/pmbarrett314) in [https://github.com/charliermarsh/ruff/pull/1910](https://github.com/charliermarsh/ruff/pull/1910) - Rewrite `lru_cache` to `cache` on Python 3.9+ by [@​charliermarsh](https://github.com/charliermarsh) in [https://github.com/charliermarsh/ruff/pull/1918](https://github.com/charliermarsh/ruff/pull/1918) - Avoid syntax errors when fixing parenthesized unused variables by [@​charliermarsh](https://github.com/charliermarsh) in [https://github.com/charliermarsh/ruff/pull/1919](https://github.com/charliermarsh/ruff/pull/1919) - Add some new testimonials by [@​charliermarsh](https://github.com/charliermarsh) in [https://github.com/charliermarsh/ruff/pull/1921](https://github.com/charliermarsh/ruff/pull/1921) - Avoid removing statements that contain side-effects by [@​charliermarsh](https://github.com/charliermarsh) in [https://github.com/charliermarsh/ruff/pull/1920](https://github.com/charliermarsh/ruff/pull/1920) - Add benchmark scripts for no-IO by [@​charliermarsh](https://github.com/charliermarsh) in [https://github.com/charliermarsh/ruff/pull/1925](https://github.com/charliermarsh/ruff/pull/1925) - Add flake8-pie PIE796: prefer-unique-enum by [@​ljesparis](https://github.com/ljesparis) in [https://github.com/charliermarsh/ruff/pull/1923](https://github.com/charliermarsh/ruff/pull/1923) - \[pyupgrade] Automatically rewrite format-strings to f-strings by [@​colin99d](https://github.com/colin99d) in [https://github.com/charliermarsh/ruff/pull/1905](https://github.com/charliermarsh/ruff/pull/1905) #### New Contributors - [@​pmbarrett314](https://github.com/pmbarrett314) made their first contribution in [https://github.com/charliermarsh/ruff/pull/1910](https://github.com/charliermarsh/ruff/pull/1910) - [@​ljesparis](https://github.com/ljesparis) made their first contribution in [https://github.com/charliermarsh/ruff/pull/1923](https://github.com/charliermarsh/ruff/pull/1923) **Full Changelog**: astral-sh/ruff@v0.0.223...v0.0.224 </details> --- ### Configuration 📅 **Schedule**: Branch creation - At any time (no schedule defined), Automerge - At any time (no schedule defined). 🚦 **Automerge**: Enabled. ♻ **Rebasing**: Whenever PR is behind base branch, or you tick the rebase/retry checkbox. 🔕 **Ignore**: Close this PR and you won't be reminded about this update again. --- - [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check this box --- This PR has been generated by [Mend Renovate](https://www.mend.io/free-developer-tools/renovate/). View repository job log [here](https://app.renovatebot.com/dashboard#github/ixm-one/pytest-cmake-presets). <!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiIzNC4xMDIuNyIsInVwZGF0ZWRJblZlciI6IjM0LjEwMi43In0=--> Signed-off-by: Renovate Bot <[email protected]> Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
…regardless of line-length (`UP032`) (#10238) ## Summary Fixes #10235 This PR changes `UP032` to flag all `"".format` calls that can technically be rewritten to an f-string, even if rewritting it to an fstring, at least automatically, exceeds the line length (or increases the amount by which it goes over the line length). I looked at the Git history to understand whether the check prevents some false positives (reported by an issue), but i haven't found a compelling reason to limit the rule to only flag format calls that stay in the line length limit: * #7818 Changed the heuristic to determine if the fix fits to address #7810 * #1905 first version of the rule I did take a look at pyupgrade and couldn't find a similar check, at least not in the rule code (maybe it's checked somewhere else?) https://github.com/asottile/pyupgrade/blob/main/pyupgrade/_plugins/fstrings.py ## Breaking Change? This could be seen as a breaking change according to ruff's [versioning policy](https://docs.astral.sh/ruff/versioning/): > The behavior of a stable rule is changed * The scope of a stable rule is significantly increased * The intent of the rule changes * Does not include bug fixes that follow the original intent of the rule It does increase the scope of the rule, but it is in the original intent of the rule (so it's not). ## Test Plan See changed test output
…regardless of line-length (`UP032`) (astral-sh#10238) ## Summary Fixes astral-sh#10235 This PR changes `UP032` to flag all `"".format` calls that can technically be rewritten to an f-string, even if rewritting it to an fstring, at least automatically, exceeds the line length (or increases the amount by which it goes over the line length). I looked at the Git history to understand whether the check prevents some false positives (reported by an issue), but i haven't found a compelling reason to limit the rule to only flag format calls that stay in the line length limit: * astral-sh#7818 Changed the heuristic to determine if the fix fits to address astral-sh#7810 * astral-sh#1905 first version of the rule I did take a look at pyupgrade and couldn't find a similar check, at least not in the rule code (maybe it's checked somewhere else?) https://github.com/asottile/pyupgrade/blob/main/pyupgrade/_plugins/fstrings.py ## Breaking Change? This could be seen as a breaking change according to ruff's [versioning policy](https://docs.astral.sh/ruff/versioning/): > The behavior of a stable rule is changed * The scope of a stable rule is significantly increased * The intent of the rule changes * Does not include bug fixes that follow the original intent of the rule It does increase the scope of the rule, but it is in the original intent of the rule (so it's not). ## Test Plan See changed test output
Draft opened for visibility. This is a part of #827. This PR is currently affected by the \N{emoji} bug that we are searching for a fix for.