-
Notifications
You must be signed in to change notification settings - Fork 894
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鈥檒l occasionally send you account related emails.
Already on GitHub? Sign in to your account
flake8_to_ruff: support isort
options
#2082
Merged
charliermarsh
merged 11 commits into
astral-sh:main
from
shannonrothe:flake8-to-ruff-isort
Jan 22, 2023
Merged
Changes from 7 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
d2e882c
flake8_to_ruff: support `isort` options
shannonrothe 37a87df
introduce `ToolConfigs` struct
shannonrothe aa15c6a
rename `ToolConfigs` -> `ExternalConfig`
shannonrothe 8c8c497
introduce lifetimes on `ExternalConfig`
shannonrothe 0780fa0
consolidate `Tools` and `Pyproject` structs
shannonrothe 4852baf
derive `Default` trait
shannonrothe e8b270e
clippy
shannonrothe 7f03ab5
fmt
shannonrothe 4127eb4
avoid parsing TOML twice
shannonrothe c3dacad
Merge branch 'refs/heads/main' into flake8-to-ruff-isort
charliermarsh 6852ef0
Unfurl maps
charliermarsh File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
use super::{black::Black, isort::Isort}; | ||
|
||
#[derive(Default)] | ||
pub struct ExternalConfig<'a> { | ||
pub black: Option<&'a Black>, | ||
pub isort: Option<&'a Isort>, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
//! Extract isort configuration settings from a pyproject.toml. | ||
|
||
use std::path::Path; | ||
|
||
use anyhow::Result; | ||
use serde::{Deserialize, Serialize}; | ||
|
||
use super::pyproject::Pyproject; | ||
|
||
/// The [isort configuration](https://pycqa.github.io/isort/docs/configuration/config_files.html). | ||
#[derive(Debug, PartialEq, Eq, Serialize, Deserialize, Default)] | ||
pub struct Isort { | ||
#[serde(alias = "src-paths", alias = "src_paths")] | ||
pub src_paths: Option<Vec<String>>, | ||
} | ||
|
||
pub fn parse_isort_options<P: AsRef<Path>>(path: P) -> Result<Option<Isort>> { | ||
let contents = std::fs::read_to_string(path)?; | ||
Ok(toml_edit::easy::from_str::<Pyproject>(&contents)? | ||
.tool | ||
.and_then(|tool| tool.isort)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,13 @@ | ||
mod black; | ||
mod converter; | ||
mod external_config; | ||
mod isort; | ||
mod parser; | ||
mod plugin; | ||
mod pyproject; | ||
|
||
pub use black::parse_black_options; | ||
pub use converter::convert; | ||
pub use external_config::ExternalConfig; | ||
pub use isort::parse_isort_options; | ||
pub use plugin::Plugin; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
use serde::{Deserialize, Serialize}; | ||
|
||
use super::{black::Black, isort::Isort}; | ||
|
||
#[derive(Debug, PartialEq, Eq, Serialize, Deserialize)] | ||
pub struct Tools { | ||
pub black: Option<Black>, | ||
pub isort: Option<Isort>, | ||
} | ||
|
||
#[derive(Debug, PartialEq, Eq, Serialize, Deserialize)] | ||
pub struct Pyproject { | ||
pub tool: Option<Tools>, | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 returnsExternalConfig
. Right now, we're parsing the TOML twice intoPyproject
, then extractingtool.black
andtool.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
tomain.rs
, then createExternalConfig
fromPyproject
.)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 ownblack
/isort
presumably forserde
(de)serialization. I'm trying to do something like:but the
tool.{black,isort}.as_ref()
lines complain because we're referencingtool.{black,isort}
which is not owned by the current function. Any ideas? :)Or, alternatively, if
flake8_to_ruff::parse
returnsPyproject
and we try and produce anExternalConfig
frommain.rs
: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:
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 returnPyproject
instead, which circumvents the above issue.The current issue is mapping out of
Tools
(whereblack
/isort
are owned) intoExternalConfig
(whereblack
/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?
Edit: maybe this?
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
.