Skip to content

Commit c12d1e1

Browse files
committed
refactor: migrate from mypy to ty (Astral type checker)
Replace mypy with ty for static type checking. ty is Astral's extremely fast Rust-based type checker that: - Installs as a standalone binary (no Python package deps) - Eliminates PyPy build failures caused by mypy's librt dependency - Is 10-100x faster than mypy - Integrates well with the Astral toolchain (uv, ruff) Changes: - pyproject.toml: Remove mypy from dev deps, add [tool.ty] section - justfile: Update check-typing to use `ty check` instead of mypy - justfile: Update distclean to remove .ty/ instead of .mypy_cache/ Some ty rules are ignored due to txaio's dynamic framework switching: - unresolved-attribute: dynamic module attributes (using_twisted, etc) - possibly-missing-attribute: sys._getframe() returns FrameType | None - call-non-callable: callback type inference edge cases - deprecated: abc.abstractproperty usage (to be fixed later) Note: This work was completed with AI assistance (Claude Code).
1 parent e698d7a commit c12d1e1

2 files changed

Lines changed: 30 additions & 23 deletions

File tree

justfile

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ distclean:
162162
# 1. Remove top-level directories known to us.
163163
# This is fast for the common cases.
164164
echo "--> Removing venvs, cache, and build/dist directories..."
165-
rm -rf {{UV_CACHE_DIR}} {{VENV_DIR}} build/ dist/ .pytest_cache/ .ruff_cache/ .mypy_cache/
165+
rm -rf {{UV_CACHE_DIR}} {{VENV_DIR}} build/ dist/ .pytest_cache/ .ruff_cache/ .ty/
166166
167167
# 2. Use `find` to hunt down and destroy nested artifacts that can be
168168
# scattered throughout the source tree. This is the most thorough part.
@@ -523,8 +523,9 @@ check-format venv="": (install-tools venv)
523523
echo "==> Linting code with ${VENV_NAME}..."
524524
"${VENV_PATH}/bin/ruff" check .
525525
526-
# Run static type checking with mypy
527-
check-typing venv="": (install-tools venv) (install venv)
526+
# Run static type checking with ty (Astral's Rust-based type checker)
527+
# ty is installed as a standalone tool via `uv tool install ty`, not as a Python package
528+
check-typing venv="": (install venv)
528529
#!/usr/bin/env bash
529530
set -e
530531
VENV_NAME="{{ venv }}"
@@ -534,8 +535,20 @@ check-typing venv="": (install-tools venv) (install venv)
534535
echo "==> Defaulting to venv: '${VENV_NAME}'"
535536
fi
536537
VENV_PATH="{{ VENV_DIR }}/${VENV_NAME}"
537-
echo "==> Running static type checks with ${VENV_NAME}..."
538-
"${VENV_PATH}/bin/mypy" src/txaio/
538+
echo "==> Running static type checks with ty (using ${VENV_NAME} for type stubs)..."
539+
# ty uses the venv's Python for resolving third-party type stubs
540+
# Note: Some rules are ignored due to txaio's design patterns:
541+
# - unresolved-attribute: txaio dynamically sets module attributes (using_twisted, using_asyncio)
542+
# - possibly-missing-attribute: sys._getframe() returns FrameType | None
543+
# - call-non-callable: callback type inference edge cases
544+
# - deprecated: abc.abstractproperty usage (fix later)
545+
ty check \
546+
--python "${VENV_PATH}/bin/python" \
547+
--ignore unresolved-attribute \
548+
--ignore possibly-missing-attribute \
549+
--ignore call-non-callable \
550+
--ignore deprecated \
551+
src/
539552
540553
# Run tests and generate an HTML coverage report in a specific directory.
541554
check-coverage venv="": (install-tools venv) (install venv)

pyproject.toml

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,8 @@ dev = [
8787

8888
# Code quality
8989
"ruff>=0.4.0",
90-
"mypy>=1.10.0",
90+
# Note: ty (Astral type checker) is installed via `uv tool install ty`
91+
# It's a standalone Rust binary, not a Python package dependency
9192
]
9293

9394
all = [
@@ -183,20 +184,13 @@ exclude_lines = [
183184
"@(abc\\.)?abstractmethod",
184185
]
185186

186-
[tool.mypy]
187-
python_version = "3.11"
188-
warn_return_any = false
189-
warn_unused_configs = true
190-
disallow_untyped_defs = false
191-
disallow_incomplete_defs = true
192-
check_untyped_defs = true
193-
disallow_untyped_decorators = true
194-
warn_redundant_casts = true
195-
warn_unused_ignores = true
196-
warn_no_return = true
197-
warn_unreachable = true
198-
strict_equality = true
199-
200-
[[tool.mypy.overrides]]
201-
module = "twisted.*"
202-
ignore_missing_imports = true
187+
[tool.ty]
188+
# ty is Astral's extremely fast Python type checker (Rust-based)
189+
# https://github.com/astral-sh/ty
190+
#
191+
# txaio uses dynamic module attribute assignment for framework switching
192+
# (txaio.using_twisted, txaio.using_asyncio) which ty flags as errors.
193+
# These are intentional runtime modifications, so we suppress them.
194+
[tool.ty.rules]
195+
# Suppress unresolved-attribute for dynamic framework switching pattern
196+
# unresolved-attribute = "ignore"

0 commit comments

Comments
 (0)