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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use ast::PythonVersion internally in the formatter and linter #16170

Merged
merged 36 commits into from
Feb 18, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
48e7160
add serde adapter module, use PythonVersion in formatter
ntBre Feb 14, 2025
ad7f688
convert to old PythonVersion for formatter snapshots
ntBre Feb 14, 2025
55d4b5e
accept ruff settings snapshot (bad idea?)
ntBre Feb 14, 2025
84f518a
tidy formatter PythonVersion
ntBre Feb 14, 2025
f40253c
use (de)serialize_with in linter too
ntBre Feb 14, 2025
50fd040
delete separate enum code
ntBre Feb 14, 2025
a38c577
use Display instead of Debug for showing settings
ntBre Feb 17, 2025
195dd99
only use AstPythonVersion internally, move pep440 to external side
ntBre Feb 17, 2025
e218330
move adapter functions to `serde` module and rename
ntBre Feb 17, 2025
7ef41dc
update schema
ntBre Feb 17, 2025
67b2ffc
gate CacheKey behind existing `cache` feature
ntBre Feb 17, 2025
da98908
move serde helpers to formatter, where they are used
ntBre Feb 17, 2025
e12fe0a
remove serde adapters
ntBre Feb 17, 2025
8349aab
print the filename for failed format tests
ntBre Feb 17, 2025
a0c5cd6
update formatter json files for new serialization format
ntBre Feb 17, 2025
ee268a4
use PythonVersion Display impl instead of converting, update snaps
ntBre Feb 17, 2025
af041b6
remove unused TryFrom
ntBre Feb 17, 2025
4ede555
remove `latest` comment, the method no longer exists
ntBre Feb 17, 2025
33caff6
simplify From now that as_tuple is back
ntBre Feb 17, 2025
e9be081
remove unused Default impl for formatter::PythonVersion
ntBre Feb 17, 2025
02297d4
update comment
ntBre Feb 17, 2025
55e76fa
restore try_from and use it for default
ntBre Feb 18, 2025
0de7783
test that PythonVersion::default doesn't panic
ntBre Feb 18, 2025
d64deb0
pub use python_version and use ast::PythonVersion
ntBre Feb 18, 2025
020ffdf
serde mod doesn't need pub now
ntBre Feb 18, 2025
5f7ee7a
remove PartialOrd and Ord
ntBre Feb 18, 2025
bad3bb7
remove CacheKey
ntBre Feb 18, 2025
3231dde
move `get_minimum_supported_version` to where it's used
ntBre Feb 18, 2025
0abcc2c
update import script
ntBre Feb 18, 2025
6dc5bf9
delete formatter PythonVersion
ntBre Feb 18, 2025
3c90da8
fix imports
ntBre Feb 18, 2025
b75d033
use PythonVersion from ruff_python_ast, not ast::python_version
ntBre Feb 18, 2025
5acc2ae
Merge branch 'main' into brent/formatter-version
ntBre Feb 18, 2025
8d7cc7a
tidy up after merge
ntBre Feb 18, 2025
f8f162f
fix panic message on invalid JSON file
ntBre Feb 18, 2025
4e98d1a
document unwrap safety
ntBre Feb 18, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions crates/red_knot/src/python_version.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ impl std::fmt::Display for PythonVersion {
}
}

impl From<PythonVersion> for ruff_python_ast::python_version::PythonVersion {
impl From<PythonVersion> for ruff_python_ast::PythonVersion {
fn from(value: PythonVersion) -> Self {
match value {
PythonVersion::Py37 => Self::PY37,
Expand All @@ -61,8 +61,8 @@ mod tests {
#[test]
fn same_default_as_python_version() {
assert_eq!(
ruff_python_ast::python_version::PythonVersion::from(PythonVersion::default()),
ruff_python_ast::python_version::PythonVersion::default()
ruff_python_ast::PythonVersion::from(PythonVersion::default()),
ruff_python_ast::PythonVersion::default()
);
}
}
2 changes: 1 addition & 1 deletion crates/red_knot/tests/file_watching.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use ruff_db::system::{
OsSystem, System, SystemPath, SystemPathBuf, UserConfigDirectoryOverrideGuard,
};
use ruff_db::Upcast;
use ruff_python_ast::python_version::PythonVersion;
use ruff_python_ast::PythonVersion;

struct TestCase {
db: ProjectDatabase,
Expand Down
2 changes: 1 addition & 1 deletion crates/red_knot_project/src/combine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::{collections::HashMap, hash::BuildHasher};

use red_knot_python_semantic::{PythonPlatform, SitePackages};
use ruff_db::system::SystemPathBuf;
use ruff_python_ast::python_version::PythonVersion;
use ruff_python_ast::PythonVersion;

/// Combine two values, preferring the values in `self`.
///
Expand Down
2 changes: 1 addition & 1 deletion crates/red_knot_project/src/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ mod tests {
use anyhow::{anyhow, Context};
use insta::assert_ron_snapshot;
use ruff_db::system::{SystemPathBuf, TestSystem};
use ruff_python_ast::python_version::PythonVersion;
use ruff_python_ast::PythonVersion;

use crate::{ProjectDiscoveryError, ProjectMetadata};

Expand Down
2 changes: 1 addition & 1 deletion crates/red_knot_project/src/metadata/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use ruff_db::diagnostic::{Diagnostic, DiagnosticId, Severity, Span};
use ruff_db::files::system_path_to_file;
use ruff_db::system::{System, SystemPath};
use ruff_macros::Combine;
use ruff_python_ast::python_version::PythonVersion;
use ruff_python_ast::PythonVersion;
use rustc_hash::FxHashMap;
use serde::{Deserialize, Serialize};
use std::borrow::Cow;
Expand Down
2 changes: 1 addition & 1 deletion crates/red_knot_project/src/metadata/pyproject.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::metadata::options::Options;
use crate::metadata::value::{RangedValue, ValueSource, ValueSourceGuard};
use pep440_rs::{release_specifiers_to_ranges, Version, VersionSpecifiers};
use ruff_python_ast::python_version::PythonVersion;
use ruff_python_ast::PythonVersion;
use serde::{Deserialize, Deserializer, Serialize};
use std::collections::Bound;
use std::ops::Deref;
Expand Down
2 changes: 1 addition & 1 deletion crates/red_knot_python_semantic/src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ pub(crate) mod tests {
use ruff_db::system::{DbWithTestSystem, System, SystemPathBuf, TestSystem};
use ruff_db::vendored::VendoredFileSystem;
use ruff_db::{Db as SourceDb, Upcast};
use ruff_python_ast::python_version::PythonVersion;
use ruff_python_ast::PythonVersion;

#[salsa::db]
#[derive(Clone)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -631,7 +631,7 @@ impl PartialEq<SearchPath> for VendoredPathBuf {
#[cfg(test)]
mod tests {
use ruff_db::Db;
use ruff_python_ast::python_version::PythonVersion;
use ruff_python_ast::PythonVersion;

use crate::db::tests::TestDb;
use crate::module_resolver::testing::{FileSpec, MockedTypeshed, TestCase, TestCaseBuilder};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use rustc_hash::{FxBuildHasher, FxHashSet};
use ruff_db::files::{File, FilePath, FileRootKind};
use ruff_db::system::{DirectoryEntry, System, SystemPath, SystemPathBuf};
use ruff_db::vendored::{VendoredFileSystem, VendoredPath};
use ruff_python_ast::python_version::PythonVersion;
use ruff_python_ast::PythonVersion;

use crate::db::Db;
use crate::module_name::ModuleName;
Expand Down Expand Up @@ -725,7 +725,7 @@ mod tests {
assert_const_function_query_was_not_run, assert_function_query_was_not_run,
};
use ruff_db::Db;
use ruff_python_ast::python_version::PythonVersion;
use ruff_python_ast::PythonVersion;

use crate::db::tests::TestDb;
use crate::module_name::ModuleName;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use ruff_db::system::{DbWithTestSystem, SystemPath, SystemPathBuf};
use ruff_db::vendored::VendoredPathBuf;
use ruff_python_ast::python_version::PythonVersion;
use ruff_python_ast::PythonVersion;

use crate::db::tests::TestDb;
use crate::program::{Program, SearchPathSettings};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::num::{NonZeroU16, NonZeroUsize};
use std::ops::{RangeFrom, RangeInclusive};
use std::str::FromStr;

use ruff_python_ast::python_version::PythonVersion;
use ruff_python_ast::PythonVersion;
use rustc_hash::FxHashMap;

use crate::db::Db;
Expand Down
2 changes: 1 addition & 1 deletion crates/red_knot_python_semantic/src/program.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::Db;

use anyhow::Context;
use ruff_db::system::{SystemPath, SystemPathBuf};
use ruff_python_ast::python_version::PythonVersion;
use ruff_python_ast::PythonVersion;
use salsa::Durability;
use salsa::Setter;

Expand Down
2 changes: 1 addition & 1 deletion crates/red_knot_python_semantic/src/site_packages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use std::num::NonZeroUsize;
use std::ops::Deref;

use ruff_db::system::{System, SystemPath, SystemPathBuf};
use ruff_python_ast::python_version::PythonVersion;
use ruff_python_ast::PythonVersion;

type SitePackagesDiscoveryResult<T> = Result<T, SitePackagesDiscoveryError>;

Expand Down
4 changes: 2 additions & 2 deletions crates/red_knot_python_semantic/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use indexmap::IndexSet;
use itertools::Itertools;
use ruff_db::files::File;
use ruff_python_ast as ast;
use ruff_python_ast::python_version::PythonVersion;
use ruff_python_ast::PythonVersion;
use type_ordering::union_elements_ordering;

pub(crate) use self::builder::{IntersectionBuilder, UnionBuilder};
Expand Down Expand Up @@ -4535,7 +4535,7 @@ pub(crate) mod tests {
use ruff_db::parsed::parsed_module;
use ruff_db::system::DbWithTestSystem;
use ruff_db::testing::assert_function_query_was_not_run;
use ruff_python_ast::python_version::PythonVersion;
use ruff_python_ast::PythonVersion;
use test_case::test_case;

/// Explicitly test for Python version <3.13 and >=3.13, to ensure that
Expand Down
2 changes: 1 addition & 1 deletion crates/red_knot_test/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

use anyhow::Context;
use red_knot_python_semantic::PythonPlatform;
use ruff_python_ast::python_version::PythonVersion;
use ruff_python_ast::PythonVersion;
use serde::Deserialize;

#[derive(Deserialize, Debug, Default, Clone)]
Expand Down
2 changes: 1 addition & 1 deletion crates/red_knot_test/src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use ruff_db::files::{File, Files};
use ruff_db::system::{DbWithTestSystem, System, SystemPath, SystemPathBuf, TestSystem};
use ruff_db::vendored::VendoredFileSystem;
use ruff_db::{Db as SourceDb, Upcast};
use ruff_python_ast::python_version::PythonVersion;
use ruff_python_ast::PythonVersion;

#[salsa::db]
#[derive(Clone)]
Expand Down
6 changes: 3 additions & 3 deletions crates/red_knot_wasm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ pub enum PythonVersion {
Py313,
}

impl From<PythonVersion> for ruff_python_ast::python_version::PythonVersion {
impl From<PythonVersion> for ruff_python_ast::PythonVersion {
fn from(value: PythonVersion) -> Self {
match value {
PythonVersion::Py37 => Self::PY37,
Expand Down Expand Up @@ -308,8 +308,8 @@ mod tests {
#[test]
fn same_default_as_python_version() {
assert_eq!(
ruff_python_ast::python_version::PythonVersion::from(PythonVersion::default()),
ruff_python_ast::python_version::PythonVersion::default()
ruff_python_ast::PythonVersion::from(PythonVersion::default()),
ruff_python_ast::PythonVersion::default()
);
}
}
9 changes: 5 additions & 4 deletions crates/ruff/src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ use ruff_linter::settings::types::{
PythonVersion, UnsafeFixes,
};
use ruff_linter::{RuleParser, RuleSelector, RuleSelectorParser};
use ruff_python_ast as ast;
use ruff_source_file::{LineIndex, OneIndexed};
use ruff_text_size::TextRange;
use ruff_workspace::configuration::{Configuration, RuleSelection};
Expand Down Expand Up @@ -728,7 +729,7 @@ impl CheckCommand {
preview: resolve_bool_arg(self.preview, self.no_preview).map(PreviewMode::from),
respect_gitignore: resolve_bool_arg(self.respect_gitignore, self.no_respect_gitignore),
select: self.select,
target_version: self.target_version,
target_version: self.target_version.map(ast::PythonVersion::from),
unfixable: self.unfixable,
// TODO(charlie): Included in `pyproject.toml`, but not inherited.
cache_dir: self.cache_dir,
Expand Down Expand Up @@ -770,7 +771,7 @@ impl FormatCommand {
exclude: self.exclude,
preview: resolve_bool_arg(self.preview, self.no_preview).map(PreviewMode::from),
force_exclude: resolve_bool_arg(self.force_exclude, self.no_force_exclude),
target_version: self.target_version,
target_version: self.target_version.map(ast::PythonVersion::from),
cache_dir: self.cache_dir,
extension: self.extension,
..ExplicitConfigOverrides::default()
Expand Down Expand Up @@ -800,7 +801,7 @@ impl AnalyzeGraphCommand {
None
},
preview: resolve_bool_arg(self.preview, self.no_preview).map(PreviewMode::from),
target_version: self.target_version,
target_version: self.target_version.map(ast::PythonVersion::from),
..ExplicitConfigOverrides::default()
};

Expand Down Expand Up @@ -1264,7 +1265,7 @@ struct ExplicitConfigOverrides {
preview: Option<PreviewMode>,
respect_gitignore: Option<bool>,
select: Option<Vec<RuleSelector>>,
target_version: Option<PythonVersion>,
target_version: Option<ast::PythonVersion>,
unfixable: Option<Vec<RuleSelector>>,
// TODO(charlie): Captured in pyproject.toml as a default, but not part of `Settings`.
cache_dir: Option<PathBuf>,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ linter.rules.should_fix = [
linter.per_file_ignores = {}
linter.safety_table.forced_safe = []
linter.safety_table.forced_unsafe = []
linter.target_version = Py37
linter.target_version = 3.7
linter.preview = disabled
linter.explicit_preview_rules = false
linter.extension = ExtensionMapping({})
Expand Down Expand Up @@ -373,7 +373,7 @@ linter.ruff.allowed_markup_calls = []

# Formatter Settings
formatter.exclude = []
formatter.target_version = Py37
formatter.target_version = 3.7
formatter.preview = disabled
formatter.line_width = 100
formatter.line_ending = auto
Expand All @@ -387,7 +387,7 @@ formatter.docstring_code_line_width = dynamic
# Analyze Settings
analyze.exclude = []
analyze.preview = disabled
analyze.target_version = Py37
analyze.target_version = 3.7
analyze.detect_string_imports = false
analyze.extension = ExtensionMapping({})
analyze.include_dependencies = {}
Expand Down
2 changes: 1 addition & 1 deletion crates/ruff_benchmark/benches/red_knot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use ruff_db::diagnostic::{Diagnostic, DiagnosticId, Severity};
use ruff_db::files::{system_path_to_file, File};
use ruff_db::source::source_text;
use ruff_db::system::{MemoryFileSystem, SystemPath, SystemPathBuf, TestSystem};
use ruff_python_ast::python_version::PythonVersion;
use ruff_python_ast::PythonVersion;
use rustc_hash::FxHashSet;

struct Case {
Expand Down
2 changes: 1 addition & 1 deletion crates/ruff_graph/src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use ruff_db::files::{File, Files};
use ruff_db::system::{OsSystem, System, SystemPathBuf};
use ruff_db::vendored::{VendoredFileSystem, VendoredFileSystemBuilder};
use ruff_db::{Db as SourceDb, Upcast};
use ruff_python_ast::python_version::PythonVersion;
use ruff_python_ast::PythonVersion;

static EMPTY_VENDORED: std::sync::LazyLock<VendoredFileSystem> = std::sync::LazyLock::new(|| {
let mut builder = VendoredFileSystemBuilder::new(CompressionMethod::Stored);
Expand Down
5 changes: 3 additions & 2 deletions crates/ruff_graph/src/settings.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use ruff_linter::display_settings;
use ruff_linter::settings::types::{ExtensionMapping, FilePatternSet, PreviewMode, PythonVersion};
use ruff_linter::settings::types::{ExtensionMapping, FilePatternSet, PreviewMode};
use ruff_macros::CacheKey;
use ruff_python_ast::PythonVersion;
use std::collections::BTreeMap;
use std::fmt;
use std::path::PathBuf;
Expand All @@ -24,7 +25,7 @@ impl fmt::Display for AnalyzeSettings {
fields = [
self.exclude,
self.preview,
self.target_version | debug,
self.target_version,
self.detect_string_imports,
self.extension | debug,
self.include_dependencies | debug,
Expand Down
Loading
Loading