Skip to content

Commit 1f27d53

Browse files
authored
[ty] File inclusion and exclusion (astral-sh#18498)
1 parent 3c6c017 commit 1f27d53

File tree

23 files changed

+2724
-155
lines changed

23 files changed

+2724
-155
lines changed

Cargo.lock

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ quote = { version = "1.0.23" }
126126
rand = { version = "0.9.0" }
127127
rayon = { version = "1.10.0" }
128128
regex = { version = "1.10.2" }
129+
regex-automata = { version = "0.4.9" }
129130
rustc-hash = { version = "2.0.0" }
130131
rustc-stable-hash = { version = "0.1.2" }
131132
# When updating salsa, make sure to also update the revision in `fuzz/Cargo.toml`
@@ -165,7 +166,7 @@ tracing-subscriber = { version = "0.3.18", default-features = false, features =
165166
"env-filter",
166167
"fmt",
167168
"ansi",
168-
"smallvec",
169+
"smallvec"
169170
] }
170171
tryfn = { version = "0.2.1" }
171172
typed-arena = { version = "2.0.2" }
@@ -175,7 +176,11 @@ unicode-width = { version = "0.2.0" }
175176
unicode_names2 = { version = "1.2.2" }
176177
unicode-normalization = { version = "0.1.23" }
177178
url = { version = "2.5.0" }
178-
uuid = { version = "1.6.1", features = ["v4", "fast-rng", "macro-diagnostics"] }
179+
uuid = { version = "1.6.1", features = [
180+
"v4",
181+
"fast-rng",
182+
"macro-diagnostics",
183+
] }
179184
walkdir = { version = "2.3.2" }
180185
wasm-bindgen = { version = "0.2.92" }
181186
wasm-bindgen-test = { version = "0.3.42" }
@@ -210,8 +215,8 @@ must_use_candidate = "allow"
210215
similar_names = "allow"
211216
single_match_else = "allow"
212217
too_many_lines = "allow"
213-
needless_continue = "allow" # An explicit continue can be more readable, especially if the alternative is an empty block.
214-
unnecessary_debug_formatting = "allow" # too many instances, the display also doesn't quote the path which is often desired in logs where we use them the most often.
218+
needless_continue = "allow" # An explicit continue can be more readable, especially if the alternative is an empty block.
219+
unnecessary_debug_formatting = "allow" # too many instances, the display also doesn't quote the path which is often desired in logs where we use them the most often.
215220
# Without the hashes we run into a `rustfmt` bug in some snapshot tests, see #13250
216221
needless_raw_string_hashes = "allow"
217222
# Disallowed restriction lints

crates/ruff_db/src/diagnostic/mod.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -665,6 +665,9 @@ pub enum DiagnosticId {
665665

666666
/// No rule with the given name exists.
667667
UnknownRule,
668+
669+
/// A glob pattern doesn't follow the expected syntax.
670+
InvalidGlob,
668671
}
669672

670673
impl DiagnosticId {
@@ -699,6 +702,7 @@ impl DiagnosticId {
699702
DiagnosticId::Lint(name) => name.as_str(),
700703
DiagnosticId::RevealedType => "revealed-type",
701704
DiagnosticId::UnknownRule => "unknown-rule",
705+
DiagnosticId::InvalidGlob => "invalid-glob",
702706
}
703707
}
704708

crates/ruff_db/src/system/path.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,30 @@ impl SystemPath {
4545
SystemPath::from_std_path(dunce::simplified(self.as_std_path())).unwrap()
4646
}
4747

48+
/// Returns `true` if the `SystemPath` is absolute, i.e., if it is independent of
49+
/// the current directory.
50+
///
51+
/// * On Unix, a path is absolute if it starts with the root, so
52+
/// `is_absolute` and [`has_root`] are equivalent.
53+
///
54+
/// * On Windows, a path is absolute if it has a prefix and starts with the
55+
/// root: `c:\windows` is absolute, while `c:temp` and `\temp` are not.
56+
///
57+
/// # Examples
58+
///
59+
/// ```
60+
/// use ruff_db::system::SystemPath;
61+
///
62+
/// assert!(!SystemPath::new("foo.txt").is_absolute());
63+
/// ```
64+
///
65+
/// [`has_root`]: Utf8Path::has_root
66+
#[inline]
67+
#[must_use]
68+
pub fn is_absolute(&self) -> bool {
69+
self.0.is_absolute()
70+
}
71+
4872
/// Extracts the file extension, if possible.
4973
///
5074
/// The extension is:
@@ -538,6 +562,10 @@ impl SystemPathBuf {
538562
self.0.into_std_path_buf()
539563
}
540564

565+
pub fn into_string(self) -> String {
566+
self.0.into_string()
567+
}
568+
541569
#[inline]
542570
pub fn as_path(&self) -> &SystemPath {
543571
SystemPath::new(&self.0)

crates/ty/docs/cli.md

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/ty/docs/configuration.md

Lines changed: 61 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/ty/src/args.rs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ use clap::{ArgAction, ArgMatches, Error, Parser};
55
use ruff_db::system::SystemPathBuf;
66
use ty_project::combine::Combine;
77
use ty_project::metadata::options::{EnvironmentOptions, Options, SrcOptions, TerminalOptions};
8-
use ty_project::metadata::value::{RangedValue, RelativePathBuf, ValueSource};
8+
use ty_project::metadata::value::{
9+
RangedValue, RelativeExcludePattern, RelativePathBuf, ValueSource,
10+
};
911
use ty_python_semantic::lint;
1012

1113
#[derive(Debug, Parser)]
@@ -148,6 +150,13 @@ pub(crate) struct CheckCommand {
148150
respect_ignore_files: Option<bool>,
149151
#[clap(long, overrides_with("respect_ignore_files"), hide = true)]
150152
no_respect_ignore_files: bool,
153+
154+
/// Glob patterns for files to exclude from type checking.
155+
///
156+
/// Uses gitignore-style syntax to exclude files and directories from type checking.
157+
/// Supports patterns like `tests/`, `*.tmp`, `**/__pycache__/**`.
158+
#[arg(long, help_heading = "File selection")]
159+
exclude: Option<Vec<String>>,
151160
}
152161

153162
impl CheckCommand {
@@ -195,6 +204,12 @@ impl CheckCommand {
195204
}),
196205
src: Some(SrcOptions {
197206
respect_ignore_files,
207+
exclude: self.exclude.map(|excludes| {
208+
excludes
209+
.iter()
210+
.map(|exclude| RelativeExcludePattern::cli(exclude))
211+
.collect()
212+
}),
198213
..SrcOptions::default()
199214
}),
200215
rules,

0 commit comments

Comments
 (0)