-
Notifications
You must be signed in to change notification settings - Fork 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
Use ruff line-length in format_dev #6870
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,6 +18,7 @@ use imara_diff::{diff, Algorithm}; | |
use indicatif::ProgressStyle; | ||
#[cfg_attr(feature = "singlethreaded", allow(unused_imports))] | ||
use rayon::iter::{IntoParallelIterator, ParallelIterator}; | ||
use ruff::line_width::LineLength; | ||
use serde::Deserialize; | ||
use similar::{ChangeTag, TextDiff}; | ||
use tempfile::NamedTempFile; | ||
|
@@ -36,10 +37,17 @@ use ruff_formatter::{FormatError, LineWidth, PrintError}; | |
use ruff_python_formatter::{ | ||
format_module, FormatModuleError, MagicTrailingComma, PyFormatOptions, | ||
}; | ||
use ruff_workspace::resolver::python_files_in_path; | ||
use ruff_workspace::resolver::{python_files_in_path, PyprojectConfig, Resolver}; | ||
|
||
/// Find files that ruff would check so we can format them. Adapted from `ruff_cli`. | ||
fn ruff_check_paths(dirs: &[PathBuf]) -> anyhow::Result<Vec<Result<DirEntry, ignore::Error>>> { | ||
#[allow(clippy::type_complexity)] | ||
fn ruff_check_paths( | ||
dirs: &[PathBuf], | ||
) -> anyhow::Result<( | ||
Vec<Result<DirEntry, ignore::Error>>, | ||
Resolver, | ||
PyprojectConfig, | ||
)> { | ||
let args_matches = FormatCommand::command() | ||
.no_binary_name(true) | ||
.get_matches_from(dirs); | ||
|
@@ -57,11 +65,11 @@ fn ruff_check_paths(dirs: &[PathBuf]) -> anyhow::Result<Vec<Result<DirEntry, ign | |
FilePattern::Builtin("*.pyi"), | ||
]) | ||
.unwrap(); | ||
let (paths, _resolver) = python_files_in_path(&cli.files, &pyproject_config, &overrides)?; | ||
let (paths, resolver) = python_files_in_path(&cli.files, &pyproject_config, &overrides)?; | ||
if paths.is_empty() { | ||
bail!("no python files in {:?}", dirs) | ||
} | ||
Ok(paths) | ||
Ok((paths, resolver, pyproject_config)) | ||
} | ||
|
||
/// Collects statistics over the formatted files to compute the Jaccard index or the similarity | ||
|
@@ -448,7 +456,7 @@ fn format_dev_project( | |
|
||
// Find files to check (or in this case, format twice). Adapted from ruff_cli | ||
// First argument is ignored | ||
let paths = ruff_check_paths(files)?; | ||
let (paths, resolver, pyproject_config) = ruff_check_paths(files)?; | ||
|
||
let results = { | ||
let pb_span = | ||
|
@@ -461,7 +469,14 @@ fn format_dev_project( | |
#[cfg(feature = "singlethreaded")] | ||
let iter = { paths.into_iter() }; | ||
iter.map(|dir_entry| { | ||
let result = format_dir_entry(dir_entry, stability_check, write, &black_options); | ||
let result = format_dir_entry( | ||
dir_entry, | ||
stability_check, | ||
write, | ||
&black_options, | ||
&resolver, | ||
&pyproject_config, | ||
); | ||
pb_span.pb_inc(1); | ||
result | ||
}) | ||
|
@@ -517,6 +532,8 @@ fn format_dir_entry( | |
stability_check: bool, | ||
write: bool, | ||
options: &BlackOptions, | ||
resolver: &Resolver, | ||
pyproject_config: &PyprojectConfig, | ||
) -> anyhow::Result<(Result<Statistics, CheckFileError>, PathBuf), Error> { | ||
let dir_entry = match dir_entry.context("Iterating the files in the repository failed") { | ||
Ok(dir_entry) => dir_entry, | ||
|
@@ -528,10 +545,19 @@ fn format_dir_entry( | |
return Ok((Ok(Statistics::default()), file)); | ||
} | ||
|
||
let file = dir_entry.path().to_path_buf(); | ||
let options = options.to_py_format_options(&file); | ||
let path = dir_entry.path().to_path_buf(); | ||
let mut options = options.to_py_format_options(&path); | ||
|
||
let settings = resolver.resolve(&path, pyproject_config); | ||
// That's a bad way of doing this but it's not worth doing something better for format_dev | ||
if settings.line_length != LineLength::default() { | ||
let line_width = LineWidth::try_from(settings.line_length.value()) | ||
.expect("Configured line length is too large for the formatter"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i still don't like this divergence There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can do |
||
options = options.with_line_width(line_width); | ||
} | ||
|
||
// Handle panics (mostly in `debug_assert!`) | ||
let result = match catch_unwind(|| format_dev_file(&file, stability_check, write, options)) { | ||
let result = match catch_unwind(|| format_dev_file(&path, stability_check, write, options)) { | ||
Ok(result) => result, | ||
Err(panic) => { | ||
if let Some(message) = panic.downcast_ref::<String>() { | ||
|
@@ -550,7 +576,7 @@ fn format_dir_entry( | |
} | ||
} | ||
}; | ||
Ok((result, file)) | ||
Ok((result, path)) | ||
} | ||
|
||
/// A compact diff that only shows a header and changes, but nothing unchanged. This makes 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
Oops, something went wrong.
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.
Is this because you're trying to see if it's not the default? Why can't we just use
settings.line_length
? Because it will be wrong for projects in the formatter CI that use Black but not Ruff?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.
yes, this only exists to have a (dummy) usage for the line length without breaking the formatter ecosystem checks
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.
Should
Settings.line_length
be anOption<LineLength>
if that's something we want to distinguish?