Skip to content

Commit

Permalink
Allow users to extend the set of included files via include (#3914)
Browse files Browse the repository at this point in the history
  • Loading branch information
charliermarsh authored Apr 12, 2023
1 parent 8ce2270 commit b999e4b
Show file tree
Hide file tree
Showing 7 changed files with 111 additions and 16 deletions.
31 changes: 18 additions & 13 deletions crates/ruff/src/resolver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ use rustc_hash::FxHashSet;
use ruff_python_stdlib::path::is_python_file;

use crate::fs;
use crate::jupyter::is_jupyter_notebook;
use crate::settings::configuration::Configuration;
use crate::settings::pyproject::settings_toml;
use crate::settings::{pyproject, AllSettings, Settings};
Expand Down Expand Up @@ -200,14 +199,6 @@ fn match_exclusion<P: AsRef<Path>, R: AsRef<Path>>(
exclusion.is_match(file_path) || exclusion.is_match(file_basename)
}

/// Return `true` if the [`DirEntry`] appears to be that of a Python file or jupyter notebook.
pub fn is_python_entry(entry: &DirEntry) -> bool {
(is_python_file(entry.path()) || is_jupyter_notebook(entry.path()))
&& !entry
.file_type()
.map_or(false, |file_type| file_type.is_dir())
}

/// Find all Python (`.py`, `.pyi` and `.ipynb` files) in a set of paths.
pub fn python_files_in_path(
paths: &[PathBuf],
Expand Down Expand Up @@ -325,10 +316,24 @@ pub fn python_files_in_path(
}

if result.as_ref().map_or(true, |entry| {
// Accept all files that are passed-in directly.
(entry.depth() == 0 && entry.file_type().map_or(false, |ft| ft.is_file()))
// Accept all Python files.
|| is_python_entry(entry)
if entry.depth() == 0 {
// Accept all files that are passed-in directly.
entry.file_type().map_or(false, |ft| ft.is_file())
} else {
// Otherwise, check if the file is included.
let path = entry.path();
let resolver = resolver.read().unwrap();
let settings = resolver.resolve(path, pyproject_strategy);
if settings.include.is_match(path) {
debug!("Included path via `include`: {:?}", path);
true
} else if settings.extend_include.is_match(path) {
debug!("Included path via `extend-include`: {:?}", path);
true
} else {
false
}
}
}) {
files.lock().unwrap().push(result);
}
Expand Down
31 changes: 30 additions & 1 deletion crates/ruff/src/settings/configuration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,18 +47,20 @@ pub struct Configuration {
pub exclude: Option<Vec<FilePattern>>,
pub extend: Option<PathBuf>,
pub extend_exclude: Vec<FilePattern>,
pub extend_include: Vec<FilePattern>,
pub external: Option<Vec<String>>,
pub fix: Option<bool>,
pub fix_only: Option<bool>,
pub force_exclude: Option<bool>,
pub format: Option<SerializationFormat>,
pub ignore_init_module_imports: Option<bool>,
pub include: Option<Vec<FilePattern>>,
pub line_length: Option<usize>,
pub namespace_packages: Option<Vec<PathBuf>>,
pub required_version: Option<Version>,
pub respect_gitignore: Option<bool>,
pub show_source: Option<bool>,
pub show_fixes: Option<bool>,
pub show_source: Option<bool>,
pub src: Option<Vec<PathBuf>>,
pub target_version: Option<PythonVersion>,
pub task_tags: Option<Vec<String>>,
Expand Down Expand Up @@ -148,12 +150,33 @@ impl Configuration {
.collect()
})
.unwrap_or_default(),
extend_include: options
.extend_include
.map(|paths| {
paths
.into_iter()
.map(|pattern| {
let absolute = fs::normalize_path_to(&pattern, project_root);
FilePattern::User(pattern, absolute)
})
.collect()
})
.unwrap_or_default(),
external: options.external,
fix: options.fix,
fix_only: options.fix_only,
format: options.format,
force_exclude: options.force_exclude,
ignore_init_module_imports: options.ignore_init_module_imports,
include: options.include.map(|paths| {
paths
.into_iter()
.map(|pattern| {
let absolute = fs::normalize_path_to(&pattern, project_root);
FilePattern::User(pattern, absolute)
})
.collect()
}),
line_length: options.line_length,
namespace_packages: options
.namespace_packages
Expand Down Expand Up @@ -224,11 +247,17 @@ impl Configuration {
.into_iter()
.chain(self.extend_exclude.into_iter())
.collect(),
extend_include: config
.extend_include
.into_iter()
.chain(self.extend_include.into_iter())
.collect(),
external: self.external.or(config.external),
fix: self.fix.or(config.fix),
fix_only: self.fix_only.or(config.fix_only),
format: self.format.or(config.format),
force_exclude: self.force_exclude.or(config.force_exclude),
include: self.include.or(config.include),
ignore_init_module_imports: self
.ignore_init_module_imports
.or(config.ignore_init_module_imports),
Expand Down
5 changes: 5 additions & 0 deletions crates/ruff/src/settings/defaults.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ pub static EXCLUDE: Lazy<Vec<FilePattern>> = Lazy::new(|| {
]
});

pub static INCLUDE: Lazy<Vec<FilePattern>> =
Lazy::new(|| vec![FilePattern::Builtin("*.py"), FilePattern::Builtin("*.pyi")]);

impl Default for Settings {
fn default() -> Self {
Self {
Expand All @@ -66,9 +69,11 @@ impl Default for Settings {
dummy_variable_rgx: DUMMY_VARIABLE_RGX.clone(),
exclude: FilePatternSet::try_from_vec(EXCLUDE.clone()).unwrap(),
extend_exclude: FilePatternSet::default(),
extend_include: FilePatternSet::default(),
external: HashSet::default(),
force_exclude: false,
ignore_init_module_imports: false,
include: FilePatternSet::try_from_vec(INCLUDE.clone()).unwrap(),
line_length: LINE_LENGTH,
namespace_packages: vec![],
per_file_ignores: vec![],
Expand Down
8 changes: 6 additions & 2 deletions crates/ruff/src/settings/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@ pub struct Settings {
pub exclude: FilePatternSet,
pub extend_exclude: FilePatternSet,
pub force_exclude: bool,
pub include: FilePatternSet,
pub extend_include: FilePatternSet,
pub respect_gitignore: bool,
pub project_root: PathBuf,

Expand Down Expand Up @@ -155,10 +157,12 @@ impl Settings {
config.exclude.unwrap_or_else(|| defaults::EXCLUDE.clone()),
)?,
extend_exclude: FilePatternSet::try_from_vec(config.extend_exclude)?,
extend_include: FilePatternSet::try_from_vec(config.extend_include)?,
external: FxHashSet::from_iter(config.external.unwrap_or_default()),

force_exclude: config.force_exclude.unwrap_or(false),

include: FilePatternSet::try_from_vec(
config.include.unwrap_or_else(|| defaults::INCLUDE.clone()),
)?,
ignore_init_module_imports: config.ignore_init_module_imports.unwrap_or_default(),
line_length: config.line_length.unwrap_or(defaults::LINE_LENGTH),
namespace_packages: config.namespace_packages.unwrap_or_default(),
Expand Down
30 changes: 30 additions & 0 deletions crates/ruff/src/settings/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,22 @@ pub struct Options {
///
/// For more information on the glob syntax, refer to the [`globset` documentation](https://docs.rs/globset/latest/globset/#syntax).
pub extend_exclude: Option<Vec<String>>,
#[option(
default = "[]",
value_type = "list[str]",
example = r#"
# In addition to the standard set of inclusions, include `.pyw` files.
extend-include = ["*.pyw"]
"#
)]
/// A list of file patterns to include when linting, in addition to those
/// specified by `include`.
///
/// Inclusion are based on globs, and should be single-path patterns, like
/// `*.pyw`, to include any file with the `.pyw` extension.
///
/// For more information on the glob syntax, refer to the [`globset` documentation](https://docs.rs/globset/latest/globset/#syntax).
pub extend_include: Option<Vec<String>>,
#[option(
default = "[]",
value_type = "list[RuleSelector]",
Expand Down Expand Up @@ -253,6 +269,20 @@ pub struct Options {
/// that the import is either added to the module's `__all__` symbol, or
/// re-exported with a redundant alias (e.g., `import os as os`).
pub ignore_init_module_imports: Option<bool>,
#[option(
default = r#"["*.py", "*.pyi"]"#,
value_type = "list[str]",
example = r#"
include = ["*.py"]
"#
)]
/// A list of file patterns to include when linting.
///
/// Inclusion are based on globs, and should be single-path patterns, like
/// `*.pyw`, to include any file with the `.pyw` extension.
///
/// For more information on the glob syntax, refer to the [`globset` documentation](https://docs.rs/globset/latest/globset/#syntax).
pub include: Option<Vec<String>>,
#[option(
default = "88",
value_type = "int",
Expand Down
2 changes: 2 additions & 0 deletions crates/ruff_wasm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,11 +110,13 @@ pub fn defaultSettings() -> Result<JsValue, JsValue> {
exclude: None,
extend: None,
extend_exclude: None,
extend_include: None,
fix: None,
fix_only: None,
fixable: None,
force_exclude: None,
format: None,
include: None,
ignore_init_module_imports: None,
namespace_packages: None,
per_file_ignores: None,
Expand Down
20 changes: 20 additions & 0 deletions ruff.schema.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit b999e4b

Please sign in to comment.