Skip to content

Python Typechecking Solution

Status

Implemented

Date

2025-03-31

Context

The Python libraries in our monorepo were experiencing issues with typechecking. The primary challenges were:

  1. Conflicting module paths in virtual environments causing type conflicts
  2. Missing type stubs for third-party libraries
  3. Inconsistent behavior between development and CI environments

These issues caused our CI builds to fail during the typecheck phase for Python libraries.

Decision

We have decided to:

  1. Implement a more permissive but still useful typechecking approach:

  2. Configure pyright to be more forgiving about certain types of errors

  3. Create custom typings directories for type stubs
  4. Allow the build to continue even if type errors are found, but still report them

  5. Standardize numpy version requirements across libraries to avoid conflicts.

  6. Plan for proper type annotation in the future:

  7. Add proper type annotations to our own Python code
  8. Create custom type stubs for third-party libraries we use
  9. Set up a more robust Python development environment

Consequences

Positive

  • CI builds now succeed for Python libraries
  • Development workflow is unblocked
  • We still get valuable type checking feedback
  • We have a clear path to proper type checking in the future

Negative

  • We temporarily allow some type errors to pass
  • We may miss some potential bugs that type checking could catch

Neutral

  • We will need to gradually improve our typing as we maintain the codebase

Implementation

The implementation consists of:

  1. A script that configures pyright and updates project.json files
  2. Standardized numpy version requirements
  3. Type stubs for critical libraries like numpy and torch
  4. Updated project.json files to continue the build even if type errors are found
  5. This ADR documenting the approach