Skip to content

mypy -> ty - #365

Merged
tony merged 6 commits into
masterfrom
ty
Nov 30, 2026
Merged

tony merged 6 commits into
masterfrom
ty

Conversation

@tony

@tony tony commented Nov 29, 2026 •

Copy link
Copy Markdown
Member

Closes #364.

The CLI, tests, root pytest fixture and Sphinx extension now run through ty 0.0.113. Both the Python 3.11 minimum and the Python 3.15 CI target detected an invalid assignment in each of the 13 tracked files. Trunk used strict mypy, with five direct stub packages, no plugins and an exception for untyped imports from cihai. Removing that exception from the historical configuration exposed six import diagnostics in four files. The new configuration retains the stubs and adds a release cooldown exception for ty. Migration issue · Former checker configuration · Checker and CI changes · ty release

Typing changes

  • The info and reverse commands now enumerate mapped ORM attributes through SQLAlchemy’s public API. SQLAlchemy is an explicit runtime dependency. With the same bundled Unihan data and SQLite database schema, trunk and the candidate produced identical output for single and multiple matches, missing characters and search terms, and the default and all-fields modes. CLI commit
  • The terminal color decision converts the stream’s TTY result to a boolean. The 24 combinations of color mode, TTY state and the two environment overrides matched trunk. Color handling
  • The installation directives declare the optional mapping Docutils accepts and the string value their converter returns. A Sphinx build exercised both directives with default, full and compact variants and produced an error for each unsupported value. Directive annotations

Published cihai 0.38.0 requires the SQLAlchemy mypy extra. That extra still installs mypy and types-greenlet transitively when installing this CLI. The upstream change removing that extra has merged. CLI installations will continue to resolve mypy until a compatible cihai release includes it. Upstream dependency change · CLI release note · CLI checks · Published dependency · Resolved dependency edge · SQLAlchemy extra

Verification

All 28 tests passed with coverage on CPython 3.11.15 and 3.15.0. Python 3.15 reported SQLite connection cleanup warnings. The Sphinx HTML build, Ruff lint and format checks, actionlint and lock validation passed. On the bundled Unihan data, the reverse search used for the output comparison returned six rows. Fixture

Type-checker benchmark

The mypy baseline uses the project’s pre-migration settings and dependency lock. Both tools checked the same 13 candidate files with Python 3.11.15. Both targeted Python 3.11. The two source snapshots differ in 3 of those files. Mypy settings · Ty settings · Candidate lock · Baseline lock

Cache state mypy 2.4.1 ty 0.0.113 mypy / ty
Empty checker cache 5262.4 ± 530.2 ms 105.0 ± 8.9 ms 50.11×
Repeat after two warmups 154.0 ± 3.1 ms 112.2 ± 7.4 ms 1.37×

The table gives mean wall time and sample standard deviation across 8 fresh CLI processes per cell. mypy 2.4.1 exited 0 (16/16). ty 0.0.113 exited 0 (16/16). hyperfine 1.20.0 timed the installed checker executables on Linux x86_64. Stub packages in the shared environment include types-colorama 0.4.15.20260508, types-docutils 0.23.0.20261122, types-greenlet 3.5.0.20260518, types-pygments 2.21.0.20261122, types-pyyaml 6.0.12.20261122, types-requests 2.33.0.20261030. Mypy used no configured plugins. Mypy searched src from the measured source root. Explicit package bases were enabled. The historical config enabled ignore_missing_imports for cihai.*. The project directly listed five stub packages. Its locked cihai dependency also brought in mypy and types-greenlet through sqlalchemy[mypy].

Cold runs cleared the checker cache before each sample. Repeat runs followed two untimed warmups. Mypy reused its module cache. Ty wrote no files under its isolated cache root, so its repeat timings measure another fresh process. The OS page cache stayed warm. Setup and diagnostics ran outside the timed commands.

Diagnostics

Check Exit Errors Warnings
mypy-trunk 0 0 0
mypy-empty 0 0 0
mypy-repeat 0 0 0
ty-empty 0 0 0
ty-repeat 0 0 0

mypy-trunk reported Success: no issues found in 13 source files.

Samples

Checker State Exit codes, in order Wall times (ms)
mypy cold 0, 0, 0, 0, 0, 0, 0, 0 5597.07, 4402.61, 5439.96, 4719.98, 5542.78, 6051.61, 5363.40, 4981.68
ty cold 0, 0, 0, 0, 0, 0, 0, 0 102.41, 118.33, 116.30, 110.96, 99.04, 99.78, 98.39, 94.96
mypy warm 0, 0, 0, 0, 0, 0, 0, 0 153.43, 152.60, 152.49, 150.07, 156.06, 152.32, 160.39, 154.63
ty warm 0, 0, 0, 0, 0, 0, 0, 0 124.59, 119.32, 114.03, 108.05, 103.15, 109.43, 114.64, 104.36

File manifest

  • conftest.py
  • docs/_ext/cihai_install.py
  • docs/conf.py
  • src/cihai_cli/__about__.py
  • src/cihai_cli/__init__.py
  • src/cihai_cli/_colors.py
  • src/cihai_cli/_formatter.py
  • src/cihai_cli/cli.py
  • tests/__init__.py
  • tests/conftest.py
  • tests/test_cli.py
  • tests/test_docs_examples.py
  • tests/test_install_widget.py

Reproduce

Run from a clone containing both revisions with bash, uv 0.13.10, hyperfine 1.20.0 and the utilities used below.

$ bash <<'SH'
set -euo pipefail
base=a192d21511a176b41ae1788a59d3eaef88ee288b
candidate=fde0dfd248a438e7045d040e652ac65ed2bb855d
work=$(mktemp -d)
results=$(mktemp -d)
trap 'rm -rf "$work"' EXIT
for side in base candidate; do
  mkdir "$work/$side"
  git archive -o "$work/source.tar" "${!side}"
  tar -xf "$work/source.tar" -C "$work/$side"
done
files=(conftest.py docs/_ext/cihai_install.py docs/conf.py src/cihai_cli/__about__.py src/cihai_cli/__init__.py src/cihai_cli/_colors.py src/cihai_cli/_formatter.py src/cihai_cli/cli.py tests/__init__.py tests/conftest.py tests/test_cli.py tests/test_docs_examples.py tests/test_install_widget.py)
old_files=("${files[@]}")
unset VIRTUAL_ENV PYTHONPATH PYTHONHOME UV_PROJECT UV_WORKING_DIRECTORY
for key in $(compgen -e | awk '/^(MYPY|TY_)/'); do unset "$key"; done
export UV_PROJECT_ENVIRONMENT="$work/venv"
opts=(--frozen --dev --all-extras --python 3.11.15)
uv sync "${opts[@]}" --project "$work/base"
export VIRTUAL_ENV="$work/venv" PATH="$work/venv/bin:$PATH" NO_COLOR=1
export XDG_CACHE_HOME="$work/xdg-cache" XDG_CONFIG_HOME="$work/xdg-config"
status=0
(cd "$work/base" && mypy --python-executable "$work/venv/bin/python" --config-file pyproject.toml --cache-dir "$work/trunk-cache" -- "${old_files[@]}") > "$results/mypy-trunk.txt" 2>&1 || status=1
project=$("$work/venv/bin/python" -c 'import sys,tomllib; print(tomllib.load(open(sys.argv[1], "rb"))["project"]["name"])' "$work/base/pyproject.toml")
uv sync "${opts[@]}" --project "$work/candidate" --inexact --reinstall-package "$project"
uv sync "${opts[@]}" --project "$work/base" --inexact --no-install-project
uv pip check --python "$work/venv/bin/python"
cd "$work/candidate"
cp "$work/base/pyproject.toml" .bench-trunk-mypy.toml
mypy=("$work"/venv/bin/mypy --python-executable "$work"/venv/bin/python --config-file "$work"/candidate/.bench-trunk-mypy.toml --cache-dir "$work"/mypy-cache --)
ty=("$work"/venv/bin/ty check --python "$work"/venv/bin/python --python-version 3.11 --)
for mode in diagnostics cold warm; do
  for tool in mypy ty; do
    declare -n cmd=$tool
    cache="$work/mypy-cache"
    [[ $tool == mypy ]] || cache="$XDG_CACHE_HOME"
    if [[ $mode == diagnostics ]]; then
      rm -rf "$cache"
      for state in empty repeat; do
        diag=("${cmd[@]}")
        if [[ $tool == ty ]]; then
          diag=("${cmd[@]:0:${#cmd[@]}-1}" --output-format concise --)
        fi
        "${diag[@]}" "${files[@]}" > "$results/$tool-$state.txt" 2>&1 || status=1
      done
      continue
    fi
    controls=()
    if [[ $mode == cold ]]; then
      printf -v clear '%q ' rm -rf "$cache"
      controls=(--prepare "$clear")
    else
      for pass in 1 2; do "${cmd[@]}" "${files[@]}" >/dev/null 2>&1 || status=1; done
    fi
    printf -v command '%q ' "${cmd[@]}" "${files[@]}"
    json="$results/$tool-$mode.json"
    hyperfine --shell=none \
      --runs 8 \
      --ignore-failure \
      --command-name "$tool-$mode" \
      --export-json "$json" \
      "${controls[@]}" "$command"
    jq -e '.results[].exit_codes | length == 8 and all(. == 0)' "$json" || status=1
  done
done
printf 'Raw evidence: %s\n' "$results"
exit "$status"
SH

The Python typing policy describes how to check a proposed signature against downstream callers and when an open input type is appropriate.

Hosted checks

Checks on 5d7ab107c7c2

Passed — codecov/project · CodeQL · Analyze (actions) · Analyze (python) · build (3.15).

Skipped — release · build · release.

tony added 4 commits November 29, 2026 09:05
why: The CLI, Sphinx directives, fixtures, and tests need the same
typing checks in development and CI.

what:
- Pin ty in the dev and lint groups and exempt it from the cooldown.
- Check the minimum and matrix Python targets, and require parameter
  and return annotations with Ruff.
- Use the project checker in the editor and native watch command.
why: Docutils allows directive classes to clear their option mapping.
The install and usage widgets use string validators for their variants.

what:
- Allow None in each option spec and declare the converter's string
  return type to match directives.choice.
why: Both lookups receive reflected ORM instances. Their mapped attributes provide the public field names used by the YAML output.
what:
- Iterate column attributes on the instance mapper in info and reverse.
- Keep the human-readable field filter and empty-value handling.
- Declare SQLAlchemy as a direct runtime dependency for the mapper API.
why: Color selection exposes a boolean even when the stream implementation returns a truth-valued integer from isatty.
what:
- Convert the terminal probe to bool after applying explicit color settings.
@codecov

codecov Bot commented Nov 29, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 89.47%. Comparing base (a192d21) to head (5d7ab10).

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #365      +/-   ##
==========================================
+ Coverage   89.42%   89.47%   +0.04%     
==========================================
  Files          12       12              
  Lines         700      703       +3     
==========================================
+ Hits          626      629       +3     
  Misses         74       74              

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

tony added a commit that referenced this pull request Nov 29, 2026
why: Contributors need the supported checker and its scope in the release notes.
what:
- Summarize the migration and link its pull request.
@tony tony changed the title Check the CLI, tests and Sphinx extension with ty mypy -> ty Nov 29, 2026
tony added 2 commits November 29, 2026 13:11
why: Contributors need a consistent way to resolve typing ambiguities while preserving the contract callers rely on.
what:
- Describe the limited uses of object and Any at input boundaries.
- Check precise signatures against supported callers, misuse and downstream inference.
why: Contributors need the supported checker and its scope in the release notes.
what:
- Summarize the migration and link its pull request.
@tony
tony merged commit bce39ee into master Nov 30, 2026
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

Migrate the CLI type check from mypy to ty

1 participant