-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
flake8_to_ruff: support isort
options
#2082
flake8_to_ruff: support isort
options
#2082
Conversation
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.
Thanks for getting involved!
For posterity: relevant issue is #1749. |
Added to description 👌🏼 |
I am not sure if this PR should close #1749 given that isort also has other settings not implemented by this PR. |
Fair enough, happy to remove from the description. Another question - we could probably build off this PR to alleviate #1567 right? Is there a counterpart option in Ruff for the |
It's actually a bit different -- that issue references a setting that tells isort to sort the imports by their string length. isort does have a line-length setting, and we should extract that and pass it along to Ruff (we have a top-level option for that), but it wouldn't close that issue :) |
Relatedly, everything in this section of the configuration options can be extracted from the |
src/flake8_to_ruff/black.rs
Outdated
struct Pyproject { | ||
tool: Option<Tools>, | ||
} | ||
|
||
pub fn parse_black_options<P: AsRef<Path>>(path: P) -> Result<Option<Black>> { |
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 should have one parse
function that returns ExternalConfig
. Right now, we're parsing the TOML twice into Pyproject
, then extracting tool.black
and tool.isort
respectively, then merging them back into a single struct.
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.
(Or, one parse function that returns Pyproject
to main.rs
, then create ExternalConfig
from Pyproject
.)
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.
Ah yep that makes sense.
Tools
has to own black
/isort
presumably for serde
(de)serialization. I'm trying to do something like:
pub fn parse<'a, P: AsRef<Path>>(path: P) -> Result<ExternalConfig<'a>> {
let contents = std::fs::read_to_string(path)?;
let pyproject = toml_edit::easy::from_str::<Pyproject>(&contents)?;
Ok(pyproject
.tool
.map(|tool| ExternalConfig {
black: tool.black.as_ref(),
isort: tool.isort.as_ref(),
})
.unwrap_or_default())
}
but the tool.{black,isort}.as_ref()
lines complain because we're referencing tool.{black,isort}
which is not owned by the current function. Any ideas? :)
Or, alternatively, if flake8_to_ruff::parse
returns Pyproject
and we try and produce an ExternalConfig
from main.rs
:
let pyproject = cli.pyproject.map(flake8_to_ruff::parse).transpose()?;
let external_config = pyproject
.map(|pyproject| {
pyproject
.tool
.map(|tool| ExternalConfig {
// these lines aren't happy
black: tool.black.as_ref(),
isort: tool.isort.as_ref(),
})
.unwrap_or_default()
})
.unwrap_or_default();
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.
The signature doesn't make sense:
pub fn parse<'a, P: AsRef<Path>>(path: P) -> Result<ExternalConfig<'a>> {
The liftetime in the return type has to come from somewhere ... you can either change the input to text: &'a str
or make the return type owned.
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.
Sorry @not-my-profile, I've changed flake8_to_ruff::parse
to return Pyproject
instead, which circumvents the above issue.
The current issue is mapping out of Tools
(where black
/isort
are owned) into ExternalConfig
(where black
/isort
are references).
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 seems to work, but is there a preferred (more idiomatic) way?
let mut external_config = ExternalConfig::default();
let pyproject = cli.pyproject.map(flake8_to_ruff::parse).transpose()?;
if let Some(pyproject) = &pyproject {
if let Some(tool) = &pyproject.tool {
external_config = ExternalConfig {
black: tool.black.as_ref(),
isort: tool.isort.as_ref(),
};
}
}
Edit: maybe this?
let external_config = pyproject
.as_ref()
.map(|pyproject| {
pyproject
.tool
.as_ref()
.map(|tool| ExternalConfig {
black: tool.black.as_ref(),
isort: tool.isort.as_ref(),
})
.unwrap_or_default()
})
.unwrap_or_default();
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.
Implemented suggestions here @charliermarsh: 4127eb4 👍🏼
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.
Awesome -- will review in a bit.
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.
Thank you! I tweaked it a little bit to remove some levels of nesting by using .and_then
.
[![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.229` -> `^0.0.230` | [![age](https://badges.renovateapi.com/packages/pypi/ruff/0.0.230/age-slim)](https://docs.renovatebot.com/merge-confidence/) | [![adoption](https://badges.renovateapi.com/packages/pypi/ruff/0.0.230/adoption-slim)](https://docs.renovatebot.com/merge-confidence/) | [![passing](https://badges.renovateapi.com/packages/pypi/ruff/0.0.230/compatibility-slim/0.0.229)](https://docs.renovatebot.com/merge-confidence/) | [![confidence](https://badges.renovateapi.com/packages/pypi/ruff/0.0.230/confidence-slim/0.0.229)](https://docs.renovatebot.com/merge-confidence/) | --- ### Release Notes <details> <summary>charliermarsh/ruff</summary> ### [`v0.0.230`](https://togithub.com/charliermarsh/ruff/releases/tag/v0.0.230) [Compare Source](https://togithub.com/charliermarsh/ruff/compare/v0.0.229...v0.0.230) #### What's Changed - fix: pin rustpython to the same revision to fix cargo vendor by [@​figsoda](https://togithub.com/figsoda) in [https://github.com/charliermarsh/ruff/pull/2069](https://togithub.com/charliermarsh/ruff/pull/2069) - feat: implementation for TRY004 by [@​sbrugman](https://togithub.com/sbrugman) in [https://github.com/charliermarsh/ruff/pull/2066](https://togithub.com/charliermarsh/ruff/pull/2066) - ICN001 import-alias-is-not-conventional should check "from" imports by [@​Zeddicus414](https://togithub.com/Zeddicus414) in [https://github.com/charliermarsh/ruff/pull/2070](https://togithub.com/charliermarsh/ruff/pull/2070) - Update link to Pylint parity tracking issue by [@​cosmojg](https://togithub.com/cosmojg) in [https://github.com/charliermarsh/ruff/pull/2074](https://togithub.com/charliermarsh/ruff/pull/2074) - ICN001 check from imports that have no alias by [@​Zeddicus414](https://togithub.com/Zeddicus414) in [https://github.com/charliermarsh/ruff/pull/2072](https://togithub.com/charliermarsh/ruff/pull/2072) - Index source code upfront to power (row, column) lookups by [@​charliermarsh](https://togithub.com/charliermarsh) in [https://github.com/charliermarsh/ruff/pull/1990](https://togithub.com/charliermarsh/ruff/pull/1990) - Remove remaining `ropey` usages by [@​charliermarsh](https://togithub.com/charliermarsh) in [https://github.com/charliermarsh/ruff/pull/2076](https://togithub.com/charliermarsh/ruff/pull/2076) - Include package path in cache key by [@​charliermarsh](https://togithub.com/charliermarsh) in [https://github.com/charliermarsh/ruff/pull/2077](https://togithub.com/charliermarsh/ruff/pull/2077) - feat: update scripts to new rules structure by [@​sbrugman](https://togithub.com/sbrugman) in [https://github.com/charliermarsh/ruff/pull/2078](https://togithub.com/charliermarsh/ruff/pull/2078) - Base `INP` check on package inference by [@​charliermarsh](https://togithub.com/charliermarsh) in [https://github.com/charliermarsh/ruff/pull/2079](https://togithub.com/charliermarsh/ruff/pull/2079) - Improve generator precedence operations by [@​charliermarsh](https://togithub.com/charliermarsh) in [https://github.com/charliermarsh/ruff/pull/2080](https://togithub.com/charliermarsh/ruff/pull/2080) - Support decorators in source code generator by [@​charliermarsh](https://togithub.com/charliermarsh) in [https://github.com/charliermarsh/ruff/pull/2081](https://togithub.com/charliermarsh/ruff/pull/2081) - feat: enable autofix for TRY004 by [@​sbrugman](https://togithub.com/sbrugman) in [https://github.com/charliermarsh/ruff/pull/2084](https://togithub.com/charliermarsh/ruff/pull/2084) - Refactor, decouple and support "PL" by [@​not-my-profile](https://togithub.com/not-my-profile) in [https://github.com/charliermarsh/ruff/pull/2051](https://togithub.com/charliermarsh/ruff/pull/2051) - \[`pep8-naming`]\[`N806`] Don't mark `TypeVar` & `NewType` Assignment as Errors by [@​saadmk11](https://togithub.com/saadmk11) in [https://github.com/charliermarsh/ruff/pull/2085](https://togithub.com/charliermarsh/ruff/pull/2085) - Update linters pypi links to latest version by [@​alonme](https://togithub.com/alonme) in [https://github.com/charliermarsh/ruff/pull/2062](https://togithub.com/charliermarsh/ruff/pull/2062) - flake8\_to_ruff: support `isort` options by [@​shannonrothe](https://togithub.com/shannonrothe) in [https://github.com/charliermarsh/ruff/pull/2082](https://togithub.com/charliermarsh/ruff/pull/2082) - Update RustPython to fix `Dict.keys` type by [@​harupy](https://togithub.com/harupy) in [https://github.com/charliermarsh/ruff/pull/2086](https://togithub.com/charliermarsh/ruff/pull/2086) #### New Contributors - [@​figsoda](https://togithub.com/figsoda) made their first contribution in [https://github.com/charliermarsh/ruff/pull/2069](https://togithub.com/charliermarsh/ruff/pull/2069) - [@​cosmojg](https://togithub.com/cosmojg) made their first contribution in [https://github.com/charliermarsh/ruff/pull/2074](https://togithub.com/charliermarsh/ruff/pull/2074) - [@​alonme](https://togithub.com/alonme) made their first contribution in [https://github.com/charliermarsh/ruff/pull/2062](https://togithub.com/charliermarsh/ruff/pull/2062) - [@​shannonrothe](https://togithub.com/shannonrothe) made their first contribution in [https://github.com/charliermarsh/ruff/pull/2082](https://togithub.com/charliermarsh/ruff/pull/2082) **Full Changelog**: astral-sh/ruff@v0.0.229...v0.0.230 </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:eyJjcmVhdGVkSW5WZXIiOiIzNC4xMDguNCIsInVwZGF0ZWRJblZlciI6IjM0LjEwOC40In0=--> Signed-off-by: Renovate Bot <[email protected]> Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Hey @charliermarsh 👋🏼 I've been learning Rust for a little bit but haven't contributed any PRs until this one, so excuse me if it's a little rough. I mostly followed what was already in place for picking up
Black
options in theflake8_to_ruff
converter.For the
isort
options, I've chosen to support onlysrc_paths
for now as a simple starting point (this could be extended upon).Admittedly, I'm not super familiar with the Python ecosystem and the associated tools (isort/Black), so let me know if I'm missing anything obvious 👌🏼
See: #1749