Skip to content

Commit

Permalink
Get rid of a lot of boilerplate
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexWaygood committed Jul 2, 2024
1 parent 1a82d21 commit d91626c
Show file tree
Hide file tree
Showing 6 changed files with 517 additions and 1,111 deletions.
4 changes: 0 additions & 4 deletions crates/red_knot_module_resolver/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,6 @@ mod typeshed;
pub use db::{Db, Jar};
pub use module::{Module, ModuleKind};
pub use module_name::ModuleName;
pub use path::{
ExtraPath, ExtraPathBuf, FirstPartyPath, FirstPartyPathBuf, SitePackagesPath,
SitePackagesPathBuf, StandardLibraryPath, StandardLibraryPathBuf,
};
pub use resolver::{resolve_module, set_module_resolution_settings, ModuleResolutionSettings};
pub use supported_py_version::SupportedPyVersion;
pub use typeshed::TypeshedVersions;
53 changes: 5 additions & 48 deletions crates/red_knot_module_resolver/src/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@ use std::sync::Arc;
use ruff_db::vfs::VfsFile;

use crate::module_name::ModuleName;
use crate::path::{
ExtraPathBuf, FirstPartyPathBuf, ModuleResolutionPathRef, SitePackagesPathBuf,
StandardLibraryPath, StandardLibraryPathBuf,
};
use crate::path::{ModuleResolutionPath, ModuleResolutionPathRef};
use crate::Db;

/// Representation of a Python module.
Expand All @@ -20,7 +17,7 @@ impl Module {
pub(crate) fn new(
name: ModuleName,
kind: ModuleKind,
search_path: Arc<ModuleSearchPathEntry>,
search_path: Arc<ModuleResolutionPath>,
file: VfsFile,
) -> Self {
Self {
Expand All @@ -44,8 +41,8 @@ impl Module {
}

/// The search path from which the module was resolved.
pub(crate) fn search_path(&self) -> &ModuleSearchPathEntry {
&self.inner.search_path
pub(crate) fn search_path(&self) -> ModuleResolutionPathRef {
ModuleResolutionPathRef::from(&*self.inner.search_path)
}

/// Determine whether this module is a single-file module or a package
Expand Down Expand Up @@ -80,7 +77,7 @@ impl salsa::DebugWithDb<dyn Db> for Module {
struct ModuleInner {
name: ModuleName,
kind: ModuleKind,
search_path: Arc<ModuleSearchPathEntry>,
search_path: Arc<ModuleResolutionPath>,
file: VfsFile,
}

Expand All @@ -92,43 +89,3 @@ pub enum ModuleKind {
/// A python package (`foo/__init__.py` or `foo/__init__.pyi`)
Package,
}

/// Enumeration of the different kinds of search paths type checkers are expected to support.
///
/// N.B. Although we don't implement `Ord` for this enum, they are ordered in terms of the
/// priority that we want to give these modules when resolving them,
/// as per [the order given in the typing spec]
///
/// [the order given in the typing spec]: https://typing.readthedocs.io/en/latest/spec/distributing.html#import-resolution-ordering
#[derive(Debug, Eq, PartialEq, Hash)]
pub(crate) enum ModuleSearchPathEntry {
/// "Extra" paths provided by the user in a config file, env var or CLI flag.
/// E.g. mypy's `MYPYPATH` env var, or pyright's `stubPath` configuration setting
Extra(ExtraPathBuf),

/// Files in the project we're directly being invoked on
FirstParty(FirstPartyPathBuf),

/// The `stdlib` directory of typeshed (either vendored or custom)
StandardLibrary(StandardLibraryPathBuf),

/// Stubs or runtime modules installed in site-packages
SitePackagesThirdParty(SitePackagesPathBuf),
// TODO(Alex): vendor third-party stubs from typeshed as well?
// VendoredThirdParty(VendoredPathBuf),
}

impl ModuleSearchPathEntry {
pub(crate) fn stdlib_from_typeshed_root(typeshed: &StandardLibraryPath) -> Self {
Self::StandardLibrary(StandardLibraryPath::stdlib_from_typeshed_root(typeshed))
}

pub(crate) fn path(&self) -> ModuleResolutionPathRef {
match self {
Self::Extra(path) => ModuleResolutionPathRef::Extra(path),
Self::FirstParty(path) => ModuleResolutionPathRef::FirstParty(path),
Self::StandardLibrary(path) => ModuleResolutionPathRef::StandardLibrary(path),
Self::SitePackagesThirdParty(path) => ModuleResolutionPathRef::SitePackages(path),
}
}
}
Loading

0 comments on commit d91626c

Please sign in to comment.