Skip to content

Add type annotations #1090

@ArmaanjeetSandhu

Description

@ArmaanjeetSandhu

CodeCarbon currently ships without type annotations and without a py.typed marker, which means static type checkers cannot provide type checking, autocompletion, or inline documentation for CodeCarbon code. Adding type support would be a meaningful quality-of-life improvement.

What I'm proposing

I'd like to contribute inline type annotations directly to CodeCarbon's source files, following PEP 484 and PEP 561. Concretely, this would involve:

  • Annotating function signatures and class attributes across CodeCarbon's public API, starting with the most commonly used surfaces.
  • Adding an empty codecarbon/py.typed marker file, which signals to type checkers that the package supports typing.
  • Ensuring the annotations are verified by running mypy and/or pyright in CI, so they don't silently drift out of sync with the implementation.

I plan to use stubgen (from mypy) to generate an initial skeleton, then refine the signatures by hand, particularly for the more complex parts of the API that involve union types or overloads.

A separate types-codecarbon package on PyPI is an alternative, but inline annotations are generally the preferred long-term solution because the types live alongside the code and stay in sync naturally with each release. A separate package creates a maintenance burden where type stubs can lag behind the library's actual API.

Before I start

I wanted to open this issue first to check a few things:

  1. Are the maintainers open to this change in principle?
  2. Is there a preference for inline annotations vs. a separate stub package?
  3. Are there any parts of the codebase that might need special treatment?

Happy to start with a focused PR covering just the core public API and expand from there based on feedback. Please do let me know your thoughts!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions