From f58a54f043aab836706a22eb3f9b4d450477ba99 Mon Sep 17 00:00:00 2001 From: Brent Westbrook <36778786+ntBre@users.noreply.github.com> Date: Fri, 14 Feb 2025 12:48:08 -0500 Subject: [PATCH] Move `red_knot_python_semantic::PythonVersion` to the `ruff_python_ast` crate (#16147) ## Summary This PR moves the `PythonVersion` struct from the `red_knot_python_semantic` crate to the `ruff_python_ast` crate so that it can be used more easily in the syntax error detection work. Compared to that [prototype](https://github.com/astral-sh/ruff/pull/16090/) these changes reduce us from 2 `PythonVersion` structs to 1. This does not unify any of the `PythonVersion` *enums*, but I hope to make some progress on that in a follow-up. ## Test Plan Existing tests, this should not change any external behavior. --------- Co-authored-by: Alex Waygood --- Cargo.lock | 4 +- crates/red_knot/Cargo.toml | 1 + crates/red_knot/src/python_version.rs | 6 +-- crates/red_knot/tests/file_watching.rs | 3 +- crates/red_knot_project/src/combine.rs | 3 +- crates/red_knot_project/src/metadata.rs | 2 +- .../red_knot_project/src/metadata/options.rs | 5 +-- .../src/metadata/pyproject.rs | 2 +- crates/red_knot_python_semantic/src/db.rs | 2 +- crates/red_knot_python_semantic/src/lib.rs | 2 - .../src/module_resolver/path.rs | 2 +- .../src/module_resolver/resolver.rs | 5 ++- .../src/module_resolver/testing.rs | 2 +- .../src/module_resolver/typeshed.rs | 39 ++++++++++--------- .../red_knot_python_semantic/src/program.rs | 2 +- .../src/site_packages.rs | 3 +- crates/red_knot_python_semantic/src/types.rs | 5 ++- crates/red_knot_test/src/config.rs | 3 +- crates/red_knot_test/src/db.rs | 3 +- crates/red_knot_wasm/Cargo.toml | 2 +- crates/red_knot_wasm/src/lib.rs | 6 +-- crates/ruff_benchmark/Cargo.toml | 1 - crates/ruff_benchmark/benches/red_knot.rs | 2 +- crates/ruff_graph/src/db.rs | 4 +- crates/ruff_python_ast/src/lib.rs | 1 + .../src/python_version.rs | 8 ++-- .../red_knot_check_invalid_syntax.rs | 3 +- 27 files changed, 62 insertions(+), 59 deletions(-) rename crates/{red_knot_python_semantic => ruff_python_ast}/src/python_version.rs (96%) diff --git a/Cargo.lock b/Cargo.lock index 8225d98c32..e77c9fa458 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2414,6 +2414,7 @@ dependencies = [ "red_knot_server", "regex", "ruff_db", + "ruff_python_ast", "ruff_python_trivia", "salsa", "tempfile", @@ -2563,9 +2564,9 @@ dependencies = [ "js-sys", "log", "red_knot_project", - "red_knot_python_semantic", "ruff_db", "ruff_notebook", + "ruff_python_ast", "wasm-bindgen", "wasm-bindgen-test", ] @@ -2726,7 +2727,6 @@ dependencies = [ "mimalloc", "rayon", "red_knot_project", - "red_knot_python_semantic", "ruff_db", "ruff_linter", "ruff_python_ast", diff --git a/crates/red_knot/Cargo.toml b/crates/red_knot/Cargo.toml index 805bf19483..fc8fce040a 100644 --- a/crates/red_knot/Cargo.toml +++ b/crates/red_knot/Cargo.toml @@ -16,6 +16,7 @@ red_knot_python_semantic = { workspace = true } red_knot_project = { workspace = true, features = ["zstd"] } red_knot_server = { workspace = true } ruff_db = { workspace = true, features = ["os", "cache"] } +ruff_python_ast = { workspace = true } anyhow = { workspace = true } chrono = { workspace = true } diff --git a/crates/red_knot/src/python_version.rs b/crates/red_knot/src/python_version.rs index 8e7f0c8df3..bacf6ac290 100644 --- a/crates/red_knot/src/python_version.rs +++ b/crates/red_knot/src/python_version.rs @@ -40,7 +40,7 @@ impl std::fmt::Display for PythonVersion { } } -impl From for red_knot_python_semantic::PythonVersion { +impl From for ruff_python_ast::python_version::PythonVersion { fn from(value: PythonVersion) -> Self { match value { PythonVersion::Py37 => Self::PY37, @@ -61,8 +61,8 @@ mod tests { #[test] fn same_default_as_python_version() { assert_eq!( - red_knot_python_semantic::PythonVersion::from(PythonVersion::default()), - red_knot_python_semantic::PythonVersion::default() + ruff_python_ast::python_version::PythonVersion::from(PythonVersion::default()), + ruff_python_ast::python_version::PythonVersion::default() ); } } diff --git a/crates/red_knot/tests/file_watching.rs b/crates/red_knot/tests/file_watching.rs index ff1f21b982..3091c83dce 100644 --- a/crates/red_knot/tests/file_watching.rs +++ b/crates/red_knot/tests/file_watching.rs @@ -9,13 +9,14 @@ use red_knot_project::metadata::pyproject::{PyProject, Tool}; use red_knot_project::metadata::value::{RangedValue, RelativePathBuf}; use red_knot_project::watch::{directory_watcher, ChangeEvent, ProjectWatcher}; use red_knot_project::{Db, ProjectDatabase, ProjectMetadata}; -use red_knot_python_semantic::{resolve_module, ModuleName, PythonPlatform, PythonVersion}; +use red_knot_python_semantic::{resolve_module, ModuleName, PythonPlatform}; use ruff_db::files::{system_path_to_file, File, FileError}; use ruff_db::source::source_text; use ruff_db::system::{ OsSystem, System, SystemPath, SystemPathBuf, UserConfigDirectoryOverrideGuard, }; use ruff_db::Upcast; +use ruff_python_ast::python_version::PythonVersion; struct TestCase { db: ProjectDatabase, diff --git a/crates/red_knot_project/src/combine.rs b/crates/red_knot_project/src/combine.rs index 5baf0e4c4c..4dacbbb220 100644 --- a/crates/red_knot_project/src/combine.rs +++ b/crates/red_knot_project/src/combine.rs @@ -1,7 +1,8 @@ use std::{collections::HashMap, hash::BuildHasher}; -use red_knot_python_semantic::{PythonPlatform, PythonVersion, SitePackages}; +use red_knot_python_semantic::{PythonPlatform, SitePackages}; use ruff_db::system::SystemPathBuf; +use ruff_python_ast::python_version::PythonVersion; /// Combine two values, preferring the values in `self`. /// diff --git a/crates/red_knot_project/src/metadata.rs b/crates/red_knot_project/src/metadata.rs index 9002ed7fcf..4d3b54cdd6 100644 --- a/crates/red_knot_project/src/metadata.rs +++ b/crates/red_knot_project/src/metadata.rs @@ -309,8 +309,8 @@ mod tests { use anyhow::{anyhow, Context}; use insta::assert_ron_snapshot; - use red_knot_python_semantic::PythonVersion; use ruff_db::system::{SystemPathBuf, TestSystem}; + use ruff_python_ast::python_version::PythonVersion; use crate::{ProjectDiscoveryError, ProjectMetadata}; diff --git a/crates/red_knot_project/src/metadata/options.rs b/crates/red_knot_project/src/metadata/options.rs index 09a167ca98..222b95c336 100644 --- a/crates/red_knot_project/src/metadata/options.rs +++ b/crates/red_knot_project/src/metadata/options.rs @@ -1,13 +1,12 @@ use crate::metadata::value::{RangedValue, RelativePathBuf, ValueSource, ValueSourceGuard}; use crate::Db; use red_knot_python_semantic::lint::{GetLintError, Level, LintSource, RuleSelection}; -use red_knot_python_semantic::{ - ProgramSettings, PythonPlatform, PythonVersion, SearchPathSettings, SitePackages, -}; +use red_knot_python_semantic::{ProgramSettings, PythonPlatform, SearchPathSettings, SitePackages}; 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 rustc_hash::FxHashMap; use serde::{Deserialize, Serialize}; use std::borrow::Cow; diff --git a/crates/red_knot_project/src/metadata/pyproject.rs b/crates/red_knot_project/src/metadata/pyproject.rs index 58f650ee91..d5dcf5aa99 100644 --- a/crates/red_knot_project/src/metadata/pyproject.rs +++ b/crates/red_knot_project/src/metadata/pyproject.rs @@ -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 red_knot_python_semantic::PythonVersion; +use ruff_python_ast::python_version::PythonVersion; use serde::{Deserialize, Deserializer, Serialize}; use std::collections::Bound; use std::ops::Deref; diff --git a/crates/red_knot_python_semantic/src/db.rs b/crates/red_knot_python_semantic/src/db.rs index 8b94da20ff..822a8a5125 100644 --- a/crates/red_knot_python_semantic/src/db.rs +++ b/crates/red_knot_python_semantic/src/db.rs @@ -19,7 +19,6 @@ pub(crate) mod tests { use std::sync::Arc; use crate::program::{Program, SearchPathSettings}; - use crate::python_version::PythonVersion; use crate::{default_lint_registry, ProgramSettings, PythonPlatform}; use super::Db; @@ -29,6 +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; #[salsa::db] #[derive(Clone)] diff --git a/crates/red_knot_python_semantic/src/lib.rs b/crates/red_knot_python_semantic/src/lib.rs index 010de76df0..4e18bb7317 100644 --- a/crates/red_knot_python_semantic/src/lib.rs +++ b/crates/red_knot_python_semantic/src/lib.rs @@ -9,7 +9,6 @@ pub use module_name::ModuleName; pub use module_resolver::{resolve_module, system_module_search_paths, KnownModule, Module}; pub use program::{Program, ProgramSettings, SearchPathSettings, SitePackages}; pub use python_platform::PythonPlatform; -pub use python_version::PythonVersion; pub use semantic_model::{HasType, SemanticModel}; pub mod ast_node_ref; @@ -20,7 +19,6 @@ mod module_resolver; mod node_key; mod program; mod python_platform; -mod python_version; pub mod semantic_index; mod semantic_model; pub(crate) mod site_packages; diff --git a/crates/red_knot_python_semantic/src/module_resolver/path.rs b/crates/red_knot_python_semantic/src/module_resolver/path.rs index e0e6974f38..da8ce2690c 100644 --- a/crates/red_knot_python_semantic/src/module_resolver/path.rs +++ b/crates/red_knot_python_semantic/src/module_resolver/path.rs @@ -631,10 +631,10 @@ impl PartialEq for VendoredPathBuf { #[cfg(test)] mod tests { use ruff_db::Db; + use ruff_python_ast::python_version::PythonVersion; use crate::db::tests::TestDb; use crate::module_resolver::testing::{FileSpec, MockedTypeshed, TestCase, TestCaseBuilder}; - use crate::python_version::PythonVersion; use super::*; diff --git a/crates/red_knot_python_semantic/src/module_resolver/resolver.rs b/crates/red_knot_python_semantic/src/module_resolver/resolver.rs index 5fb8b686b0..720cb14d9b 100644 --- a/crates/red_knot_python_semantic/src/module_resolver/resolver.rs +++ b/crates/red_knot_python_semantic/src/module_resolver/resolver.rs @@ -6,12 +6,13 @@ 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 crate::db::Db; use crate::module_name::ModuleName; use crate::module_resolver::typeshed::{vendored_typeshed_versions, TypeshedVersions}; use crate::site_packages::VirtualEnvironment; -use crate::{Program, PythonVersion, SearchPathSettings, SitePackages}; +use crate::{Program, SearchPathSettings, SitePackages}; use super::module::{Module, ModuleKind}; use super::path::{ModulePath, SearchPath, SearchPathValidationError}; @@ -724,12 +725,12 @@ 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 crate::db::tests::TestDb; use crate::module_name::ModuleName; use crate::module_resolver::module::ModuleKind; use crate::module_resolver::testing::{FileSpec, MockedTypeshed, TestCase, TestCaseBuilder}; - use crate::PythonVersion; use crate::{ProgramSettings, PythonPlatform}; use super::*; diff --git a/crates/red_knot_python_semantic/src/module_resolver/testing.rs b/crates/red_knot_python_semantic/src/module_resolver/testing.rs index d448e670f3..c54c8f34b4 100644 --- a/crates/red_knot_python_semantic/src/module_resolver/testing.rs +++ b/crates/red_knot_python_semantic/src/module_resolver/testing.rs @@ -1,9 +1,9 @@ use ruff_db::system::{DbWithTestSystem, SystemPath, SystemPathBuf}; use ruff_db::vendored::VendoredPathBuf; +use ruff_python_ast::python_version::PythonVersion; use crate::db::tests::TestDb; use crate::program::{Program, SearchPathSettings}; -use crate::python_version::PythonVersion; use crate::{ProgramSettings, PythonPlatform, SitePackages}; /// A test case for the module resolver. diff --git a/crates/red_knot_python_semantic/src/module_resolver/typeshed.rs b/crates/red_knot_python_semantic/src/module_resolver/typeshed.rs index 731fe95f61..a52f3d1449 100644 --- a/crates/red_knot_python_semantic/src/module_resolver/typeshed.rs +++ b/crates/red_knot_python_semantic/src/module_resolver/typeshed.rs @@ -4,11 +4,12 @@ use std::num::{NonZeroU16, NonZeroUsize}; use std::ops::{RangeFrom, RangeInclusive}; use std::str::FromStr; +use ruff_python_ast::python_version::PythonVersion; use rustc_hash::FxHashMap; use crate::db::Db; use crate::module_name::ModuleName; -use crate::{Program, PythonVersion}; +use crate::Program; pub(in crate::module_resolver) fn vendored_typeshed_versions(db: &dyn Db) -> TypeshedVersions { TypeshedVersions::from_str( @@ -278,12 +279,12 @@ impl FromStr for PyVersionRange { let mut parts = s.split('-').map(str::trim); match (parts.next(), parts.next(), parts.next()) { (Some(lower), Some(""), None) => { - let lower = PythonVersion::from_versions_file_string(lower)?; + let lower = python_version_from_versions_file_string(lower)?; Ok(Self::AvailableFrom(lower..)) } (Some(lower), Some(upper), None) => { - let lower = PythonVersion::from_versions_file_string(lower)?; - let upper = PythonVersion::from_versions_file_string(upper)?; + let lower = python_version_from_versions_file_string(lower)?; + let upper = python_version_from_versions_file_string(upper)?; Ok(Self::AvailableWithin(lower..=upper)) } _ => Err(TypeshedVersionsParseErrorKind::UnexpectedNumberOfHyphens), @@ -302,21 +303,21 @@ impl fmt::Display for PyVersionRange { } } -impl PythonVersion { - fn from_versions_file_string(s: &str) -> Result { - let mut parts = s.split('.').map(str::trim); - let (Some(major), Some(minor), None) = (parts.next(), parts.next(), parts.next()) else { - return Err(TypeshedVersionsParseErrorKind::UnexpectedNumberOfPeriods( - s.to_string(), - )); - }; - PythonVersion::try_from((major, minor)).map_err(|int_parse_error| { - TypeshedVersionsParseErrorKind::IntegerParsingFailure { - version: s.to_string(), - err: int_parse_error, - } - }) - } +fn python_version_from_versions_file_string( + s: &str, +) -> Result { + let mut parts = s.split('.').map(str::trim); + let (Some(major), Some(minor), None) = (parts.next(), parts.next(), parts.next()) else { + return Err(TypeshedVersionsParseErrorKind::UnexpectedNumberOfPeriods( + s.to_string(), + )); + }; + PythonVersion::try_from((major, minor)).map_err(|int_parse_error| { + TypeshedVersionsParseErrorKind::IntegerParsingFailure { + version: s.to_string(), + err: int_parse_error, + } + }) } #[cfg(test)] diff --git a/crates/red_knot_python_semantic/src/program.rs b/crates/red_knot_python_semantic/src/program.rs index 80a36ceebb..3ae8860f59 100644 --- a/crates/red_knot_python_semantic/src/program.rs +++ b/crates/red_knot_python_semantic/src/program.rs @@ -1,10 +1,10 @@ use crate::module_resolver::SearchPaths; use crate::python_platform::PythonPlatform; -use crate::python_version::PythonVersion; use crate::Db; use anyhow::Context; use ruff_db::system::{SystemPath, SystemPathBuf}; +use ruff_python_ast::python_version::PythonVersion; use salsa::Durability; use salsa::Setter; diff --git a/crates/red_knot_python_semantic/src/site_packages.rs b/crates/red_knot_python_semantic/src/site_packages.rs index 26aba30557..265de68837 100644 --- a/crates/red_knot_python_semantic/src/site_packages.rs +++ b/crates/red_knot_python_semantic/src/site_packages.rs @@ -14,8 +14,7 @@ use std::num::NonZeroUsize; use std::ops::Deref; use ruff_db::system::{System, SystemPath, SystemPathBuf}; - -use crate::PythonVersion; +use ruff_python_ast::python_version::PythonVersion; type SitePackagesDiscoveryResult = Result; diff --git a/crates/red_knot_python_semantic/src/types.rs b/crates/red_knot_python_semantic/src/types.rs index cd6db33c44..95c6d5abd8 100644 --- a/crates/red_knot_python_semantic/src/types.rs +++ b/crates/red_knot_python_semantic/src/types.rs @@ -8,6 +8,7 @@ use itertools::Itertools; use ruff_db::diagnostic::Severity; use ruff_db::files::File; use ruff_python_ast as ast; +use ruff_python_ast::python_version::PythonVersion; use type_ordering::union_elements_ordering; pub(crate) use self::builder::{IntersectionBuilder, UnionBuilder}; @@ -43,7 +44,7 @@ use crate::types::diagnostic::INVALID_TYPE_FORM; use crate::types::infer::infer_unpack_types; use crate::types::mro::{Mro, MroError, MroIterator}; use crate::types::narrow::narrowing_constraint; -use crate::{Db, FxOrderSet, Module, Program, PythonVersion}; +use crate::{Db, FxOrderSet, Module, Program}; mod builder; mod call; @@ -4949,12 +4950,12 @@ pub(crate) mod tests { use super::*; use crate::db::tests::{setup_db, TestDbBuilder}; use crate::stdlib::typing_symbol; - use crate::PythonVersion; use ruff_db::files::system_path_to_file; 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 as ast; + use ruff_python_ast::python_version::PythonVersion; use test_case::test_case; /// Explicitly test for Python version <3.13 and >=3.13, to ensure that diff --git a/crates/red_knot_test/src/config.rs b/crates/red_knot_test/src/config.rs index a94ccd6e30..a2e6816f30 100644 --- a/crates/red_knot_test/src/config.rs +++ b/crates/red_knot_test/src/config.rs @@ -9,7 +9,8 @@ //! ``` use anyhow::Context; -use red_knot_python_semantic::{PythonPlatform, PythonVersion}; +use red_knot_python_semantic::PythonPlatform; +use ruff_python_ast::python_version::PythonVersion; use serde::Deserialize; #[derive(Deserialize, Debug, Default, Clone)] diff --git a/crates/red_knot_test/src/db.rs b/crates/red_knot_test/src/db.rs index 12b34df4fa..7736599e91 100644 --- a/crates/red_knot_test/src/db.rs +++ b/crates/red_knot_test/src/db.rs @@ -3,12 +3,13 @@ use std::sync::Arc; use red_knot_python_semantic::lint::{LintRegistry, RuleSelection}; use red_knot_python_semantic::{ default_lint_registry, Db as SemanticDb, Program, ProgramSettings, PythonPlatform, - PythonVersion, SearchPathSettings, + SearchPathSettings, }; 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; #[salsa::db] #[derive(Clone)] diff --git a/crates/red_knot_wasm/Cargo.toml b/crates/red_knot_wasm/Cargo.toml index 7cf0838834..413dc2021b 100644 --- a/crates/red_knot_wasm/Cargo.toml +++ b/crates/red_knot_wasm/Cargo.toml @@ -19,10 +19,10 @@ doctest = false default = ["console_error_panic_hook"] [dependencies] -red_knot_python_semantic = { workspace = true } red_knot_project = { workspace = true, default-features = false, features = ["deflate"] } ruff_db = { workspace = true, default-features = false, features = [] } +ruff_python_ast = { workspace = true } ruff_notebook = { workspace = true } console_error_panic_hook = { workspace = true, optional = true } diff --git a/crates/red_knot_wasm/src/lib.rs b/crates/red_knot_wasm/src/lib.rs index 5854f9eba6..440bffd333 100644 --- a/crates/red_knot_wasm/src/lib.rs +++ b/crates/red_knot_wasm/src/lib.rs @@ -198,7 +198,7 @@ pub enum PythonVersion { Py313, } -impl From for red_knot_python_semantic::PythonVersion { +impl From for ruff_python_ast::python_version::PythonVersion { fn from(value: PythonVersion) -> Self { match value { PythonVersion::Py37 => Self::PY37, @@ -308,8 +308,8 @@ mod tests { #[test] fn same_default_as_python_version() { assert_eq!( - red_knot_python_semantic::PythonVersion::from(PythonVersion::default()), - red_knot_python_semantic::PythonVersion::default() + ruff_python_ast::python_version::PythonVersion::from(PythonVersion::default()), + ruff_python_ast::python_version::PythonVersion::default() ); } } diff --git a/crates/ruff_benchmark/Cargo.toml b/crates/ruff_benchmark/Cargo.toml index 2c56cd3194..cea63ae29f 100644 --- a/crates/ruff_benchmark/Cargo.toml +++ b/crates/ruff_benchmark/Cargo.toml @@ -49,7 +49,6 @@ ruff_python_ast = { workspace = true } ruff_python_formatter = { workspace = true } ruff_python_parser = { workspace = true } ruff_python_trivia = { workspace = true } -red_knot_python_semantic = { workspace = true } red_knot_project = { workspace = true } [lints] diff --git a/crates/ruff_benchmark/benches/red_knot.rs b/crates/ruff_benchmark/benches/red_knot.rs index 87366b04de..fb9bcade73 100644 --- a/crates/ruff_benchmark/benches/red_knot.rs +++ b/crates/ruff_benchmark/benches/red_knot.rs @@ -8,13 +8,13 @@ use red_knot_project::metadata::options::{EnvironmentOptions, Options}; use red_knot_project::metadata::value::RangedValue; use red_knot_project::watch::{ChangeEvent, ChangedKind}; use red_knot_project::{Db, ProjectDatabase, ProjectMetadata}; -use red_knot_python_semantic::PythonVersion; use ruff_benchmark::criterion::{criterion_group, criterion_main, BatchSize, Criterion}; use ruff_benchmark::TestFile; 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 rustc_hash::FxHashSet; struct Case { diff --git a/crates/ruff_graph/src/db.rs b/crates/ruff_graph/src/db.rs index ac51abc8b2..16d6d3675a 100644 --- a/crates/ruff_graph/src/db.rs +++ b/crates/ruff_graph/src/db.rs @@ -4,13 +4,13 @@ use zip::CompressionMethod; use red_knot_python_semantic::lint::{LintRegistry, RuleSelection}; use red_knot_python_semantic::{ - default_lint_registry, Db, Program, ProgramSettings, PythonPlatform, PythonVersion, - SearchPathSettings, + default_lint_registry, Db, Program, ProgramSettings, PythonPlatform, SearchPathSettings, }; 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; static EMPTY_VENDORED: std::sync::LazyLock = std::sync::LazyLock::new(|| { let mut builder = VendoredFileSystemBuilder::new(CompressionMethod::Stored); diff --git a/crates/ruff_python_ast/src/lib.rs b/crates/ruff_python_ast/src/lib.rs index f76087fca0..fa3e778fdb 100644 --- a/crates/ruff_python_ast/src/lib.rs +++ b/crates/ruff_python_ast/src/lib.rs @@ -19,6 +19,7 @@ mod node; mod nodes; pub mod operator_precedence; pub mod parenthesize; +pub mod python_version; pub mod relocate; pub mod script; pub mod statement_visitor; diff --git a/crates/red_knot_python_semantic/src/python_version.rs b/crates/ruff_python_ast/src/python_version.rs similarity index 96% rename from crates/red_knot_python_semantic/src/python_version.rs rename to crates/ruff_python_ast/src/python_version.rs index a161dd1e85..66745f8a48 100644 --- a/crates/red_knot_python_semantic/src/python_version.rs +++ b/crates/ruff_python_ast/src/python_version.rs @@ -2,8 +2,7 @@ use std::fmt; /// Representation of a Python version. /// -/// Unlike the `TargetVersion` enums in the CLI crates, -/// this does not necessarily represent a Python version that we actually support. +/// N.B. This does not necessarily represent a Python version that we actually support. #[derive(Debug, Clone, Copy, Eq, PartialEq, Ord, PartialOrd, Hash)] pub struct PythonVersion { pub major: u8, @@ -41,8 +40,7 @@ impl PythonVersion { PythonVersion::PY312, PythonVersion::PY313, ] - .iter() - .copied() + .into_iter() } pub fn free_threaded_build_available(self) -> bool { @@ -84,7 +82,7 @@ impl fmt::Display for PythonVersion { #[cfg(feature = "serde")] mod serde { - use crate::PythonVersion; + use super::PythonVersion; impl<'de> serde::Deserialize<'de> for PythonVersion { fn deserialize(deserializer: D) -> Result diff --git a/fuzz/fuzz_targets/red_knot_check_invalid_syntax.rs b/fuzz/fuzz_targets/red_knot_check_invalid_syntax.rs index 758746a5e0..55a4d761b6 100644 --- a/fuzz/fuzz_targets/red_knot_check_invalid_syntax.rs +++ b/fuzz/fuzz_targets/red_knot_check_invalid_syntax.rs @@ -11,12 +11,13 @@ use red_knot_python_semantic::lint::LintRegistry; use red_knot_python_semantic::types::check_types; use red_knot_python_semantic::{ default_lint_registry, lint::RuleSelection, Db as SemanticDb, Program, ProgramSettings, - PythonPlatform, PythonVersion, SearchPathSettings, + PythonPlatform, SearchPathSettings, }; use ruff_db::files::{system_path_to_file, File, Files}; 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_parser::{parse_unchecked, Mode}; /// Database that can be used for testing.