Skip to content

Commit 8a25531

Browse files
authored
red-knot: improve internal documentation in module.rs (#11638)
1 parent 9b6d2ce commit 8a25531

File tree

1 file changed

+28
-2
lines changed

1 file changed

+28
-2
lines changed

crates/red_knot/src/module.rs

+28-2
Original file line numberDiff line numberDiff line change
@@ -17,27 +17,40 @@ use crate::FxDashMap;
1717
pub struct Module(u32);
1818

1919
impl Module {
20+
/// Return the absolute name of the module (e.g. `foo.bar`)
2021
pub fn name(&self, db: &dyn SemanticDb) -> QueryResult<ModuleName> {
2122
let jar: &SemanticJar = db.jar()?;
2223
let modules = &jar.module_resolver;
2324

2425
Ok(modules.modules.get(self).unwrap().name.clone())
2526
}
2627

28+
/// Return the path to the source code that defines this module
2729
pub fn path(&self, db: &dyn SemanticDb) -> QueryResult<ModulePath> {
2830
let jar: &SemanticJar = db.jar()?;
2931
let modules = &jar.module_resolver;
3032

3133
Ok(modules.modules.get(self).unwrap().path.clone())
3234
}
3335

36+
/// Determine whether this module is a single-file module or a package
3437
pub fn kind(&self, db: &dyn SemanticDb) -> QueryResult<ModuleKind> {
3538
let jar: &SemanticJar = db.jar()?;
3639
let modules = &jar.module_resolver;
3740

3841
Ok(modules.modules.get(self).unwrap().kind)
3942
}
4043

44+
/// Attempt to resolve a dependency of this module to an absolute [`ModuleName`].
45+
///
46+
/// A dependency could be either absolute (e.g. the `foo` dependency implied by `from foo import bar`)
47+
/// or relative to this module (e.g. the `.foo` dependency implied by `from .foo import bar`)
48+
///
49+
/// - Returns an error if the query failed.
50+
/// - Returns `Ok(None)` if the query succeeded,
51+
/// but the dependency refers to a module that does not exist.
52+
/// - Returns `Ok(Some(ModuleName))` if the query succeeded,
53+
/// and the dependency refers to a module that exists.
4154
pub fn resolve_dependency(
4255
&self,
4356
db: &dyn SemanticDb,
@@ -124,10 +137,13 @@ impl ModuleName {
124137
Some(Self(name))
125138
}
126139

140+
/// An iterator over the components of the module name:
141+
/// `foo.bar.baz` -> `foo`, `bar`, `baz`
127142
pub fn components(&self) -> impl DoubleEndedIterator<Item = &str> {
128143
self.0.split('.')
129144
}
130145

146+
/// The name of this module's immediate parent, if it has a parent
131147
pub fn parent(&self) -> Option<ModuleName> {
132148
let (_, parent) = self.0.rsplit_once('.')?;
133149

@@ -159,9 +175,10 @@ impl std::fmt::Display for ModuleName {
159175

160176
#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)]
161177
pub enum ModuleKind {
178+
/// A single-file module (e.g. `foo.py` or `foo.pyi`)
162179
Module,
163180

164-
/// A python package (a `__init__.py` or `__init__.pyi` file)
181+
/// A python package (`foo/__init__.py` or `foo/__init__.pyi`)
165182
Package,
166183
}
167184

@@ -181,10 +198,12 @@ impl ModuleSearchPath {
181198
}
182199
}
183200

201+
/// Determine whether this is a first-party, third-party or standard-library search path
184202
pub fn kind(&self) -> ModuleSearchPathKind {
185203
self.inner.kind
186204
}
187205

206+
/// Return the location of the search path on the file system
188207
pub fn path(&self) -> &Path {
189208
&self.inner.path
190209
}
@@ -459,6 +478,7 @@ impl ModuleResolver {
459478
}
460479
}
461480

481+
/// Remove a module from the inner cache
462482
pub(crate) fn remove_module(&mut self, file_id: FileId) {
463483
// No locking is required because we're holding a mutable reference to `self`.
464484
let Some((_, id)) = self.by_file.remove(&file_id) else {
@@ -505,15 +525,19 @@ impl ModulePath {
505525
Self { root, file_id }
506526
}
507527

528+
/// The search path that was used to locate the module
508529
pub fn root(&self) -> &ModuleSearchPath {
509530
&self.root
510531
}
511532

533+
/// The file containing the source code for the module
512534
pub fn file(&self) -> FileId {
513535
self.file_id
514536
}
515537
}
516538

539+
/// Given a module name and a list of search paths in which to lookup modules,
540+
/// attempt to resolve the module name
517541
fn resolve_name(
518542
name: &ModuleName,
519543
search_paths: &[ModuleSearchPath],
@@ -635,7 +659,9 @@ enum PackageKind {
635659
/// A root package or module. E.g. `foo` in `foo.bar.baz` or just `foo`.
636660
Root,
637661

638-
/// A regular sub-package where the parent contains an `__init__.py`. For example `bar` in `foo.bar` when the `foo` directory contains an `__init__.py`.
662+
/// A regular sub-package where the parent contains an `__init__.py`.
663+
///
664+
/// For example, `bar` in `foo.bar` when the `foo` directory contains an `__init__.py`.
639665
Regular,
640666

641667
/// A sub-package in a namespace package. A namespace package is a package without an `__init__.py`.

0 commit comments

Comments
 (0)