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

flake8_to_ruff: support isort options #2082

Merged
merged 11 commits into from Jan 22, 2023

Conversation

shannonrothe
Copy link
Contributor

@shannonrothe shannonrothe commented Jan 22, 2023

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 the flake8_to_ruff converter.

For the isort options, I've chosen to support only src_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

Copy link
Member

@charliermarsh charliermarsh left a 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!

src/flake8_to_ruff/isort.rs Outdated Show resolved Hide resolved
src/flake8_to_ruff/converter.rs Outdated Show resolved Hide resolved
@charliermarsh
Copy link
Member

For posterity: relevant issue is #1749.

@shannonrothe
Copy link
Contributor Author

For posterity: relevant issue is #1749.

Added to description 👌🏼

@not-my-profile
Copy link
Contributor

I am not sure if this PR should close #1749 given that isort also has other settings not implemented by this PR.

@shannonrothe
Copy link
Contributor Author

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 Line-Length isort option?

@charliermarsh
Copy link
Member

Ah yeah, sorry, it shouldn't close #1749 -- it should just reference it, like "See: #1749" or similar.

@charliermarsh
Copy link
Member

Another question - we could probably build off this PR to alleviate #1567 right? Is there a counterpart option in Ruff for the Line-Length isort option?

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 :)

@charliermarsh
Copy link
Member

Relatedly, everything in this section of the configuration options can be extracted from the isort section in a pyproject.toml, like you're doing here for src.

struct Pyproject {
tool: Option<Tools>,
}

pub fn parse_black_options<P: AsRef<Path>>(path: P) -> Result<Option<Black>> {
Copy link
Member

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.

Copy link
Member

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.)

Copy link
Contributor Author

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();

Copy link
Contributor

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.

Copy link
Contributor Author

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).

Copy link
Contributor Author

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();

Copy link
Contributor Author

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 👍🏼

Copy link
Member

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.

Copy link
Member

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.

@charliermarsh charliermarsh merged commit 36fb8f7 into astral-sh:main Jan 22, 2023
renovate bot added a commit to ixm-one/pytest-cmake-presets that referenced this pull request Jan 22, 2023
[![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
[@&#8203;figsoda](https://togithub.com/figsoda) in
[astral-sh/ruff#2069
- feat: implementation for TRY004 by
[@&#8203;sbrugman](https://togithub.com/sbrugman) in
[astral-sh/ruff#2066
- ICN001 import-alias-is-not-conventional should check "from" imports by
[@&#8203;Zeddicus414](https://togithub.com/Zeddicus414) in
[astral-sh/ruff#2070
- Update link to Pylint parity tracking issue by
[@&#8203;cosmojg](https://togithub.com/cosmojg) in
[astral-sh/ruff#2074
- ICN001 check from imports that have no alias by
[@&#8203;Zeddicus414](https://togithub.com/Zeddicus414) in
[astral-sh/ruff#2072
- Index source code upfront to power (row, column) lookups by
[@&#8203;charliermarsh](https://togithub.com/charliermarsh) in
[astral-sh/ruff#1990
- Remove remaining `ropey` usages by
[@&#8203;charliermarsh](https://togithub.com/charliermarsh) in
[astral-sh/ruff#2076
- Include package path in cache key by
[@&#8203;charliermarsh](https://togithub.com/charliermarsh) in
[astral-sh/ruff#2077
- feat: update scripts to new rules structure by
[@&#8203;sbrugman](https://togithub.com/sbrugman) in
[astral-sh/ruff#2078
- Base `INP` check on package inference by
[@&#8203;charliermarsh](https://togithub.com/charliermarsh) in
[astral-sh/ruff#2079
- Improve generator precedence operations by
[@&#8203;charliermarsh](https://togithub.com/charliermarsh) in
[astral-sh/ruff#2080
- Support decorators in source code generator by
[@&#8203;charliermarsh](https://togithub.com/charliermarsh) in
[astral-sh/ruff#2081
- feat: enable autofix for TRY004 by
[@&#8203;sbrugman](https://togithub.com/sbrugman) in
[astral-sh/ruff#2084
- Refactor, decouple and support "PL" by
[@&#8203;not-my-profile](https://togithub.com/not-my-profile) in
[astral-sh/ruff#2051
- \[`pep8-naming`]\[`N806`] Don't mark `TypeVar` & `NewType` Assignment
as Errors by [@&#8203;saadmk11](https://togithub.com/saadmk11) in
[astral-sh/ruff#2085
- Update linters pypi links to latest version by
[@&#8203;alonme](https://togithub.com/alonme) in
[astral-sh/ruff#2062
- flake8\_to_ruff: support `isort` options by
[@&#8203;shannonrothe](https://togithub.com/shannonrothe) in
[astral-sh/ruff#2082
- Update RustPython to fix `Dict.keys` type by
[@&#8203;harupy](https://togithub.com/harupy) in
[astral-sh/ruff#2086

#### New Contributors

- [@&#8203;figsoda](https://togithub.com/figsoda) made their first
contribution in
[astral-sh/ruff#2069
- [@&#8203;cosmojg](https://togithub.com/cosmojg) made their first
contribution in
[astral-sh/ruff#2074
- [@&#8203;alonme](https://togithub.com/alonme) made their first
contribution in
[astral-sh/ruff#2062
- [@&#8203;shannonrothe](https://togithub.com/shannonrothe) made their
first contribution in
[astral-sh/ruff#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 <bot@renovateapp.com>
Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants