Skip to content

Commit

Permalink
Move Program and related structs to red_knot_python_semantic (#12777)
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaReiser authored Aug 9, 2024
1 parent 64f1f34 commit 2abfab0
Show file tree
Hide file tree
Showing 23 changed files with 41 additions and 33 deletions.
3 changes: 3 additions & 0 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion crates/red_knot/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ use colored::Colorize;
use crossbeam::channel as crossbeam_channel;
use salsa::plumbing::ZalsaDatabase;

use red_knot_python_semantic::{ProgramSettings, SearchPathSettings};
use red_knot_server::run_server;
use red_knot_workspace::db::RootDatabase;
use red_knot_workspace::site_packages::site_packages_dirs_of_venv;
use red_knot_workspace::watch;
use red_knot_workspace::watch::WorkspaceWatcher;
use red_knot_workspace::workspace::WorkspaceMetadata;
use ruff_db::program::{ProgramSettings, SearchPathSettings};
use ruff_db::system::{OsSystem, System, SystemPath, SystemPathBuf};
use target_version::TargetVersion;

Expand Down
4 changes: 2 additions & 2 deletions crates/red_knot/src/target_version.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ pub enum TargetVersion {

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

impl From<TargetVersion> for ruff_db::program::TargetVersion {
impl From<TargetVersion> for red_knot_python_semantic::TargetVersion {
fn from(value: TargetVersion) -> Self {
match value {
TargetVersion::Py37 => Self::Py37,
Expand Down
5 changes: 3 additions & 2 deletions crates/red_knot/tests/file_watching.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@ use std::time::Duration;
use anyhow::{anyhow, Context};
use salsa::Setter;

use red_knot_python_semantic::{resolve_module, ModuleName};
use red_knot_python_semantic::{
resolve_module, ModuleName, Program, ProgramSettings, SearchPathSettings, TargetVersion,
};
use red_knot_workspace::db::RootDatabase;
use red_knot_workspace::watch;
use red_knot_workspace::watch::{directory_watcher, WorkspaceWatcher};
use red_knot_workspace::workspace::WorkspaceMetadata;
use ruff_db::files::{system_path_to_file, File, FileError};
use ruff_db::program::{Program, ProgramSettings, SearchPathSettings, TargetVersion};
use ruff_db::source::source_text;
use ruff_db::system::{OsSystem, SystemPath, SystemPathBuf};
use ruff_db::Upcast;
Expand Down
2 changes: 2 additions & 0 deletions crates/red_knot_python_semantic/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use rustc_hash::FxHasher;
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, TargetVersion};
pub use semantic_model::{HasTy, SemanticModel};

pub mod ast_node_ref;
Expand All @@ -13,6 +14,7 @@ mod db;
mod module_name;
mod module_resolver;
mod node_key;
mod program;
pub mod semantic_index;
mod semantic_model;
pub mod types;
Expand Down
5 changes: 2 additions & 3 deletions crates/red_knot_python_semantic/src/module_resolver/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -620,14 +620,13 @@ impl PartialEq<SearchPath> for VendoredPathBuf {

#[cfg(test)]
mod tests {
use ruff_db::program::TargetVersion;
use ruff_db::Db;

use crate::db::tests::TestDb;

use crate::module_resolver::testing::{FileSpec, MockedTypeshed, TestCase, TestCaseBuilder};

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

impl ModulePath {
#[must_use]
Expand Down
12 changes: 6 additions & 6 deletions crates/red_knot_python_semantic/src/module_resolver/resolver.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
use std::borrow::Cow;
use std::iter::FusedIterator;

use rustc_hash::{FxBuildHasher, FxHashSet};

use ruff_db::files::{File, FilePath, FileRootKind};
use ruff_db::program::{Program, SearchPathSettings, TargetVersion};
use ruff_db::system::{DirectoryEntry, System, SystemPath, SystemPathBuf};
use ruff_db::vendored::VendoredPath;
use rustc_hash::{FxBuildHasher, FxHashSet};

use crate::db::Db;
use crate::module_name::ModuleName;

use super::module::{Module, ModuleKind};
use super::path::{ModulePath, SearchPath, SearchPathValidationError};
use super::state::ResolverState;
use crate::db::Db;
use crate::module_name::ModuleName;
use crate::{Program, SearchPathSettings, TargetVersion};

/// Resolves a module name to a module.
pub fn resolve_module(db: &dyn Db, module_name: ModuleName) -> Option<Module> {
Expand Down Expand Up @@ -1143,7 +1143,7 @@ mod tests {
fn symlink() -> anyhow::Result<()> {
use anyhow::Context;

use ruff_db::program::Program;
use crate::program::Program;
use ruff_db::system::{OsSystem, SystemPath};

use crate::db::tests::TestDb;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use ruff_db::program::TargetVersion;
use ruff_db::vendored::VendoredFileSystem;

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

pub(crate) struct ResolverState<'db> {
pub(crate) db: &'db dyn Db,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use ruff_db::program::{Program, SearchPathSettings, TargetVersion};
use ruff_db::system::{DbWithTestSystem, SystemPath, SystemPathBuf};
use ruff_db::vendored::VendoredPathBuf;

use crate::db::tests::TestDb;
use crate::program::{Program, SearchPathSettings, TargetVersion};

/// A test case for the module resolver.
///
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,15 @@ use std::ops::{RangeFrom, RangeInclusive};
use std::str::FromStr;

use once_cell::sync::Lazy;
use ruff_db::program::TargetVersion;
use ruff_db::system::SystemPath;
use rustc_hash::FxHashMap;

use ruff_db::files::{system_path_to_file, File};

use super::vendored::vendored_typeshed_stubs;
use crate::db::Db;
use crate::module_name::ModuleName;

use super::vendored::vendored_typeshed_stubs;
use crate::TargetVersion;

#[derive(Debug)]
pub(crate) struct LazyTypeshedVersions<'db>(OnceCell<&'db TypeshedVersions>);
Expand Down Expand Up @@ -440,7 +439,6 @@ mod tests {
use std::path::Path;

use insta::assert_snapshot;
use ruff_db::program::TargetVersion;

use super::*;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::{system::SystemPathBuf, Db};
use crate::Db;
use ruff_db::system::SystemPathBuf;
use salsa::Durability;

#[salsa::input(singleton)]
Expand Down
2 changes: 1 addition & 1 deletion crates/red_knot_python_semantic/src/semantic_model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,10 +165,10 @@ impl HasTy for ast::Alias {
mod tests {
use ruff_db::files::system_path_to_file;
use ruff_db::parsed::parsed_module;
use ruff_db::program::{Program, SearchPathSettings, TargetVersion};
use ruff_db::system::{DbWithTestSystem, SystemPathBuf};

use crate::db::tests::TestDb;
use crate::program::{Program, SearchPathSettings, TargetVersion};
use crate::types::Type;
use crate::{HasTy, SemanticModel};

Expand Down
2 changes: 1 addition & 1 deletion crates/red_knot_python_semantic/src/types/infer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1496,13 +1496,13 @@ impl<'db> TypeInferenceBuilder<'db> {
mod tests {
use ruff_db::files::{system_path_to_file, File};
use ruff_db::parsed::parsed_module;
use ruff_db::program::{Program, SearchPathSettings, TargetVersion};
use ruff_db::system::{DbWithTestSystem, SystemPathBuf};
use ruff_db::testing::assert_function_query_was_not_run;
use ruff_python_ast::name::Name;

use crate::builtins::builtins_scope;
use crate::db::tests::TestDb;
use crate::program::{Program, SearchPathSettings, TargetVersion};
use crate::semantic_index::definition::Definition;
use crate::semantic_index::symbol::FileScopeId;
use crate::semantic_index::{global_scope, semantic_index, symbol_table, use_def_map};
Expand Down
1 change: 1 addition & 0 deletions crates/red_knot_server/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ repository = { workspace = true }
license = { workspace = true }

[dependencies]
red_knot_python_semantic = { workspace = true }
red_knot_workspace = { workspace = true }
ruff_db = { workspace = true }
ruff_linter = { workspace = true }
Expand Down
2 changes: 1 addition & 1 deletion crates/red_knot_server/src/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ use std::sync::Arc;
use anyhow::anyhow;
use lsp_types::{ClientCapabilities, Url};

use red_knot_python_semantic::{ProgramSettings, SearchPathSettings, TargetVersion};
use red_knot_workspace::db::RootDatabase;
use red_knot_workspace::workspace::WorkspaceMetadata;
use ruff_db::files::{system_path_to_file, File};
use ruff_db::program::{ProgramSettings, SearchPathSettings, TargetVersion};
use ruff_db::system::SystemPath;

use crate::edit::{DocumentKey, NotebookDocument};
Expand Down
1 change: 1 addition & 0 deletions crates/red_knot_wasm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ doctest = false
default = ["console_error_panic_hook"]

[dependencies]
red_knot_python_semantic = { workspace = true }
red_knot_workspace = { workspace = true }

ruff_db = { workspace = true }
Expand Down
4 changes: 2 additions & 2 deletions crates/red_knot_wasm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ use std::any::Any;
use js_sys::Error;
use wasm_bindgen::prelude::*;

use red_knot_python_semantic::{ProgramSettings, SearchPathSettings};
use red_knot_workspace::db::RootDatabase;
use red_knot_workspace::workspace::WorkspaceMetadata;
use ruff_db::files::{system_path_to_file, File};
use ruff_db::program::{ProgramSettings, SearchPathSettings};
use ruff_db::system::walk_directory::WalkDirectoryBuilder;
use ruff_db::system::{
DirectoryEntry, MemoryFileSystem, Metadata, System, SystemPath, SystemPathBuf,
Expand Down Expand Up @@ -184,7 +184,7 @@ pub enum TargetVersion {
Py313,
}

impl From<TargetVersion> for ruff_db::program::TargetVersion {
impl From<TargetVersion> for red_knot_python_semantic::TargetVersion {
fn from(value: TargetVersion) -> Self {
match value {
TargetVersion::Py37 => Self::Py37,
Expand Down
5 changes: 3 additions & 2 deletions crates/red_knot_workspace/src/db.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use std::panic::RefUnwindSafe;
use std::sync::Arc;

use red_knot_python_semantic::{vendored_typeshed_stubs, Db as SemanticDb};
use red_knot_python_semantic::{
vendored_typeshed_stubs, Db as SemanticDb, Program, ProgramSettings,
};
use ruff_db::files::{File, Files};
use ruff_db::program::{Program, ProgramSettings};
use ruff_db::system::System;
use ruff_db::vendored::VendoredFileSystem;
use ruff_db::{Db as SourceDb, Upcast};
Expand Down
5 changes: 3 additions & 2 deletions crates/red_knot_workspace/src/lint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -305,13 +305,14 @@ enum AnyImportRef<'a> {

#[cfg(test)]
mod tests {
use red_knot_python_semantic::{Program, SearchPathSettings, TargetVersion};
use ruff_db::files::system_path_to_file;
use ruff_db::program::{Program, SearchPathSettings, TargetVersion};
use ruff_db::system::{DbWithTestSystem, SystemPathBuf};

use super::{lint_semantic, Diagnostics};
use crate::db::tests::TestDb;

use super::{lint_semantic, Diagnostics};

fn setup_db() -> TestDb {
setup_db_with_root(SystemPathBuf::from("/src"))
}
Expand Down
2 changes: 1 addition & 1 deletion crates/red_knot_workspace/tests/check.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use red_knot_python_semantic::{ProgramSettings, SearchPathSettings, TargetVersion};
use red_knot_workspace::db::RootDatabase;
use red_knot_workspace::lint::lint_semantic;
use red_knot_workspace::workspace::WorkspaceMetadata;
use ruff_db::files::system_path_to_file;
use ruff_db::program::{ProgramSettings, SearchPathSettings, TargetVersion};
use ruff_db::system::{OsSystem, SystemPathBuf};
use std::fs;
use std::path::PathBuf;
Expand Down
1 change: 1 addition & 0 deletions crates/ruff_benchmark/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ 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_workspace = { workspace = true }

[lints]
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
@@ -1,11 +1,11 @@
#![allow(clippy::disallowed_names)]

use red_knot_python_semantic::{ProgramSettings, SearchPathSettings, TargetVersion};
use red_knot_workspace::db::RootDatabase;
use red_knot_workspace::workspace::WorkspaceMetadata;
use ruff_benchmark::criterion::{criterion_group, criterion_main, BatchSize, Criterion};
use ruff_benchmark::TestFile;
use ruff_db::files::{system_path_to_file, File};
use ruff_db::program::{ProgramSettings, SearchPathSettings, TargetVersion};
use ruff_db::source::source_text;
use ruff_db::system::{MemoryFileSystem, SystemPath, TestSystem};

Expand Down
1 change: 0 additions & 1 deletion crates/ruff_db/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ use crate::vendored::VendoredFileSystem;
pub mod file_revision;
pub mod files;
pub mod parsed;
pub mod program;
pub mod source;
pub mod system;
#[cfg(feature = "testing")]
Expand Down

0 comments on commit 2abfab0

Please sign in to comment.