Skip to content

Enhancement | Type check leverage/ with mypy (gradual) in CI - #355

Merged
diego-ojeda-binbash merged 2 commits into
masterfrom
feature/mypy
Nov 15, 2026
Merged

diego-ojeda-binbash merged 2 commits into
masterfrom
feature/mypy

Conversation

@diego-ojeda-binbash

@diego-ojeda-binbash diego-ojeda-binbash commented Nov 14, 2026 •

Copy link
Copy Markdown
Contributor

What?

mypy now type checks leverage/ in the required unit_tests job. It starts with the 52 errors it found fixed.

  • Version: mypy 1.19.1, the last release that runs on and checks for Python 3.9 (1.20 dropped it), plus types-python-dateutil.
  • Gradual config in [tool.mypy]: it checks annotated code only, so unannotated functions can be added module by module later (e.g. check_untyped_defs). boto3, botocore and simple_term_menu publish no types and are ignored.
  • CI: a "Type check (mypy)" step in unit_tests, on 3.13 only: it checks for 3.9 whatever the runtime. It is not a pre-commit hook, because it needs all the dependencies installed.

The errors

8 came from libraries without types. The other 44 were fixed in the code, without changing behavior:

Where Fix
Runner.run Overloads: int when interactive, (code, stdout, stderr) when not. _handle_subcommand was handing the union to click.Exit
leverage.py Imports each command from its module. leverage.modules.aws and the like are also submodules, so mypy saw modules instead of commands
ConfigUpdater values New get_option_value helper in _utils.py (with tests), used in auth.py and aws.py. A missing MFA role_arn now raises NoOptionError naming the option, not AttributeError on None
path.py, kubectl.py, auth.py, credentials.py, modules/utils.py Optional for None defaults, renamed variables reused with another type, a Group assert

Two commented # type: ignores:

  • TFRunner.run is always interactive, on purpose: exec() captures the output.
  • mypy can't infer the type of the run command through the leverage/__init__ ↔ leverage.leverage import cycle.

The account lookup in aws configure sso is typed as Any: it walks the HCL data by catching AttributeError. Rewriting it is out of scope here.

Results

  • mypy: no issues in 22 files.
  • 331 tests pass. Coverage skips the @overload stubs.

Relates to #346

🤖 Generated with Claude Code

Summary by CodeRabbit

  • Bug Fixes
    • Missing or empty configuration options now raise an explicit error identifying the option and section.
  • Refactor
    • Improved type annotations for command execution and optional paths without changing their runtime behavior.
  • Chores
    • Added automated type checking to the unit-test workflow and documented the check.

diego-ojeda-binbash and others added 2 commits November 14, 2026 19:54
- Runner.run: overloads, int when interactive, (code, stdout, stderr) when not
- leverage.py: import each command from its module (leverage.modules.aws and
  the like are also submodules, so mypy saw modules instead of commands)
- get_option_value in _utils: a ConfigUpdater option value is Optional; a
  missing value now raises NoOptionError instead of AttributeError on None.
  Used by auth.py and aws.py
- Optional for arguments defaulting to None, renamed variables reused with
  another type, Group assert in _handle_subcommand
- Two commented ignores: TFRunner.run is always interactive, and the type of
  the `run` command can't be inferred through the leverage/__init__ import cycle

No behavior change.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
mypy 1.19.1 (the last release that runs on and checks for Python 3.9) and
types-python-dateutil as dev dependencies, a gradual [tool.mypy] config
(annotated code only, no stubs for boto3/botocore/simple_term_menu) and a
step in the required unit_tests job, on 3.13 only. Coverage skips @overload
stubs.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@diego-ojeda-binbash diego-ojeda-binbash added enhancement New feature or request test labels Nov 14, 2026
@coderabbitai

coderabbitai Bot commented Nov 14, 2026 •

Copy link
Copy Markdown
Contributor

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

The pull request adds mypy to the development dependencies and unit-test workflow. It adds a configuration option helper, uses it in authentication and AWS code, and updates runner return types and other annotations.

Changes

mypy Adoption

Layer / File(s) Summary
Configure and run mypy
pyproject.toml, .github/workflows/tests-unit.yml, CLAUDE.md
Adds mypy dependencies and configuration. The unit-test workflow runs mypy in the Python 3.13 job. The quality commands document the check.
Centralize configuration option reads
leverage/_utils.py, leverage/modules/auth.py, leverage/modules/aws.py, tests/test_utils.py
Adds get_option_value and uses it for configuration reads in authentication and AWS code. Tests cover existing, missing, and valueless options.
Type command runner results
leverage/modules/runner.py, leverage/modules/tfrunner.py, .coveragerc
Adds return-type overloads for Runner.run and return annotations for TFRunner.run and TFRunner.exec. The coverage configuration excludes @overload declarations. Runner.binary_path uses an empty string for an unresolved path.
Update command and parameter typing
leverage/leverage.py, leverage/modules/utils.py, leverage/modules/kubectl.py, leverage/path.py, leverage/modules/credentials.py
Updates command imports and subcommand lookup, annotates optional parameters, and changes how _update_account_ids builds its mapping.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Change: Other

Suggested reviewers: angelofenoglio, franr, borland667

Merge Risk: Minimal · up to 5ddac

This change adds mypy type checking to CI and makes small typing-related code adjustments. No concrete merge-blocking risk was found.

Pre-merge checks: Passed 4

Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title "Enhancement | Type check leverage/ with mypy (gradual) in CI" matches the main change. The PR adds gradual mypy type checking for leverage/ to the unit_tests CI job, and the raw summary con…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Title check

Explanation

The title "Enhancement | Type check leverage/ with mypy (gradual) in CI" matches the main change. The PR adds gradual mypy type checking for leverage/ to the unit_tests CI job, and the raw summary confirms the workflow step, pyproject.toml mypy configuration, and the type annotations and overloads that support it. The "Enhancement |" prefix is a category label and adds little information, but the rest of the title is specific and accurate.

Finishing Touches

Generate unit tests (beta)

  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

A rabbit checks types in the code,
Then hops down a well-marked path.
The runner returns what its types declare,
Config values meet tests with care,
And the rabbit celebrates a cleaner map.

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Repository UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 911f2033-6d27-41df-a595-8bd6f8508f82
Commits

Reviewing files that changed from the base of the PR and between c488224 and 5ddacda.

Files ignored due to path filters: 1

  • poetry.lock is excluded by !**/*.lock
Files selected for processing: 15

  • .coveragerc
  • .github/workflows/tests-unit.yml
  • CLAUDE.md
  • leverage/_utils.py
  • leverage/leverage.py
  • leverage/modules/auth.py
  • leverage/modules/aws.py
  • leverage/modules/credentials.py
  • leverage/modules/kubectl.py
  • leverage/modules/runner.py
  • leverage/modules/tfrunner.py
  • leverage/modules/utils.py
  • leverage/path.py
  • pyproject.toml
  • tests/test_utils.py

Included review availability: This review used your included allowance. Your plan provides up to 1 included review per hour; 0 remain after this review.

@coveralls

Copy link
Copy Markdown

Coverage Report for CI Build 43650528200

Coverage increased (+0.2%) to 77.28%

Details

  • Coverage increased (+0.2%) from the base build.
  • Patch coverage: 57 of 57 lines across 10 files are fully covered (100%).
  • 41 coverage regressions across 7 files.

Uncovered Changes

No uncovered changes found.

Coverage Regressions

41 previously-covered lines in 7 files lost coverage.

File Lines Losing Coverage Coverage
path.py 14 82.89%
modules/kubectl.py 13 60.98%
_utils.py 5 82.5%
leverage.py 4 86.67%
modules/auth.py 2 93.49%
modules/aws.py 2 97.32%
modules/tfrunner.py 1 94.29%

Coverage Stats

Coverage Status
Relevant Lines: 4100
Covered Lines: 3286
Line Coverage: 80.15%
Relevant Branches: 1076
Covered Branches: 714
Branch Coverage: 66.36%
Branches in Coverage %: Yes
Coverage Strength: 0.8 hits per line

💛 - Coveralls

@diego-ojeda-binbash diego-ojeda-binbash self-assigned this Nov 14, 2026
@diego-ojeda-binbash

Copy link
Copy Markdown
Contributor Author

@coderabbitai review

@coderabbitai

coderabbitai Bot commented Nov 14, 2026 •

Copy link
Copy Markdown
Contributor
Action performed

Review finished.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@github-actions github-actions Bot added the minor label Nov 14, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request minor test

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants