Skip to content

monty-type-checking

Type checking for Monty sessions, powered by ty: checks code against Monty’s trimmed typeshed before execution.

SourceFile

pub struct SourceFile<'a> {
    /// source code
    pub source_code: &'a str,
    /// file path
    pub path: &'a str,
}

Definition of a source file.

new

pub fn new(source_code: &'a str, path: &'a str) -> Self

Create a new source file.

TypeChecker

pub struct TypeChecker { /* private fields */ }

run

pub fn run<'a>(
    &'a mut self,
    python_source: &SourceFile<'_>,
    stubs_file: Option<&SourceFile<'_>>,
    config: TypeCheckingConfig,
) -> Result<Option<TypeCheckingDiagnostics<'a>>, String>

Type check some python source code, checking if it’s valid to run with monty.

Arguments

  • python_source - The python source code to type check.
  • stubs_file - Optional stubs file to use for type checking.
  • config - Configuration for the type checking diagnostics.

Returns

  • Ok(None) - If there are no typing errors.
  • Ok(Some(string)) - If there are typing errors.
  • Err(String) - If there was an unexpected/internal error during type checking.

reset

pub fn reset(&mut self) -> Result<(), String>

Remove every file written since the last reset and sync the filesystem changes, so the checker can be reused for unrelated code.

Security-critical: without this a recycled worker would let one session’s modules and stubs resolve while checking the next session’s code. Each TouchedRootFile::cleanup removes its own file and walks its ancestor chain up to SRC_ROOT, removing any directory that has become empty. Shared parent directories collapse naturally once the last file inside them is gone. We sync SRC_ROOT once at the end so the next session cannot observe the previous root directory listing.

Implements: Debug, Default.

TypeCheckingDiagnostics

pub struct TypeCheckingDiagnostics<'a> { /* private fields */ }

The diagnostics of one failed type check, rendered on Display.

Borrows the checker because ty’s diagnostics only resolve their spans (file paths, source snippets) against the database that produced them — which is why the format is chosen up front, in TypeChecker::run, and why callers that outlive the checker (anything across a process boundary) must keep the rendered string rather than this.

Implements: Display.