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
16 changes: 4 additions & 12 deletions flake8_to_ruff/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,21 +48,13 @@ fn main() -> Result<()> {
let config = ini.load(cli.file).map_err(|msg| anyhow::anyhow!(msg))?;

// Read the pyproject.toml file.
let black = cli
let tool_configs = 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();
.map(flake8_to_ruff::parse_tool_configs)
.transpose()?;

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

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

use super::black::Black;
use super::isort::ISort;
use super::isort::Isort;
use super::plugin::Plugin;
use super::tool_configs::ToolConfigs;
use super::{parser, plugin};
use crate::registry::RuleSelector;
use crate::rules::flake8_pytest_style::types::{
Expand All @@ -24,8 +25,7 @@ use crate::warn_user;

pub fn convert(
config: &HashMap<String, HashMap<String, Option<String>>>,
black: Option<&Black>,
isort: Option<&ISort>,
tool_configs: Option<&ToolConfigs>,
plugins: Option<Vec<Plugin>>,
) -> Result<Pyproject> {
// Extract the Flake8 section.
Expand Down Expand Up @@ -379,26 +379,28 @@ pub fn convert(
}

// Extract any settings from the existing `pyproject.toml`.
if let Some(black) = black {
if let Some(line_length) = &black.line_length {
options.line_length = Some(*line_length);
}
if let Some(configs) = tool_configs {
if let Some(black) = &configs.black {
if let Some(line_length) = &black.line_length {
options.line_length = Some(*line_length);
}

if let Some(target_version) = &black.target_version {
if let Some(target_version) = target_version.iter().min() {
options.target_version = Some(*target_version);
if let Some(target_version) = &black.target_version {
if let Some(target_version) = target_version.iter().min() {
options.target_version = Some(*target_version);
}
}
}
}

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());
if let Some(isort) = &configs.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());
}
}
}
}
Expand All @@ -416,6 +418,7 @@ mod tests {

use super::super::plugin::Plugin;
use super::convert;
use super::ToolConfigs;
use crate::registry::RuleSelector;
use crate::rules::pydocstyle::settings::Convention;
use crate::rules::{flake8_quotes, pydocstyle};
Expand All @@ -428,7 +431,6 @@ mod tests {
&HashMap::from([("flake8".to_string(), HashMap::default())]),
None,
None,
None,
)?;
let expected = Pyproject::new(Options {
allowed_confusables: None,
Expand Down Expand Up @@ -492,7 +494,6 @@ 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 @@ -557,7 +558,6 @@ 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 @@ -622,7 +622,6 @@ 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 @@ -687,7 +686,6 @@ 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 @@ -760,7 +758,6 @@ mod tests {
)]),
)]),
None,
None,
Some(vec![Plugin::Flake8Docstrings]),
)?;
let expected = Pyproject::new(Options {
Expand Down Expand Up @@ -833,7 +830,6 @@ mod tests {
)]),
None,
None,
None,
)?;
let expected = Pyproject::new(Options {
allowed_confusables: None,
Expand Down
9 changes: 4 additions & 5 deletions src/flake8_to_ruff/isort.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,24 @@ use std::path::Path;
use anyhow::Result;
use serde::{Deserialize, Serialize};

// use crate::settings::types::PythonVersion;

/// The [isort configuration](https://pycqa.github.io/isort/docs/configuration/config_files.html).
#[derive(Debug, PartialEq, Eq, Serialize, Deserialize, Default)]
pub struct ISort {
pub struct Isort {
#[serde(alias = "src-paths", alias = "src_paths")]
pub src_paths: Option<Vec<String>>,
}

#[derive(Debug, PartialEq, Eq, Serialize, Deserialize)]
struct Tools {
isort: Option<ISort>,
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>> {
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
Expand Down
2 changes: 2 additions & 0 deletions src/flake8_to_ruff/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ mod converter;
mod isort;
mod parser;
mod plugin;
mod tool_configs;

pub use black::parse_black_options;
pub use converter::convert;
pub use isort::parse_isort_options;
pub use plugin::Plugin;
pub use tool_configs::parse_tool_configs;
17 changes: 17 additions & 0 deletions src/flake8_to_ruff/tool_configs.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
use std::path::Path;

use anyhow::Result;

use super::{black::Black, isort::Isort, parse_black_options, parse_isort_options};

pub struct ToolConfigs {
pub black: Option<Black>,
pub isort: Option<Isort>,
shannonrothe marked this conversation as resolved.
Show resolved Hide resolved
}

pub fn parse_tool_configs<P: AsRef<Path>>(path: P) -> Result<ToolConfigs> {
Ok(ToolConfigs {
black: parse_black_options(path.as_ref())?,
isort: parse_isort_options(path)?,
shannonrothe marked this conversation as resolved.
Show resolved Hide resolved
})
}