ty_python_core/
program.rs1use crate::{Db, platform::PythonPlatform};
2
3use ruff_db::files::File;
4use ruff_db::system::SystemPath;
5use ruff_db::vendored::VendoredFileSystem;
6use ruff_python_ast::PythonVersion;
7use ty_module_resolver::{ResolverEnvironment, SearchPaths};
8use ty_site_packages::PythonVersionWithSource;
9
10use crate::ProgramFile;
11
12pub use ty_module_resolver::{FallibleStrategy, MisconfigurationStrategy, UseDefaultStrategy};
14
15#[salsa::interned(debug, heap_size=ruff_memory_usage::heap_size)]
16pub struct Program<'db> {
17 #[returns(ref)]
18 pub python_platform: PythonPlatform,
19
20 #[returns(copy)]
21 pub resolver_environment: ResolverEnvironment<'db>,
22}
23
24impl get_size2::GetSize for Program<'_> {}
25
26impl<'db> Program<'db> {
27 pub fn from_settings(db: &'db dyn Db, settings: &ProgramSettings) -> Self {
29 let ProgramSettings {
30 python_version,
31 python_platform,
32 search_paths,
33 } = settings;
34
35 let resolver_environment =
36 ResolverEnvironment::new(db, python_version.version, search_paths);
37 Program::new(db, python_platform, resolver_environment)
38 }
39
40 pub fn python_version(self, db: &'db dyn Db) -> PythonVersion {
41 self.resolver_environment(db).python_version(db)
42 }
43
44 pub fn search_paths(self, db: &'db dyn Db) -> &'db SearchPaths {
45 self.resolver_environment(db).search_paths(db)
46 }
47
48 pub fn program_file(self, db: &'db dyn Db, file: File) -> ProgramFile<'db> {
49 ProgramFile::new(db, file, self)
50 }
51
52 pub fn custom_stdlib_search_path(self, db: &'db dyn Db) -> Option<&'db SystemPath> {
53 self.search_paths(db).custom_stdlib()
54 }
55}
56
57#[derive(Clone, Debug, Eq, PartialEq, get_size2::GetSize)]
58pub struct ProgramSettings {
59 pub python_version: PythonVersionWithSource,
60 pub python_platform: PythonPlatform,
61 pub search_paths: SearchPaths,
62}
63
64impl ProgramSettings {
65 pub fn empty(vendored: &VendoredFileSystem) -> Self {
66 Self {
67 python_version: PythonVersionWithSource::default(),
68 python_platform: PythonPlatform::default(),
69 search_paths: SearchPaths::empty(vendored),
70 }
71 }
72}