-
Notifications
You must be signed in to change notification settings - Fork 896
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
Enable autofix for annotations within 'simple' string literals #3657
Conversation
5a9cba4
to
cd55390
Compare
PR Check ResultsEcosystem✅ ecosystem check detected no changes. BenchmarkLinux
Windows
|
self.deferred.string_type_definitions.reverse(); | ||
while let Some((range, expression, (in_annotation, in_type_checking_block), deferral)) = | ||
while let Some((range, value, (in_annotation, in_type_checking_block), deferral)) = | ||
self.deferred.string_type_definitions.pop() |
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.
Nit: We can try to use drain
to avoid the need for reversing and manually call pop
let mut string_type_definitions = self.deferred.string_type_definitions.drain(..).rev();
for ((range, value, (in_annotation, in_type_checking_block), deferral)) = string_type_definitions {
...
}
If the borrow checker gets mad because of multiple mutable borrows of self
, then try this:
let mut string_type_definitions = std::mem::take(&mut self.deferred.string_type_definitions);
for ((range, ..)) = string_type_definitions.drain(..).rev() {
..
}
// Restore vector, to re-use the heap allocations
self.deferred.string_type_definitions = string_type_definitions;
Both these approaches assume that the loop's body does not read from or write to `self.deferred.string_type_definitions.
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.
I think we actually do want to write to self.deferred.string_type_definitions
, but we probably don't do it currently (#3655).
crates/ruff_python_ast/src/scope.rs
Outdated
use std::num::TryFromIntError; | ||
use std::ops::{Deref, Index, IndexMut}; | ||
|
||
use rustc_hash::FxHashMap; |
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.
Revert?
@@ -66,3 +72,48 @@ pub fn is_pep585_builtin(expr: &Expr, context: &Context) -> bool { | |||
PEP_585_BUILTINS_ELIGIBLE.contains(&call_path.as_slice()) | |||
}) | |||
} | |||
|
|||
#[derive(is_macro::Is)] |
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.
isn't that a case for derive(PartialEq, Eq)]
?
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.
This macro gives us something slightly different -- we can do annotation.is_simple()
, similar to what we get with Result
.
cd55390
to
6ee2ddf
Compare
[![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://togithub.com/charliermarsh/ruff) | `^0.0.257` -> `^0.0.258` | [![age](https://badges.renovateapi.com/packages/pypi/ruff/0.0.258/age-slim)](https://docs.renovatebot.com/merge-confidence/) | [![adoption](https://badges.renovateapi.com/packages/pypi/ruff/0.0.258/adoption-slim)](https://docs.renovatebot.com/merge-confidence/) | [![passing](https://badges.renovateapi.com/packages/pypi/ruff/0.0.258/compatibility-slim/0.0.257)](https://docs.renovatebot.com/merge-confidence/) | [![confidence](https://badges.renovateapi.com/packages/pypi/ruff/0.0.258/confidence-slim/0.0.257)](https://docs.renovatebot.com/merge-confidence/) | --- ### Release Notes <details> <summary>charliermarsh/ruff</summary> ### [`v0.0.258`](https://togithub.com/charliermarsh/ruff/releases/tag/v0.0.258) [Compare Source](https://togithub.com/charliermarsh/ruff/compare/v0.0.257...v0.0.258) <!-- Release notes generated using configuration in .github/release.yml at main --> #### What's Changed ##### Rules - \[`flake8-comprehensions`] Update `C416` with dict comprehension (autofixable) by [@​dhruvmanila](https://togithub.com/dhruvmanila) in [astral-sh/ruff#3605 - \[`pylint`]: Implement `assert-on-string-literal` (`W0129`) by [@​latonis](https://togithub.com/latonis) in [astral-sh/ruff#3610 - \[`pyupgrade`] Convert single-argument %-style format calls by [@​charliermarsh](https://togithub.com/charliermarsh) in [astral-sh/ruff#3600 - \[`pyupgrade`] Flag PEP 585 and PEP 604 violations in quoted annotations by [@​charliermarsh](https://togithub.com/charliermarsh) in [astral-sh/ruff#3593 - \[`pyupgrade`] Enable autofix for annotations within 'simple' string literals by [@​charliermarsh](https://togithub.com/charliermarsh) in [astral-sh/ruff#3657 - \[`pyflakes`] Add autofix functionality for `F523` ([#​3613](https://togithub.com/charliermarsh/ruff/issues/3613)) by [@​JonathanPlasse](https://togithub.com/JonathanPlasse) in [astral-sh/ruff#3613 - \[`flake8-bandit`]: Implement deny-list rules for suspicious member calls by [@​colin99d](https://togithub.com/colin99d) in [astral-sh/ruff#3239 - \[`flake8-annotations`] Add autofix for `ANN204` with magic methods by [@​JonathanPlasse](https://togithub.com/JonathanPlasse) in [astral-sh/ruff#3633 - \[`pylint`] Implement `binary-op-exception` (`PLW0711`) by [@​latonis](https://togithub.com/latonis) in [astral-sh/ruff#3639 - \[`flake8-django`]: Implement rule DJ012 by [@​dhruvmanila](https://togithub.com/dhruvmanila) in [astral-sh/ruff#3659 ##### Bug Fixes - Check exclusions prior to resolving `pyproject.toml` files by [@​charliermarsh](https://togithub.com/charliermarsh) in [astral-sh/ruff#3588 - Fix D417 false positive by [@​JonathanPlasse](https://togithub.com/JonathanPlasse) in [astral-sh/ruff#3596 - Avoid removing comment hash for noqa's with trailing content by [@​charliermarsh](https://togithub.com/charliermarsh) in [astral-sh/ruff#3589 - Avoid panics for implicitly-concatenated docstrings by [@​charliermarsh](https://togithub.com/charliermarsh) in [astral-sh/ruff#3584 - Fix infinite loop due to rules `D207` & `W605` by [@​vlindhol](https://togithub.com/vlindhol) in [astral-sh/ruff#3609 - Avoid trimming escaped whitespace in D210 by [@​charliermarsh](https://togithub.com/charliermarsh) in [astral-sh/ruff#3635 - Handle `UP032` autofix with adjacent keywords by [@​JonathanPlasse](https://togithub.com/JonathanPlasse) in [astral-sh/ruff#3636 - Consider same-site fixes to be overlapping by [@​charliermarsh](https://togithub.com/charliermarsh) in [astral-sh/ruff#3638 - Avoid `RUF007` fixes for more than two arguments by [@​JonathanPlasse](https://togithub.com/JonathanPlasse) in [astral-sh/ruff#3654 - Allow `pairwise` diagnostics for `zip(..., strict=True)` by [@​charliermarsh](https://togithub.com/charliermarsh) in [astral-sh/ruff#3669 - isort: fix bad interaction between `force-sort-within-sections` and `force-to-top` by [@​bluetech](https://togithub.com/bluetech) in [astral-sh/ruff#3645 - Gracefully handle lint panics by [@​MichaReiser](https://togithub.com/MichaReiser) in [astral-sh/ruff#3509 - Fix TRY300 false positive by [@​JonathanPlasse](https://togithub.com/JonathanPlasse) in [astral-sh/ruff#3634 - Avoid raising PEP 604 errors with forward-referenced members by [@​charliermarsh](https://togithub.com/charliermarsh) in [astral-sh/ruff#3640 - Avoid attempting infinite `open` fix with re-bound builtin by [@​charliermarsh](https://togithub.com/charliermarsh) in [astral-sh/ruff#3650 - Check indentation level when executing E231 by [@​kyoto7250](https://togithub.com/kyoto7250) in [astral-sh/ruff#3668 - Flag, but don't fix, unused imports (`F401`) in `ModuleNotFoundError` blocks by [@​charliermarsh](https://togithub.com/charliermarsh) in [astral-sh/ruff#3658 #### New Contributors - [@​Rogdham](https://togithub.com/Rogdham) made their first contribution in [astral-sh/ruff#3607 - [@​vlindhol](https://togithub.com/vlindhol) made their first contribution in [astral-sh/ruff#3609 - [@​dhruvmanila](https://togithub.com/dhruvmanila) made their first contribution in [astral-sh/ruff#3605 - [@​luke396](https://togithub.com/luke396) made their first contribution in [astral-sh/ruff#3604 - [@​fuziontech](https://togithub.com/fuziontech) made their first contribution in [astral-sh/ruff#3641 **Full Changelog**: astral-sh/ruff@v0.0.257...v0.0.258 </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:eyJjcmVhdGVkSW5WZXIiOiIzNS4xNy4xIiwidXBkYXRlZEluVmVyIjoiMzUuMTcuMSJ9--> Signed-off-by: Renovate Bot <bot@renovateapp.com> Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
[![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://togithub.com/charliermarsh/ruff) | `==0.0.257` -> `==0.0.259` | [![age](https://badges.renovateapi.com/packages/pypi/ruff/0.0.259/age-slim)](https://docs.renovatebot.com/merge-confidence/) | [![adoption](https://badges.renovateapi.com/packages/pypi/ruff/0.0.259/adoption-slim)](https://docs.renovatebot.com/merge-confidence/) | [![passing](https://badges.renovateapi.com/packages/pypi/ruff/0.0.259/compatibility-slim/0.0.257)](https://docs.renovatebot.com/merge-confidence/) | [![confidence](https://badges.renovateapi.com/packages/pypi/ruff/0.0.259/confidence-slim/0.0.257)](https://docs.renovatebot.com/merge-confidence/) | --- ### Release Notes <details> <summary>charliermarsh/ruff</summary> ### [`v0.0.259`](https://togithub.com/charliermarsh/ruff/releases/tag/v0.0.259) [Compare Source](https://togithub.com/charliermarsh/ruff/compare/v0.0.258...v0.0.259) <!-- Release notes generated using configuration in .github/release.yml at main --> #### Summary Follow-up release to `v0.0.258` to fix an issue related to rule resolution via `select` and `ignore`. #### What's Changed ##### Bug Fixes - Fix RuleSet.remove by [@​MichaReiser](https://togithub.com/MichaReiser) in [astral-sh/ruff#3685 - Respect all rule-exemption sources when suppressing parser errors by [@​charliermarsh](https://togithub.com/charliermarsh) in [astral-sh/ruff#3665 - Avoid nested loops in `missing_whitespace` by [@​charliermarsh](https://togithub.com/charliermarsh) in [astral-sh/ruff#3688 **Full Changelog**: astral-sh/ruff@v0.0.258...v0.0.259 ### [`v0.0.258`](https://togithub.com/charliermarsh/ruff/releases/tag/v0.0.258) [Compare Source](https://togithub.com/charliermarsh/ruff/compare/v0.0.257...v0.0.258) <!-- Release notes generated using configuration in .github/release.yml at main --> #### What's Changed ##### Rules - \[`flake8-comprehensions`] Update `C416` with dict comprehension (autofixable) by [@​dhruvmanila](https://togithub.com/dhruvmanila) in [astral-sh/ruff#3605 - \[`pylint`]: Implement `assert-on-string-literal` (`W0129`) by [@​latonis](https://togithub.com/latonis) in [astral-sh/ruff#3610 - \[`pyupgrade`] Convert single-argument %-style format calls by [@​charliermarsh](https://togithub.com/charliermarsh) in [astral-sh/ruff#3600 - \[`pyupgrade`] Flag PEP 585 and PEP 604 violations in quoted annotations by [@​charliermarsh](https://togithub.com/charliermarsh) in [astral-sh/ruff#3593 - \[`pyupgrade`] Enable autofix for annotations within 'simple' string literals by [@​charliermarsh](https://togithub.com/charliermarsh) in [astral-sh/ruff#3657 - \[`pyflakes`] Add autofix functionality for `F523` ([#​3613](https://togithub.com/charliermarsh/ruff/issues/3613)) by [@​JonathanPlasse](https://togithub.com/JonathanPlasse) in [astral-sh/ruff#3613 - \[`flake8-bandit`]: Implement deny-list rules for suspicious member calls by [@​colin99d](https://togithub.com/colin99d) in [astral-sh/ruff#3239 - \[`flake8-annotations`] Add autofix for `ANN204` with magic methods by [@​JonathanPlasse](https://togithub.com/JonathanPlasse) in [astral-sh/ruff#3633 - \[`pylint`] Implement `binary-op-exception` (`PLW0711`) by [@​latonis](https://togithub.com/latonis) in [astral-sh/ruff#3639 - \[`flake8-django`]: Implement rule DJ012 by [@​dhruvmanila](https://togithub.com/dhruvmanila) in [astral-sh/ruff#3659 ##### Bug Fixes - Check exclusions prior to resolving `pyproject.toml` files by [@​charliermarsh](https://togithub.com/charliermarsh) in [astral-sh/ruff#3588 - Fix D417 false positive by [@​JonathanPlasse](https://togithub.com/JonathanPlasse) in [astral-sh/ruff#3596 - Avoid removing comment hash for noqa's with trailing content by [@​charliermarsh](https://togithub.com/charliermarsh) in [astral-sh/ruff#3589 - Avoid panics for implicitly-concatenated docstrings by [@​charliermarsh](https://togithub.com/charliermarsh) in [astral-sh/ruff#3584 - Fix infinite loop due to rules `D207` & `W605` by [@​vlindhol](https://togithub.com/vlindhol) in [astral-sh/ruff#3609 - Avoid trimming escaped whitespace in D210 by [@​charliermarsh](https://togithub.com/charliermarsh) in [astral-sh/ruff#3635 - Handle `UP032` autofix with adjacent keywords by [@​JonathanPlasse](https://togithub.com/JonathanPlasse) in [astral-sh/ruff#3636 - Consider same-site fixes to be overlapping by [@​charliermarsh](https://togithub.com/charliermarsh) in [astral-sh/ruff#3638 - Avoid `RUF007` fixes for more than two arguments by [@​JonathanPlasse](https://togithub.com/JonathanPlasse) in [astral-sh/ruff#3654 - Allow `pairwise` diagnostics for `zip(..., strict=True)` by [@​charliermarsh](https://togithub.com/charliermarsh) in [astral-sh/ruff#3669 - isort: fix bad interaction between `force-sort-within-sections` and `force-to-top` by [@​bluetech](https://togithub.com/bluetech) in [astral-sh/ruff#3645 - Gracefully handle lint panics by [@​MichaReiser](https://togithub.com/MichaReiser) in [astral-sh/ruff#3509 - Fix TRY300 false positive by [@​JonathanPlasse](https://togithub.com/JonathanPlasse) in [astral-sh/ruff#3634 - Avoid raising PEP 604 errors with forward-referenced members by [@​charliermarsh](https://togithub.com/charliermarsh) in [astral-sh/ruff#3640 - Avoid attempting infinite `open` fix with re-bound builtin by [@​charliermarsh](https://togithub.com/charliermarsh) in [astral-sh/ruff#3650 - Check indentation level when executing E231 by [@​kyoto7250](https://togithub.com/kyoto7250) in [astral-sh/ruff#3668 - Flag, but don't fix, unused imports (`F401`) in `ModuleNotFoundError` blocks by [@​charliermarsh](https://togithub.com/charliermarsh) in [astral-sh/ruff#3658 #### New Contributors - [@​Rogdham](https://togithub.com/Rogdham) made their first contribution in [astral-sh/ruff#3607 - [@​vlindhol](https://togithub.com/vlindhol) made their first contribution in [astral-sh/ruff#3609 - [@​dhruvmanila](https://togithub.com/dhruvmanila) made their first contribution in [astral-sh/ruff#3605 - [@​luke396](https://togithub.com/luke396) made their first contribution in [astral-sh/ruff#3604 - [@​fuziontech](https://togithub.com/fuziontech) made their first contribution in [astral-sh/ruff#3641 **Full Changelog**: astral-sh/ruff@v0.0.257...v0.0.258 </details> --- ### Configuration 📅 **Schedule**: Branch creation - At any time (no schedule defined), Automerge - At any time (no schedule defined). 🚦 **Automerge**: Enabled. ♻ **Rebasing**: Whenever PR becomes conflicted, 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/allenporter/flux-local). <!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiIzNS4xNC4yIiwidXBkYXRlZEluVmVyIjoiMzUuMTQuMiJ9--> Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Summary,
Today, we avoid applying any autofixes for code within deferred string type annotations. For example:
This PR enables autofix for code within deferred string type annotations in certain cases. Namely, we allow these snippets to be fixed if and only if the annotation is "simple", defined here as not containing any implicit concatenations or escaped characters. For example, we don't even try to fix this:
...which is valid, but unusual.
Another way to think of this definition is: can we accurately predict the source code location for expressions within the string? If a string is "simple", then we can do a "located parse" starting at the beginning of the content, and all the parsed locations will be correct within the indexing scheme of the parent.
Note that if an annotation is "complex", we fallback to our existing behavior, whereby we modify the location of every located expression within the parsed annotation with the location of the string itself. So, e.g., the
int
in"Li" "st[int]"
is given the location of the entire string annotation.Closes #3656.
Closes #3508.