Skip to content

Commit

Permalink
vendored_typeshed_versions should use db.vendored
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaReiser committed Sep 21, 2024
1 parent 6c303b2 commit 1964a2f
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 61 deletions.
47 changes: 13 additions & 34 deletions crates/red_knot_python_semantic/src/module_resolver/resolver.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
use rustc_hash::{FxBuildHasher, FxHashSet};
use std::borrow::Cow;
use std::iter::FusedIterator;
use std::ops::Deref;

use ruff_db::files::{File, FilePath, FileRootKind};
use ruff_db::system::{DirectoryEntry, System, SystemPath, SystemPathBuf};
use ruff_db::vendored::{VendoredFileSystem, VendoredPath};
use rustc_hash::{FxBuildHasher, FxHashSet};
use std::borrow::Cow;
use std::iter::FusedIterator;

use super::module::{Module, ModuleKind};
use super::path::{ModulePath, SearchPath, SearchPathValidationError};
use crate::db::Db;
use crate::module_name::ModuleName;
use crate::module_resolver::typeshed::{vendored_typeshed_versions, TypeshedVersions};
use crate::module_resolver::typeshed::TypeshedVersions;
use crate::site_packages::VirtualEnvironment;
use crate::{Program, PythonVersion, SearchPathSettings, SitePackages};

Expand Down Expand Up @@ -136,7 +134,7 @@ pub(crate) struct SearchPaths {
/// for the first `site-packages` path
site_packages: Vec<SearchPath>,

typeshed_versions: ResolvedTypeshedVersions,
custom_typeshed_versions: Option<TypeshedVersions>,
}

impl SearchPaths {
Expand Down Expand Up @@ -179,7 +177,8 @@ impl SearchPaths {
tracing::debug!("Adding first-party search path '{src_root}'");
static_paths.push(SearchPath::first_party(system, src_root.to_path_buf())?);

let (typeshed_versions, stdlib_path) = if let Some(custom_typeshed) = custom_typeshed {
let (custom_typeshed_versions, stdlib_path) = if let Some(custom_typeshed) = custom_typeshed
{
let custom_typeshed = canonicalize(custom_typeshed, system);
tracing::debug!("Adding custom-stdlib search path '{custom_typeshed}'");

Expand All @@ -202,13 +201,10 @@ impl SearchPaths {

let search_path = SearchPath::custom_stdlib(db, &custom_typeshed)?;

(ResolvedTypeshedVersions::Custom(parsed), search_path)
(Some(parsed), search_path)
} else {
tracing::debug!("Using vendored stdlib");
(
ResolvedTypeshedVersions::Vendored(vendored_typeshed_versions()),
SearchPath::vendored_stdlib(),
)
(None, SearchPath::vendored_stdlib())
};

static_paths.push(stdlib_path);
Expand Down Expand Up @@ -252,7 +248,7 @@ impl SearchPaths {
Ok(SearchPaths {
static_paths,
site_packages,
typeshed_versions,
custom_typeshed_versions,
})
}

Expand All @@ -274,25 +270,8 @@ impl SearchPaths {
})
}

pub(super) fn typeshed_versions(&self) -> &TypeshedVersions {
&self.typeshed_versions
}
}

#[derive(Debug, PartialEq, Eq)]
enum ResolvedTypeshedVersions {
Vendored(&'static TypeshedVersions),
Custom(TypeshedVersions),
}

impl Deref for ResolvedTypeshedVersions {
type Target = TypeshedVersions;

fn deref(&self) -> &Self::Target {
match self {
ResolvedTypeshedVersions::Vendored(versions) => versions,
ResolvedTypeshedVersions::Custom(versions) => versions,
}
pub(super) fn custom_typeshed_versions(&self) -> Option<&TypeshedVersions> {
self.custom_typeshed_versions.as_ref()
}
}

Expand All @@ -311,7 +290,7 @@ pub(crate) fn dynamic_resolution_paths(db: &dyn Db) -> Vec<SearchPath> {
let SearchPaths {
static_paths,
site_packages,
typeshed_versions: _,
custom_typeshed_versions: _,
} = Program::get(db).search_paths(db);

let mut dynamic_paths = Vec::new();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
pub use self::vendored::vendored_typeshed_stubs;
pub(super) use self::versions::{
typeshed_versions, vendored_typeshed_versions, TypeshedVersions, TypeshedVersionsParseError,
typeshed_versions, TypeshedVersions, TypeshedVersionsParseError,
TypeshedVersionsQueryResult,
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,27 @@ use std::num::{NonZeroU16, NonZeroUsize};
use std::ops::{RangeFrom, RangeInclusive};
use std::str::FromStr;

use once_cell::sync::Lazy;
use rustc_hash::FxHashMap;

use super::vendored::vendored_typeshed_stubs;
use crate::db::Db;
use crate::module_name::ModuleName;
use crate::{Program, PythonVersion};

static VENDORED_VERSIONS: Lazy<TypeshedVersions> = Lazy::new(|| {
#[salsa::tracked(return_ref)]
pub(crate) fn vendored_typeshed_versions(db: &dyn Db) -> TypeshedVersions {
TypeshedVersions::from_str(
&vendored_typeshed_stubs()
&db.vendored()
.read_to_string("stdlib/VERSIONS")
.unwrap(),
.expect("The vendored typeshed stubs should contain a VERSIONS file"),
)
.unwrap()
});

pub(crate) fn vendored_typeshed_versions() -> &'static TypeshedVersions {
&VENDORED_VERSIONS
.expect("The VERSIONS file in the vendored typeshed stubs should be well-formed")
}

pub(crate) fn typeshed_versions(db: &dyn Db) -> &TypeshedVersions {
Program::get(db).search_paths(db).typeshed_versions()
Program::get(db)
.search_paths(db)
.custom_typeshed_versions()
.unwrap_or_else(|| vendored_typeshed_versions(db))
}

#[derive(Debug, PartialEq, Eq, Clone)]
Expand Down Expand Up @@ -332,6 +330,8 @@ mod tests {

use insta::assert_snapshot;

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

use super::*;

const TYPESHED_STDLIB_DIR: &str = "stdlib";
Expand All @@ -353,12 +353,9 @@ mod tests {

#[test]
fn can_parse_vendored_versions_file() {
let versions_data = include_str!(concat!(
env!("CARGO_MANIFEST_DIR"),
"/vendor/typeshed/stdlib/VERSIONS"
));
let db = TestDb::new();

let versions = TypeshedVersions::from_str(versions_data).unwrap();
let versions = vendored_typeshed_versions(&db);
assert!(versions.len() > 100);
assert!(versions.len() < 1000);

Expand Down Expand Up @@ -395,9 +392,9 @@ mod tests {

#[test]
fn typeshed_versions_consistent_with_vendored_stubs() {
const VERSIONS_DATA: &str = include_str!("../../../vendor/typeshed/stdlib/VERSIONS");
let db = TestDb::new();
let vendored_typeshed_versions = vendored_typeshed_versions(&db);
let vendored_typeshed_dir = Path::new("vendor/typeshed").canonicalize().unwrap();
let vendored_typeshed_versions = TypeshedVersions::from_str(VERSIONS_DATA).unwrap();

let mut empty_iterator = true;

Expand Down
Loading

0 comments on commit 1964a2f

Please sign in to comment.