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鈥檒l 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
9 changes: 8 additions & 1 deletion flake8_to_ruff/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,19 @@ fn main() -> Result<()> {
// Read the pyproject.toml file.
let black = cli
.pyproject
.clone()
.map(flake8_to_ruff::parse_black_options)
.transpose()?
.flatten();

let isort = cli
.pyproject
.map(flake8_to_ruff::parse_isort_options)
.transpose()?
.flatten();

// Create Ruff's pyproject.toml section.
let pyproject = flake8_to_ruff::convert(&config, black.as_ref(), cli.plugin)?;
let pyproject = flake8_to_ruff::convert(&config, black.as_ref(), isort.as_ref(), cli.plugin)?;
println!("{}", toml_edit::easy::to_string_pretty(&pyproject)?);

Ok(())
Expand Down
22 changes: 22 additions & 0 deletions src/flake8_to_ruff/converter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use anyhow::Result;
use colored::Colorize;

use super::black::Black;
use super::isort::ISort;
use super::plugin::Plugin;
use super::{parser, plugin};
use crate::registry::RuleSelector;
Expand All @@ -24,6 +25,7 @@ use crate::warn_user;
pub fn convert(
config: &HashMap<String, HashMap<String, Option<String>>>,
black: Option<&Black>,
isort: Option<&ISort>,
shannonrothe marked this conversation as resolved.
Show resolved Hide resolved
plugins: Option<Vec<Plugin>>,
) -> Result<Pyproject> {
// Extract the Flake8 section.
Expand Down Expand Up @@ -389,6 +391,19 @@ pub fn convert(
}
}

if let Some(isort) = isort {
if let Some(src_paths) = &isort.src_paths {
match options.src.as_mut() {
Some(src) => {
src.extend(src_paths.clone());
}
None => {
options.src = Some(src_paths.clone());
}
}
}
}

// Create the pyproject.toml.
Ok(Pyproject::new(options))
}
Expand All @@ -413,6 +428,7 @@ mod tests {
&HashMap::from([("flake8".to_string(), HashMap::default())]),
None,
None,
None,
)?;
let expected = Pyproject::new(Options {
allowed_confusables: None,
Expand Down Expand Up @@ -476,6 +492,7 @@ mod tests {
HashMap::from([("max-line-length".to_string(), Some("100".to_string()))]),
)]),
None,
None,
Some(vec![]),
)?;
let expected = Pyproject::new(Options {
Expand Down Expand Up @@ -540,6 +557,7 @@ mod tests {
HashMap::from([("max_line_length".to_string(), Some("100".to_string()))]),
)]),
None,
None,
Some(vec![]),
)?;
let expected = Pyproject::new(Options {
Expand Down Expand Up @@ -604,6 +622,7 @@ mod tests {
HashMap::from([("max_line_length".to_string(), Some("abc".to_string()))]),
)]),
None,
None,
Some(vec![]),
)?;
let expected = Pyproject::new(Options {
Expand Down Expand Up @@ -668,6 +687,7 @@ mod tests {
HashMap::from([("inline-quotes".to_string(), Some("single".to_string()))]),
)]),
None,
None,
Some(vec![]),
)?;
let expected = Pyproject::new(Options {
Expand Down Expand Up @@ -740,6 +760,7 @@ mod tests {
)]),
)]),
None,
None,
Some(vec![Plugin::Flake8Docstrings]),
)?;
let expected = Pyproject::new(Options {
Expand Down Expand Up @@ -812,6 +833,7 @@ mod tests {
)]),
None,
None,
None,
)?;
let expected = Pyproject::new(Options {
allowed_confusables: None,
Expand Down
31 changes: 31 additions & 0 deletions src/flake8_to_ruff/isort.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
//! Extract isort configuration settings from a pyproject.toml.

use std::path::Path;

use anyhow::Result;
use serde::{Deserialize, Serialize};

// use crate::settings::types::PythonVersion;
shannonrothe marked this conversation as resolved.
Show resolved Hide resolved

#[derive(Debug, PartialEq, Eq, Serialize, Deserialize, Default)]
pub struct ISort {
shannonrothe marked this conversation as resolved.
Show resolved Hide resolved
#[serde(alias = "src-paths", alias = "src_paths")]
pub src_paths: Option<Vec<String>>,
}

#[derive(Debug, PartialEq, Eq, Serialize, Deserialize)]
struct Tools {
isort: Option<ISort>,
}

#[derive(Debug, PartialEq, Eq, Serialize, Deserialize)]
struct Pyproject {
tool: Option<Tools>,
}

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))
}
2 changes: 2 additions & 0 deletions src/flake8_to_ruff/mod.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
mod black;
mod converter;
mod isort;
mod parser;
mod plugin;

pub use black::parse_black_options;
pub use converter::convert;
pub use isort::parse_isort_options;
pub use plugin::Plugin;