Skip to content

Commit 8921fbb

Browse files
authored
vendored_typeshed_versions should use db.vendored (#13434)
1 parent 3018303 commit 8921fbb

File tree

3 files changed

+24
-49
lines changed

3 files changed

+24
-49
lines changed

Diff for: crates/red_knot_python_semantic/src/module_resolver/resolver.rs

+8-24
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,21 @@
1-
use rustc_hash::{FxBuildHasher, FxHashSet};
21
use std::borrow::Cow;
32
use std::iter::FusedIterator;
4-
use std::ops::Deref;
3+
4+
use rustc_hash::{FxBuildHasher, FxHashSet};
55

66
use ruff_db::files::{File, FilePath, FileRootKind};
77
use ruff_db::system::{DirectoryEntry, System, SystemPath, SystemPathBuf};
88
use ruff_db::vendored::{VendoredFileSystem, VendoredPath};
99

10-
use super::module::{Module, ModuleKind};
11-
use super::path::{ModulePath, SearchPath, SearchPathValidationError};
1210
use crate::db::Db;
1311
use crate::module_name::ModuleName;
1412
use crate::module_resolver::typeshed::{vendored_typeshed_versions, TypeshedVersions};
1513
use crate::site_packages::VirtualEnvironment;
1614
use crate::{Program, PythonVersion, SearchPathSettings, SitePackages};
1715

16+
use super::module::{Module, ModuleKind};
17+
use super::path::{ModulePath, SearchPath, SearchPathValidationError};
18+
1819
/// Resolves a module name to a module.
1920
pub fn resolve_module(db: &dyn Db, module_name: ModuleName) -> Option<Module> {
2021
let interned_name = ModuleNameIngredient::new(db, module_name);
@@ -136,7 +137,7 @@ pub(crate) struct SearchPaths {
136137
/// for the first `site-packages` path
137138
site_packages: Vec<SearchPath>,
138139

139-
typeshed_versions: ResolvedTypeshedVersions,
140+
typeshed_versions: TypeshedVersions,
140141
}
141142

142143
impl SearchPaths {
@@ -202,11 +203,11 @@ impl SearchPaths {
202203

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

205-
(ResolvedTypeshedVersions::Custom(parsed), search_path)
206+
(parsed, search_path)
206207
} else {
207208
tracing::debug!("Using vendored stdlib");
208209
(
209-
ResolvedTypeshedVersions::Vendored(vendored_typeshed_versions()),
210+
vendored_typeshed_versions(db),
210211
SearchPath::vendored_stdlib(),
211212
)
212213
};
@@ -279,23 +280,6 @@ impl SearchPaths {
279280
}
280281
}
281282

282-
#[derive(Debug, PartialEq, Eq)]
283-
enum ResolvedTypeshedVersions {
284-
Vendored(&'static TypeshedVersions),
285-
Custom(TypeshedVersions),
286-
}
287-
288-
impl Deref for ResolvedTypeshedVersions {
289-
type Target = TypeshedVersions;
290-
291-
fn deref(&self) -> &Self::Target {
292-
match self {
293-
ResolvedTypeshedVersions::Vendored(versions) => versions,
294-
ResolvedTypeshedVersions::Custom(versions) => versions,
295-
}
296-
}
297-
}
298-
299283
/// Collect all dynamic search paths. For each `site-packages` path:
300284
/// - Collect that `site-packages` path
301285
/// - Collect any search paths listed in `.pth` files in that `site-packages` directory

Diff for: crates/red_knot_python_semantic/src/module_resolver/typeshed/versions.rs

+10-17
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,19 @@ use std::num::{NonZeroU16, NonZeroUsize};
44
use std::ops::{RangeFrom, RangeInclusive};
55
use std::str::FromStr;
66

7-
use once_cell::sync::Lazy;
87
use rustc_hash::FxHashMap;
98

10-
use super::vendored::vendored_typeshed_stubs;
119
use crate::db::Db;
1210
use crate::module_name::ModuleName;
1311
use crate::{Program, PythonVersion};
1412

15-
static VENDORED_VERSIONS: Lazy<TypeshedVersions> = Lazy::new(|| {
13+
pub(in crate::module_resolver) fn vendored_typeshed_versions(db: &dyn Db) -> TypeshedVersions {
1614
TypeshedVersions::from_str(
17-
&vendored_typeshed_stubs()
15+
&db.vendored()
1816
.read_to_string("stdlib/VERSIONS")
19-
.unwrap(),
17+
.expect("The vendored typeshed stubs should contain a VERSIONS file"),
2018
)
21-
.unwrap()
22-
});
23-
24-
pub(crate) fn vendored_typeshed_versions() -> &'static TypeshedVersions {
25-
&VENDORED_VERSIONS
19+
.expect("The VERSIONS file in the vendored typeshed stubs should be well-formed")
2620
}
2721

2822
pub(crate) fn typeshed_versions(db: &dyn Db) -> &TypeshedVersions {
@@ -332,6 +326,8 @@ mod tests {
332326

333327
use insta::assert_snapshot;
334328

329+
use crate::db::tests::TestDb;
330+
335331
use super::*;
336332

337333
const TYPESHED_STDLIB_DIR: &str = "stdlib";
@@ -353,12 +349,9 @@ mod tests {
353349

354350
#[test]
355351
fn can_parse_vendored_versions_file() {
356-
let versions_data = include_str!(concat!(
357-
env!("CARGO_MANIFEST_DIR"),
358-
"/vendor/typeshed/stdlib/VERSIONS"
359-
));
352+
let db = TestDb::new();
360353

361-
let versions = TypeshedVersions::from_str(versions_data).unwrap();
354+
let versions = vendored_typeshed_versions(&db);
362355
assert!(versions.len() > 100);
363356
assert!(versions.len() < 1000);
364357

@@ -395,9 +388,9 @@ mod tests {
395388

396389
#[test]
397390
fn typeshed_versions_consistent_with_vendored_stubs() {
398-
const VERSIONS_DATA: &str = include_str!("../../../vendor/typeshed/stdlib/VERSIONS");
391+
let db = TestDb::new();
392+
let vendored_typeshed_versions = vendored_typeshed_versions(&db);
399393
let vendored_typeshed_dir = Path::new("vendor/typeshed").canonicalize().unwrap();
400-
let vendored_typeshed_versions = TypeshedVersions::from_str(VERSIONS_DATA).unwrap();
401394

402395
let mut empty_iterator = true;
403396

Diff for: crates/ruff_graph/src/db.rs

+6-8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
use anyhow::Result;
2-
use red_knot_python_semantic::{Db, Program, ProgramSettings, PythonVersion, SearchPathSettings};
2+
use red_knot_python_semantic::{
3+
vendored_typeshed_stubs, Db, Program, ProgramSettings, PythonVersion, SearchPathSettings,
4+
};
35
use ruff_db::files::{File, Files};
46
use ruff_db::system::{OsSystem, System, SystemPathBuf};
57
use ruff_db::vendored::VendoredFileSystem;
@@ -11,7 +13,6 @@ pub struct ModuleDb {
1113
storage: salsa::Storage<Self>,
1214
files: Files,
1315
system: OsSystem,
14-
vendored: VendoredFileSystem,
1516
}
1617

1718
impl ModuleDb {
@@ -26,12 +27,10 @@ impl ModuleDb {
2627
.next()
2728
.ok_or_else(|| anyhow::anyhow!("No source roots provided"))?;
2829

29-
let mut search_paths = SearchPathSettings::new(src_root.to_path_buf());
30+
let mut search_paths = SearchPathSettings::new(src_root);
3031

3132
// Add the remaining source roots as extra paths.
32-
for src_root in src_roots {
33-
search_paths.extra_paths.push(src_root.to_path_buf());
34-
}
33+
search_paths.extra_paths.extend(src_roots);
3534

3635
search_paths
3736
};
@@ -54,7 +53,6 @@ impl ModuleDb {
5453
Self {
5554
storage: self.storage.clone(),
5655
system: self.system.clone(),
57-
vendored: self.vendored.clone(),
5856
files: self.files.snapshot(),
5957
}
6058
}
@@ -72,7 +70,7 @@ impl Upcast<dyn SourceDb> for ModuleDb {
7270
#[salsa::db]
7371
impl SourceDb for ModuleDb {
7472
fn vendored(&self) -> &VendoredFileSystem {
75-
&self.vendored
73+
vendored_typeshed_stubs()
7674
}
7775

7876
fn system(&self) -> &dyn System {

0 commit comments

Comments
 (0)