Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
Discussion options

I am preparing a small contribution, but I’m seeing mismatches between my local checks and what passes in CI. Locally, pre-commit (ruff + mypy) fails with type‑checking errors in files I did not modify, while the main branch appears clean. I suspect a tool‑version or environment drift between my machine and CI.

Steps to Reproduce

git clone https://github.com/TheAlgorithms/Python
cd Python

python -m venv .venv
source .venv/bin/activate   # Windows: .venv\Scripts\activate

python -m pip install --upgrade pip
python -m pip install pre-commit
pre-commit install

pre-commit run --all-files --show-diff-on-failure

python -m pip install mypy
mypy .

Actual Result

Mypy reports errors in unrelated modules (e.g., incompatible types) even though the same files appear to pass CI on main.

Question

What is the correct way to align local tool versions (Python, ruff, mypy, pre‑commit hooks) with the versions used in CI so that local checks match CI behavior?

If type errors appear outside the files I changed, is it recommended to limit checks to staged files or open a separate PR to address repository‑wide issues?

You must be logged in to vote

Replies: 5 comments · 1 reply

Comment options

You’re right — this is almost always caused by tool version or environment drift
between local setup and CI.

🔧 How to align local checks with CI

  1. Pin to the repo’s pre-commit tool versions

    • Check .pre-commit-config.yaml for the exact rev values (ruff, mypy, etc.).
    • Then run:
      pre-commit clean
      pre-commit run --all-files

    This ensures your local hooks use the same versions CI uses.

  2. Match the Python version used in CI

    • Check the GitHub Actions workflow for the Python version matrix.
    • Create your virtual environment with the same Python version.
    • Python version mismatches commonly surface as mypy errors.
  3. Install repo-pinned dependencies

    • Prefer any provided requirements.txt or requirements-dev.txt.
    • Avoid installing tools globally (e.g. pip install mypy) unless CI does the same.
  4. Treat pre-commit as the source of truth

    • CI usually runs:
      pre-commit run --all-files
    • Running mypy . directly can differ because pre-commit may pass additional flags
      or configuration options.

📁 About errors in files you didn’t modify

  • It’s not expected to fix unrelated type issues in the same PR.
  • Recommended approach:
    • Keep the PR focused on your change.
    • If you uncover genuine repo-wide issues, open a separate PR or discussion.
    • Limiting checks to staged files is usually discouraged unless the project
      explicitly recommends it.
You must be logged in to vote
0 replies
Comment options

It looks like you're dealing with a classic cache mismatch or environment drift. Since aayush-dev01 mentioned the version alignment, here is the specific command sequence to force your local environment to exactly match the GitHub Actions state:

Force-update your hooks: Sometimes pre-commit caches an older version of the hook even if the config changes.
Bash

pre-commit autoupdate
pre-commit clean

Run via the environment, not the system: Instead of running mypy ., always run it through pre-commit to ensure the flags in .pre-commit-config.yaml are applied:
Bash

pre-commit run mypy --all-files

The "Shadow" Dependencies: If errors appear in files you didn't touch, it's often because your local venv has a newer (or older) version of a library than the one pinned in the repo's requirements.txt. Re-sync your venv:
Bash

pip install -r requirements.txt --force-reinstall

Regarding the unrelated errors: Definitely do not fix them in your PR. It makes the code review much harder. Keep your PR "atomic" (only your changes). If the errors persist but CI passes, you can safely ignore them as long as your specific changes are clean!

Hope this helps you get that contribution merged! 🚀

You must be logged in to vote
0 replies
Comment options

Root Cause: You're Running Tools Outside of pre-commit
The issue is in your reproduction steps - you're installing and running mypy separately:

python -m pip install mypy # ❌ This is the problem
mypy . # ❌ This bypasses pre-commit config
When you run mypy . directly, it uses:

Whatever mypy version you installed (likely latest)
Default mypy configuration (not the repo's)
All files in the directory
The CI uses pre-commit hooks which:

Pin specific tool versions in .pre-commit-config.yaml
Use the project's mypy configuration
Only check relevant files with specific flags
The Fix: Only Use pre-commit
git clone https://github.com/TheAlgorithms/Python
cd Python

python -m venv .venv
source .venv/bin/activate

pip install --upgrade pip
pip install pre-commit # Only install pre-commit
pre-commit install

This is all you need - matches CI exactly

pre-commit run --all-files
Don't install mypy, ruff, or any other tools separately. pre-commit handles everything.

Verifying Your Setup Matches CI
Check tool versions:

cat .pre-commit-config.yaml
Look for the rev: values - these are the exact versions CI uses.

Check Python version:

cat .github/workflows/*.yml | grep python-version
Make sure your venv uses the same Python version.

Verify pre-commit is working:

pre-commit run --all-files --verbose
This should show which hooks are running and their versions.

If You See Errors in Files You Didn't Modify
First, verify main branch is actually clean:

git checkout main
git pull origin main
pre-commit run --all-files
If main is clean but you still see errors:

Your branch might be outdated:

git fetch origin
git rebase origin/main
Pre-commit cache might be stale:

pre-commit clean
pre-commit install --install-hooks
pre-commit run --all-files
For your PR, only check your changes:

Stage your changes

git add path/to/your/file.py

Run checks only on staged files

pre-commit run
Answering Your Questions
Q: What is the correct way to align local tool versions with CI?

A: Only use pre-commit. Don't install mypy, ruff, or other tools separately. The .pre-commit-config.yaml file pins all versions to match CI exactly.

Q: If type errors appear outside the files I changed, what should I do?

A:

Don't fix them in your PR - keep your PR focused on your changes
Verify main is clean - if main has errors, that's a separate issue
For your PR - use pre-commit run (without --all-files) to only check staged files
If you find repo-wide issues - open a separate issue/PR to discuss with maintainers
Pro Tip: Pre-commit Workflow

Before making changes

git checkout -b my-feature
pre-commit run --all-files # Verify main is clean

Make your changes

... edit files ...

Check only your changes

git add .
pre-commit run # Only checks staged files

If all passes, commit

git commit -m "Your changes"
This ensures your local checks match CI behavior exactly, and you only deal with errors in files you actually modified.

You must be logged in to vote
1 reply
@dinakars777
Comment options

@devilfoo - Hope you are still following this Q&A thread.

Comment options

The key issue is version drift. Here's how to fix it:

1. Use the exact Python version from CI

Check .github/workflows/ci.yml for the Python version used in CI, then match it locally:

pyenv install 3.12.0   # or whatever CI uses
pyenv local 3.12.0

Or use runtime.txt / .python-version if the repo has one.

2. Install pinned dependencies

The repo's requirements.txt or pyproject.toml has pinned versions. Install them exactly:

python -m pip install -r requirements.txt
pre-commit install

The critical part: run pre-commit hooks using the same versions CI uses. The .pre-commit-config.yaml file at the repo root pins exact hook versions (e.g., rev: v0.6.9 for ruff). Make sure you have the latest hooks:

pre-commit autoupdate
pre-commit run --all-files

3. Pin mypy to the CI version

Don't pip install mypy separately — instead install the version specified in CI. Check .github/workflows/ci.yml for the mypy version, then:

python -m pip install mypy==1.11.0  # match CI version

Or better, use the exact environment from CI:

python -m pip install -e .[dev]  # if dev extras exist

4. Limit checks to staged files only

This is the recommended workflow and avoids being blocked by pre-existing errors:

pre-commit run  # without --all-files → checks only staged changes

Or for mypy specifically:

mypy path/to/your/file.py  # check only the files you changed

5. If you hit pre-existing type errors

Open a separate PR to fix them, or mention in your PR description that the errors are pre-existing and unrelated to your change. Most maintainers prefer focused PRs.

Quick fix checklist

  1. python --version matches CI
  2. pre-commit --version matches the hook versions in .pre-commit-config.yaml
  3. Use pre-commit run (no --all-files) to check only your changes
  4. Run mypy only on your files: mypy your_file.py
  5. If CI uses uv or poetry, use the same lock file
You must be logged in to vote
0 replies
Comment options

Discrepancies between local pre-commit hooks and CI arise because local checks often scan only staged files while CI evaluates the full repository, combined with differing environments. To align them, run pre-commit run mypy --all-files locally, clear caches, and define required dependencies in .pre-commit-config.yaml. Detailed community solutions can be found on GitHub and Stack Overflow.

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
🙏
Q&A
Labels
None yet
6 participants
Morty Proxy This is a proxified and sanitized view of the page, visit original site.