This document describes the recommended, low-risk migration path for replacing mypy with pyrefly in this repository.
- Move type checking from mypy to pyrefly.
- Keep CI and local workflows green during migration.
- Avoid a risky one-shot switch.
This template supports two local development modes.
- A parent/shared environment is activated before working in this package.
- Use
--activefor localuv runcommands. - Use
uv sync --inexactto avoid pruning sibling-project packages.
- The package has its own local project environment.
- Do not use
--active. - Use normal exact sync behavior.
Quick command mapping:
Monorepo: uv sync --group dev --active --inexact
Standalone: uv sync --group dev
Monorepo: uv run --active pyrefly check src tests
Standalone: uv run pyrefly check src tests
Because this repository is a template, keep a single .pre-commit-config.yaml for all contributors.
Use a small wrapper script (scripts/precommit_uv_run.sh) as the hook entry:
- If
VIRTUAL_ENVis set, it runsuv run --active ...(monorepo/shared-env mode). - If
VIRTUAL_ENVis not set, it runsuv run ...(standalone mode).
This avoids maintaining separate pre-commit configs for different project layouts.
This package lives inside a monorepo and should use the parent/shared virtual environment.
- Do not create or rely on a local package
.venv. - For local commands, use
uv --activemode souvuses the active environment. - Avoid running plain
uv run ...locally in this package; it may create a local.venv. - In a shared monorepo environment, use
uv sync --inexactto avoid pruning packages installed by sibling projects.
Before running local commands, make sure the shared monorepo environment is actually active:
echo "$VIRTUAL_ENV"If this is empty, uv run --active ... may fall back to the project environment path and create a local .venv.
In this monorepo setup, --active only changes the target environment. It does not change sync reconciliation behavior.
uv sync --activeis still exact by default.- Exact sync prunes packages not declared by the current package's
pyproject.toml. - In a shared environment, this can remove dependencies required by sibling projects.
Use this local command pattern instead:
uv sync --group dev --active --inexactAlso expected warning context:
- If
VIRTUAL_ENVpoints to the monorepo environment but the current package has a project.venvpath,uvmay warn about a mismatch. --activetellsuvto target the active environment explicitly.
Examples:
uv sync --group dev --active --inexact
uv run --active pyrefly check src tests- pyrefly check currently succeeds on src and tests.
- pyrefly currently imports settings from tool.mypy in pyproject.toml (legacy mode).
- Pre-commit is configured with local hooks for Ruff and pyrefly in
.pre-commit-config.yaml. - CI type checking uses pyrefly in
.github/workflows/test.yml.
Use a phased rollout:
- Add pyrefly to development dependencies.
- Generate explicit pyrefly config.
- Run mypy and pyrefly in parallel for a short period.
- Switch pre-commit and CI to pyrefly.
- Remove mypy configuration and dependency.
- Update docs and contributor guidance.
If pyrefly is not already present in dependency-groups.dev in pyproject.toml, add it first.
Example:
[dependency-groups]
dev = [
"pyrefly>=1.0.0",
]Then refresh the environment:
uv sync --group dev --active --inexactStandalone alternative:
uv sync --group devExpected outcome:
- pyrefly is available in local and CI development environments.
Preview migration output first:
uv run --active pyrefly init pyproject.toml --migrate-from mypy --dry-run --print-configStandalone alternative:
uv run pyrefly init pyproject.toml --migrate-from mypy --dry-run --print-configThen write the configuration:
uv run --active pyrefly init pyproject.toml --migrate-from mypy --non-interactiveStandalone alternative:
uv run pyrefly init pyproject.toml --migrate-from mypy --non-interactiveExpected outcome:
- A tool.pyrefly section is written to pyproject.toml.
- pyrefly no longer depends on implicit legacy import behavior.
For 1-2 PRs, run both checkers locally and in CI:
uv run --active mypy src tests
uv run --active pyrefly check src testsStandalone alternative:
uv run mypy src tests
uv run pyrefly check src testsExpected outcome:
- Confident parity before cutover.
- Any edge-case behavior differences are detected early.
Update tool.hatch.envs.types.scripts.check in pyproject.toml from:
mypy {args:src tests}
to:
pyrefly check {args:src tests}
Expected outcome:
- hatch run types:check uses pyrefly.
Switch pre-commit from remote mypy/Ruff hooks to local hooks for Ruff and pyrefly.
Status in this repository:
- This step is already applied.
Important:
- This repository now uses a
repo: localsection in.pre-commit-config.yaml. - Keep Ruff and pyrefly hooks in the same local section so they all use the active monorepo environment.
Recommended local hook block:
- repo: local
hooks:
- id: ruff
name: ruff
entry: scripts/precommit_uv_run.sh ruff check --fix
language: system
types: [python]
- id: ruff-format
name: ruff-format
entry: scripts/precommit_uv_run.sh ruff format
language: system
types: [python]
- id: pyrefly
name: pyrefly
entry: scripts/precommit_uv_run.sh pyrefly check src tests
language: system
pass_filenames: falseExpected outcome:
- Linting and formatting run via local Ruff hooks.
- Type checking in pre-commit runs with pyrefly.
Update the CI type-check step in .github/workflows/test.yml from mypy to pyrefly.
Status in this repository:
- This step is already applied.
Note:
- The command below is for CI only.
- For local monorepo use, always run the
--activevariant shown in other steps.
Recommended command:
uv run pyrefly check src testsExpected outcome:
- CI type-check job runs pyrefly only.
After pyrefly-only CI has passed on main:
- Remove mypy from dependency-groups.dev in pyproject.toml.
- Remove tool.mypy from pyproject.toml.
- Remove any remaining mypy references from repo docs and configs.
Expected outcome:
- Single checker policy with pyrefly.
Update references in:
- AGENTS.md
- CONTRIBUTING.md
- README.md (if applicable)
Replace mypy commands with pyrefly equivalents.
Run the following after migration:
uv sync --group dev --active --inexact
uv run --active pyrefly check src tests
uv run --active ruff check .
uv run --active ruff format --check .
uv run --active pytestStandalone alternative:
uv sync --group dev
uv run pyrefly check src tests
uv run ruff check .
uv run ruff format --check .
uv run pytestAll commands should pass before finalizing the migration.
If pyrefly introduces unexpected blocking behavior:
- Keep tool.pyrefly config but restore mypy in CI and pre-commit temporarily.
- Fix pyrefly issues incrementally.
- Re-run dual-run for a short window.
- Cut over again once stable.
This keeps type safety intact while avoiding disruption.