Skip to content

Commit

Permalink
Remove red_knot_python_semantic::python_version::TargetVersion (#12790
Browse files Browse the repository at this point in the history
)
  • Loading branch information
AlexWaygood authored Aug 10, 2024
1 parent 597c5f9 commit cf1a57d
Show file tree
Hide file tree
Showing 17 changed files with 141 additions and 210 deletions.
32 changes: 23 additions & 9 deletions crates/red_knot/src/target_version.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,36 @@ pub enum TargetVersion {
Py313,
}

impl TargetVersion {
const fn as_str(self) -> &'static str {
match self {
Self::Py37 => "py37",
Self::Py38 => "py38",
Self::Py39 => "py39",
Self::Py310 => "py310",
Self::Py311 => "py311",
Self::Py312 => "py312",
Self::Py313 => "py313",
}
}
}

impl std::fmt::Display for TargetVersion {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
red_knot_python_semantic::TargetVersion::from(*self).fmt(f)
f.write_str(self.as_str())
}
}

impl From<TargetVersion> for red_knot_python_semantic::TargetVersion {
impl From<TargetVersion> for red_knot_python_semantic::PythonVersion {
fn from(value: TargetVersion) -> Self {
match value {
TargetVersion::Py37 => Self::Py37,
TargetVersion::Py38 => Self::Py38,
TargetVersion::Py39 => Self::Py39,
TargetVersion::Py310 => Self::Py310,
TargetVersion::Py311 => Self::Py311,
TargetVersion::Py312 => Self::Py312,
TargetVersion::Py313 => Self::Py313,
TargetVersion::Py37 => Self::PY37,
TargetVersion::Py38 => Self::PY38,
TargetVersion::Py39 => Self::PY39,
TargetVersion::Py310 => Self::PY310,
TargetVersion::Py311 => Self::PY311,
TargetVersion::Py312 => Self::PY312,
TargetVersion::Py313 => Self::PY313,
}
}
}
4 changes: 2 additions & 2 deletions crates/red_knot/tests/file_watching.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use anyhow::{anyhow, Context};
use salsa::Setter;

use red_knot_python_semantic::{
resolve_module, ModuleName, Program, ProgramSettings, SearchPathSettings, TargetVersion,
resolve_module, ModuleName, Program, ProgramSettings, PythonVersion, SearchPathSettings,
};
use red_knot_workspace::db::RootDatabase;
use red_knot_workspace::watch;
Expand Down Expand Up @@ -234,7 +234,7 @@ where
}

let settings = ProgramSettings {
target_version: TargetVersion::default(),
target_version: PythonVersion::default(),
search_paths,
};

Expand Down
2 changes: 1 addition & 1 deletion crates/red_knot_python_semantic/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ pub use db::Db;
pub use module_name::ModuleName;
pub use module_resolver::{resolve_module, system_module_search_paths, vendored_typeshed_stubs};
pub use program::{Program, ProgramSettings, SearchPathSettings};
pub use python_version::{PythonVersion, TargetVersion, UnsupportedPythonVersion};
pub use python_version::PythonVersion;
pub use semantic_model::{HasTy, SemanticModel};

pub mod ast_node_ref;
Expand Down
26 changes: 13 additions & 13 deletions crates/red_knot_python_semantic/src/module_resolver/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -626,7 +626,7 @@ mod tests {

use super::*;
use crate::module_resolver::testing::{FileSpec, MockedTypeshed, TestCase, TestCaseBuilder};
use crate::TargetVersion;
use crate::python_version::PythonVersion;

impl ModulePath {
#[must_use]
Expand Down Expand Up @@ -866,7 +866,7 @@ mod tests {

fn typeshed_test_case(
typeshed: MockedTypeshed,
target_version: TargetVersion,
target_version: PythonVersion,
) -> (TestDb, SearchPath) {
let TestCase { db, stdlib, .. } = TestCaseBuilder::new()
.with_custom_typeshed(typeshed)
Expand All @@ -878,11 +878,11 @@ mod tests {
}

fn py38_typeshed_test_case(typeshed: MockedTypeshed) -> (TestDb, SearchPath) {
typeshed_test_case(typeshed, TargetVersion::Py38)
typeshed_test_case(typeshed, PythonVersion::PY38)
}

fn py39_typeshed_test_case(typeshed: MockedTypeshed) -> (TestDb, SearchPath) {
typeshed_test_case(typeshed, TargetVersion::Py39)
typeshed_test_case(typeshed, PythonVersion::PY39)
}

#[test]
Expand All @@ -898,7 +898,7 @@ mod tests {
};

let (db, stdlib_path) = py38_typeshed_test_case(TYPESHED);
let resolver = ResolverState::new(&db, TargetVersion::Py38);
let resolver = ResolverState::new(&db, PythonVersion::PY38);

let asyncio_regular_package = stdlib_path.join("asyncio");
assert!(asyncio_regular_package.is_directory(&resolver));
Expand Down Expand Up @@ -926,7 +926,7 @@ mod tests {
};

let (db, stdlib_path) = py38_typeshed_test_case(TYPESHED);
let resolver = ResolverState::new(&db, TargetVersion::Py38);
let resolver = ResolverState::new(&db, PythonVersion::PY38);

let xml_namespace_package = stdlib_path.join("xml");
assert!(xml_namespace_package.is_directory(&resolver));
Expand All @@ -948,7 +948,7 @@ mod tests {
};

let (db, stdlib_path) = py38_typeshed_test_case(TYPESHED);
let resolver = ResolverState::new(&db, TargetVersion::Py38);
let resolver = ResolverState::new(&db, PythonVersion::PY38);

let functools_module = stdlib_path.join("functools.pyi");
assert!(functools_module.to_file(&resolver).is_some());
Expand All @@ -964,7 +964,7 @@ mod tests {
};

let (db, stdlib_path) = py38_typeshed_test_case(TYPESHED);
let resolver = ResolverState::new(&db, TargetVersion::Py38);
let resolver = ResolverState::new(&db, PythonVersion::PY38);

let collections_regular_package = stdlib_path.join("collections");
assert_eq!(collections_regular_package.to_file(&resolver), None);
Expand All @@ -980,7 +980,7 @@ mod tests {
};

let (db, stdlib_path) = py38_typeshed_test_case(TYPESHED);
let resolver = ResolverState::new(&db, TargetVersion::Py38);
let resolver = ResolverState::new(&db, PythonVersion::PY38);

let importlib_namespace_package = stdlib_path.join("importlib");
assert_eq!(importlib_namespace_package.to_file(&resolver), None);
Expand All @@ -1001,7 +1001,7 @@ mod tests {
};

let (db, stdlib_path) = py38_typeshed_test_case(TYPESHED);
let resolver = ResolverState::new(&db, TargetVersion::Py38);
let resolver = ResolverState::new(&db, PythonVersion::PY38);

let non_existent = stdlib_path.join("doesnt_even_exist");
assert_eq!(non_existent.to_file(&resolver), None);
Expand Down Expand Up @@ -1029,7 +1029,7 @@ mod tests {
};

let (db, stdlib_path) = py39_typeshed_test_case(TYPESHED);
let resolver = ResolverState::new(&db, TargetVersion::Py39);
let resolver = ResolverState::new(&db, PythonVersion::PY39);

// Since we've set the target version to Py39,
// `collections` should now exist as a directory, according to VERSIONS...
Expand Down Expand Up @@ -1058,7 +1058,7 @@ mod tests {
};

let (db, stdlib_path) = py39_typeshed_test_case(TYPESHED);
let resolver = ResolverState::new(&db, TargetVersion::Py39);
let resolver = ResolverState::new(&db, PythonVersion::PY39);

// The `importlib` directory now also exists
let importlib_namespace_package = stdlib_path.join("importlib");
Expand All @@ -1082,7 +1082,7 @@ mod tests {
};

let (db, stdlib_path) = py39_typeshed_test_case(TYPESHED);
let resolver = ResolverState::new(&db, TargetVersion::Py39);
let resolver = ResolverState::new(&db, PythonVersion::PY39);

// The `xml` package no longer exists on py39:
let xml_namespace_package = stdlib_path.join("xml");
Expand Down
36 changes: 18 additions & 18 deletions crates/red_knot_python_semantic/src/module_resolver/resolver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use super::path::{ModulePath, SearchPath, SearchPathValidationError};
use super::state::ResolverState;
use crate::db::Db;
use crate::module_name::ModuleName;
use crate::{Program, SearchPathSettings, TargetVersion};
use crate::{Program, PythonVersion, SearchPathSettings};

/// Resolves a module name to a module.
pub fn resolve_module(db: &dyn Db, module_name: ModuleName) -> Option<Module> {
Expand Down Expand Up @@ -451,7 +451,7 @@ impl<'db> Iterator for PthFileIterator<'db> {
/// Validated and normalized module-resolution settings.
#[derive(Clone, Debug, PartialEq, Eq)]
pub(crate) struct ModuleResolutionSettings {
target_version: TargetVersion,
target_version: PythonVersion,

/// Search paths that have been statically determined purely from reading Ruff's configuration settings.
/// These shouldn't ever change unless the config settings themselves change.
Expand All @@ -467,7 +467,7 @@ pub(crate) struct ModuleResolutionSettings {
}

impl ModuleResolutionSettings {
fn target_version(&self) -> TargetVersion {
fn target_version(&self) -> PythonVersion {
self.target_version
}

Expand Down Expand Up @@ -496,7 +496,7 @@ fn resolve_name(db: &dyn Db, name: &ModuleName) -> Option<(SearchPath, File, Mod
let target_version = resolver_settings.target_version();
let resolver_state = ResolverState::new(db, target_version);
let is_builtin_module =
ruff_python_stdlib::sys::is_builtin_module(target_version.minor_version(), name.as_str());
ruff_python_stdlib::sys::is_builtin_module(target_version.minor, name.as_str());

for search_path in resolver_settings.search_paths(db) {
// When a builtin module is imported, standard module resolution is bypassed:
Expand Down Expand Up @@ -706,7 +706,7 @@ mod tests {
let TestCase { db, stdlib, .. } = TestCaseBuilder::new()
.with_src_files(SRC)
.with_custom_typeshed(TYPESHED)
.with_target_version(TargetVersion::Py38)
.with_target_version(PythonVersion::PY38)
.build();

let builtins_module_name = ModuleName::new_static("builtins").unwrap();
Expand All @@ -724,7 +724,7 @@ mod tests {

let TestCase { db, stdlib, .. } = TestCaseBuilder::new()
.with_custom_typeshed(TYPESHED)
.with_target_version(TargetVersion::Py38)
.with_target_version(PythonVersion::PY38)
.build();

let functools_module_name = ModuleName::new_static("functools").unwrap();
Expand Down Expand Up @@ -777,7 +777,7 @@ mod tests {

let TestCase { db, stdlib, .. } = TestCaseBuilder::new()
.with_custom_typeshed(TYPESHED)
.with_target_version(TargetVersion::Py38)
.with_target_version(PythonVersion::PY38)
.build();

let existing_modules = create_module_names(&["asyncio", "functools", "xml.etree"]);
Expand Down Expand Up @@ -822,7 +822,7 @@ mod tests {

let TestCase { db, .. } = TestCaseBuilder::new()
.with_custom_typeshed(TYPESHED)
.with_target_version(TargetVersion::Py38)
.with_target_version(PythonVersion::PY38)
.build();

let nonexisting_modules = create_module_names(&[
Expand Down Expand Up @@ -866,7 +866,7 @@ mod tests {

let TestCase { db, stdlib, .. } = TestCaseBuilder::new()
.with_custom_typeshed(TYPESHED)
.with_target_version(TargetVersion::Py39)
.with_target_version(PythonVersion::PY39)
.build();

let existing_modules = create_module_names(&[
Expand Down Expand Up @@ -908,7 +908,7 @@ mod tests {

let TestCase { db, .. } = TestCaseBuilder::new()
.with_custom_typeshed(TYPESHED)
.with_target_version(TargetVersion::Py39)
.with_target_version(PythonVersion::PY39)
.build();

let nonexisting_modules = create_module_names(&["importlib", "xml", "xml.etree"]);
Expand All @@ -932,7 +932,7 @@ mod tests {
let TestCase { db, src, .. } = TestCaseBuilder::new()
.with_src_files(SRC)
.with_custom_typeshed(TYPESHED)
.with_target_version(TargetVersion::Py38)
.with_target_version(PythonVersion::PY38)
.build();

let functools_module_name = ModuleName::new_static("functools").unwrap();
Expand All @@ -956,7 +956,7 @@ mod tests {
fn stdlib_uses_vendored_typeshed_when_no_custom_typeshed_supplied() {
let TestCase { db, stdlib, .. } = TestCaseBuilder::new()
.with_vendored_typeshed()
.with_target_version(TargetVersion::default())
.with_target_version(PythonVersion::default())
.build();

let pydoc_data_topics_name = ModuleName::new_static("pydoc_data.topics").unwrap();
Expand Down Expand Up @@ -1209,7 +1209,7 @@ mod tests {
site_packages: vec![site_packages],
};

Program::new(&db, TargetVersion::Py38, search_paths);
Program::new(&db, PythonVersion::PY38, search_paths);

let foo_module = resolve_module(&db, ModuleName::new_static("foo").unwrap()).unwrap();
let bar_module = resolve_module(&db, ModuleName::new_static("bar").unwrap()).unwrap();
Expand Down Expand Up @@ -1243,7 +1243,7 @@ mod tests {
fn deleting_an_unrelated_file_doesnt_change_module_resolution() {
let TestCase { mut db, src, .. } = TestCaseBuilder::new()
.with_src_files(&[("foo.py", "x = 1"), ("bar.py", "x = 2")])
.with_target_version(TargetVersion::Py38)
.with_target_version(PythonVersion::PY38)
.build();

let foo_module_name = ModuleName::new_static("foo").unwrap();
Expand Down Expand Up @@ -1331,7 +1331,7 @@ mod tests {
..
} = TestCaseBuilder::new()
.with_custom_typeshed(TYPESHED)
.with_target_version(TargetVersion::Py38)
.with_target_version(PythonVersion::PY38)
.build();

let functools_module_name = ModuleName::new_static("functools").unwrap();
Expand Down Expand Up @@ -1379,7 +1379,7 @@ mod tests {
..
} = TestCaseBuilder::new()
.with_custom_typeshed(TYPESHED)
.with_target_version(TargetVersion::Py38)
.with_target_version(PythonVersion::PY38)
.build();

let functools_module_name = ModuleName::new_static("functools").unwrap();
Expand Down Expand Up @@ -1419,7 +1419,7 @@ mod tests {
} = TestCaseBuilder::new()
.with_src_files(SRC)
.with_custom_typeshed(TYPESHED)
.with_target_version(TargetVersion::Py38)
.with_target_version(PythonVersion::PY38)
.build();

let functools_module_name = ModuleName::new_static("functools").unwrap();
Expand Down Expand Up @@ -1705,7 +1705,7 @@ not_a_directory

Program::new(
&db,
TargetVersion::default(),
PythonVersion::default(),
SearchPathSettings {
extra_paths: vec![],
src_root: SystemPathBuf::from("/src"),
Expand Down
6 changes: 3 additions & 3 deletions crates/red_knot_python_semantic/src/module_resolver/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@ use ruff_db::vendored::VendoredFileSystem;

use super::typeshed::LazyTypeshedVersions;
use crate::db::Db;
use crate::TargetVersion;
use crate::python_version::PythonVersion;

pub(crate) struct ResolverState<'db> {
pub(crate) db: &'db dyn Db,
pub(crate) typeshed_versions: LazyTypeshedVersions<'db>,
pub(crate) target_version: TargetVersion,
pub(crate) target_version: PythonVersion,
}

impl<'db> ResolverState<'db> {
pub(crate) fn new(db: &'db dyn Db, target_version: TargetVersion) -> Self {
pub(crate) fn new(db: &'db dyn Db, target_version: PythonVersion) -> Self {
Self {
db,
typeshed_versions: LazyTypeshedVersions::new(),
Expand Down
Loading

0 comments on commit cf1a57d

Please sign in to comment.