Expose the import resolver to Ruff

This commit is contained in:
Charlie Marsh
2023-06-27 16:29:46 -04:00
parent 2d2673f613
commit 1cb500ffd9
17 changed files with 107 additions and 107 deletions

View File

@@ -1,18 +1,19 @@
use std::path::PathBuf;
pub(crate) struct Config {
#[derive(Debug, Default)]
pub struct Config {
/// Path to use for typeshed definitions.
pub(crate) typeshed_path: Option<PathBuf>,
pub typeshed_path: Option<PathBuf>,
/// Path to custom typings (stub) modules.
pub(crate) stub_path: Option<PathBuf>,
pub stub_path: Option<PathBuf>,
/// Path to a directory containing one or more virtual environment
/// directories. This is used in conjunction with the "venv" name in
/// the config file to identify the python environment used for resolving
/// third-party modules.
pub(crate) venv_path: Option<PathBuf>,
pub venv_path: Option<PathBuf>,
/// Default venv environment.
pub(crate) venv: Option<PathBuf>,
pub venv: Option<PathBuf>,
}

View File

@@ -4,16 +4,16 @@ use crate::python_platform::PythonPlatform;
use crate::python_version::PythonVersion;
#[derive(Debug)]
pub(crate) struct ExecutionEnvironment {
pub struct ExecutionEnvironment {
/// The root directory of the execution environment.
pub(crate) root: PathBuf,
pub root: PathBuf,
/// The Python version of the execution environment.
pub(crate) python_version: PythonVersion,
pub python_version: PythonVersion,
/// The Python platform of the execution environment.
pub(crate) python_platform: PythonPlatform,
pub python_platform: PythonPlatform,
/// The extra search paths of the execution environment.
pub(crate) extra_paths: Vec<PathBuf>,
pub extra_paths: Vec<PathBuf>,
}

View File

@@ -6,7 +6,7 @@ use crate::python_platform::PythonPlatform;
use crate::python_version::PythonVersion;
/// A trait to expose the host environment to the resolver.
pub(crate) trait Host {
pub trait Host {
/// The search paths to use when resolving Python modules.
fn python_search_paths(&self) -> Vec<PathBuf>;
@@ -18,12 +18,13 @@ pub(crate) trait Host {
}
/// A host that exposes a fixed set of search paths.
pub(crate) struct StaticHost {
#[derive(Debug, Default)]
pub struct StaticHost {
search_paths: Vec<PathBuf>,
}
impl StaticHost {
pub(crate) fn new(search_paths: Vec<PathBuf>) -> Self {
pub fn new(search_paths: Vec<PathBuf>) -> Self {
Self { search_paths }
}
}

View File

@@ -7,12 +7,12 @@ use crate::py_typed::PyTypedInfo;
#[derive(Debug, Clone, PartialEq, Eq)]
#[allow(clippy::struct_excessive_bools)]
pub(crate) struct ImportResult {
pub struct ImportResult {
/// Whether the import name was relative (e.g., ".foo").
pub(crate) is_relative: bool,
/// Whether the import was resolved to a file or module.
pub(crate) is_import_found: bool,
pub is_import_found: bool,
/// The path was partially resolved, but the specific submodule
/// defining the import was not found. For example, `foo.bar` was
@@ -32,7 +32,7 @@ pub(crate) struct ImportResult {
pub(crate) is_stub_package: bool,
/// The import resolved to a built-in, local, or third-party module.
pub(crate) import_type: ImportType,
pub import_type: ImportType,
/// A vector of resolved absolute paths for each file in the module
/// name. Typically includes a sequence of `__init__.py` files, followed
@@ -114,7 +114,7 @@ impl ImportResult {
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum ImportType {
pub enum ImportType {
BuiltIn,
ThirdParty,
Local,

View File

@@ -1,5 +1,14 @@
#![allow(dead_code)]
pub use config::*;
pub use execution_environment::*;
pub use host::*;
pub use import_result::*;
pub use module_descriptor::*;
pub use python_platform::*;
pub use python_version::*;
pub use resolver::*;
mod config;
mod execution_environment;
mod host;

View File

@@ -1,8 +1,8 @@
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) struct ImportModuleDescriptor {
pub(crate) leading_dots: usize,
pub(crate) name_parts: Vec<String>,
pub(crate) imported_symbols: Vec<String>,
pub struct ImportModuleDescriptor {
pub leading_dots: usize,
pub name_parts: Vec<String>,
pub imported_symbols: Vec<String>,
}
impl ImportModuleDescriptor {

View File

@@ -1,6 +1,6 @@
/// Enum to represent a Python platform.
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub(crate) enum PythonPlatform {
pub enum PythonPlatform {
Darwin,
Linux,
Windows,

View File

@@ -1,6 +1,6 @@
/// Enum to represent a Python version.
#[derive(Debug, Copy, Clone)]
pub(crate) enum PythonVersion {
pub enum PythonVersion {
Py37,
Py38,
Py39,

View File

@@ -693,7 +693,7 @@ fn resolve_import_strict<Host: host::Host>(
/// 3. If a stub file was found, find the "best" match for the import, disallowing stub files.
/// 4. If the import wasn't resolved, try to resolve it in the parent directory, then the parent's
/// parent, and so on, until the import root is reached.
pub(crate) fn resolve_import<Host: host::Host>(
pub fn resolve_import<Host: host::Host>(
source_file: &Path,
execution_environment: &ExecutionEnvironment,
module_descriptor: &ImportModuleDescriptor,
@@ -715,37 +715,28 @@ pub(crate) fn resolve_import<Host: host::Host>(
// importing file's directory, then the parent directory, and so on, until the
// import root is reached.
let root = execution_environment.root.as_path();
if source_file.starts_with(root) {
let mut current = source_file;
while let Some(parent) = current.parent() {
if parent == root {
break;
}
debug!("Resolving absolute import in parent: {}", parent.display());
let mut result = resolve_absolute_import(
parent,
module_descriptor,
false,
false,
false,
true,
false,
);
if result.is_import_found {
if let Some(implicit_imports) = result
.implicit_imports
.filter(&module_descriptor.imported_symbols)
{
result.implicit_imports = implicit_imports;
}
return result;
}
current = parent;
let mut current = source_file;
while let Some(parent) = current.parent() {
if !parent.starts_with(root) {
break;
}
debug!("Resolving absolute import in parent: {}", parent.display());
let mut result =
resolve_absolute_import(parent, module_descriptor, false, false, false, true, false);
if result.is_import_found {
if let Some(implicit_imports) = result
.implicit_imports
.filter(&module_descriptor.imported_symbols)
{
result.implicit_imports = implicit_imports;
}
return result;
}
current = parent;
}
ImportResult::not_found()