Skip to content

chore: migrate from mypy to ty#117

Merged
hofbi merged 18 commits into
hofbi:masterfrom
cbachhuber:feat/migrate-from-mypy-to-ty
Apr 28, 2026
Merged

chore: migrate from mypy to ty#117
hofbi merged 18 commits into
hofbi:masterfrom
cbachhuber:feat/migrate-from-mypy-to-ty

Conversation

@cbachhuber
Copy link
Copy Markdown
Collaborator

Closes #115

Comment thread .pre-commit-config.yaml
entry: uvx ty check
language: system
types:
- python
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In my local experiments, this works just fine without pass_filenames: false. So, in the name of locality, I'd leave that out.

Comment thread .pre-commit-config.yaml Outdated
hooks:
- id: ty-check
name: ty-check
entry: uvx ty check
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@hofbi, you told me that you solved this not via uvx, but via additional_dependencies and calling ty check directly. My LLM told me that that's not great because ty will then run in prek's venv and not see the project's venv, so you'd need to call ty check --python .venv. However, folder .venv is created by uv, so you might as well directly use uvx ty check. That's what I'm opting for here. Am I missing something?

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesn't seem to work, see my response there.

Generally, this relates to astral-sh/ty#269, and we're discussing the two options also proposed in that issue:

  1. Call ty via uvx
  2. Call ty directly, point it to .venv and pull it via additional_dependencies

The second proposal has more likes in that thread, but not sure if that's an indicator of quality. Either implementation will make using ty-check in pre-commit.ci impossible: either because of lack of uv, or lack of the .venv folder. Are you ok with skipping ty in CI? Or should we drop pre-commit.ci in favor of a GitHub action?

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can skip in pre-commit.ci if it causes issues. I already created

prek:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6
- name: Install uv and set the Python version
uses: astral-sh/setup-uv@08807647e7069bb48b6ef5acd8ec9567f424441b # v8.1.0
with:
python-version: "3.14"
- run: SKIP=no-commit-to-branch uvx prek run --all-files --show-diff-on-failure
. pre-commit.ci is still useful for the rest because of their autofixes.

When calling ty directly (ty being installed via additional deps), we can set

Copy link
Copy Markdown
Collaborator Author

@cbachhuber cbachhuber Apr 27, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, then we can skip ty-check in pre-commit ci. Unfortunately, we'll lose the nice autofixes from ty like that. I guess you don't want to reimplement the pre-commit.ci autofix pushes via something like I'm doing in a private gitlab project?

if [ "${CI_PIPELINE_SOURCE}" = "merge_request_event" ] && ! git diff --quiet; then
  git config user.name "${GIT_COMMITTER_NAME:-GitLab CI}"
  git config user.email "${GIT_COMMITTER_EMAIL:-ci@example.com}"
  git add --update
  git commit -m "chore: apply pre-commit autofixes"
  git push "https://oauth2:${CI_PUSH_TOKEN}@${CI_SERVER_HOST}/${CI_PROJECT_PATH}.git" "HEAD:${CI_COMMIT_REF_NAME}"
fi

error-on-warnings is already enabled via pyproject.toml.

I just tried --project . as arg for ty check: that fixes local imports, but gives the same errors reported here for 3rd-party imports.

The proposal using additional_dependencies plus --python=.venv works just fine.

What shall it be?

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like the --python=.venv approach. Let's do that.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done, now you only need to skip ty-check in pre-commit.ci.

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can do this with

skip:
- markdownlint # TODO: try to enable again

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops, pre-commit.ci noob here. Done!

Comment thread pyproject.toml
Comment on lines -44 to -60
[tool.mypy]
check_untyped_defs = true
disallow_incomplete_defs = true
disallow_untyped_calls = true
disallow_untyped_defs = true
ignore_missing_imports = true
no_implicit_optional = true
show_column_numbers = true
show_error_codes = true
show_error_context = true
strict_equality = true
strict_optional = true
warn_no_return = true
warn_redundant_casts = true
warn_return_any = true
warn_unused_configs = true
warn_unused_ignores = true
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These are all on in ty per default

Comment thread .pre-commit-config.yaml Outdated
- id: ty-check
name: ty-check
entry: uvx ty check
language: system
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
language: system
language: python

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesn't work, unfortunately. I'm getting many errors like

  error[unresolved-import]: Cannot resolve imported module `pre_commit.constants`
    --> dev_tools/check_useless_exclude_paths_hooks.py:13:6
     |
  13 | from pre_commit.constants import CONFIG_FILE
     |      ^^^^^^^^^^^^^^^^^^^^
     |
  info: Searched in the following paths during module resolution:
  info:   1. /home/chris/dev-tools (first-party code)
  info:   2. vendored://stdlib (stdlib typeshed stubs vendored by ty)
  info:   3. /home/chris/.cache/prek/hooks/python-pbR08CWFH1YCCY8xTzLd/lib/python3.13/site-packages (site-packages)
  info:   4. /home/chris/.cache/prek/hooks/python-pbR08CWFH1YCCY8xTzLd/lib64/python3.13/site-packages (site-packages)
  info: make sure your Python environment is properly configured: https://docs.astral.sh/ty/modules/#python-environment

With language: python, prek creates an isolated venv for ty, which hides other dependencies.

@cbachhuber cbachhuber force-pushed the feat/migrate-from-mypy-to-ty branch from 9673661 to f169215 Compare April 27, 2026 10:42
@cbachhuber cbachhuber marked this pull request as ready for review April 27, 2026 10:43
Comment thread pyproject.toml
"pytest-cov",
"pyfakefs"
"pyfakefs",
"types-regex"
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In contrast to mypy, ty can't work with this type, see this run. So, we need to install the stubs

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you think this would work when adding to the additional dependencies as we do for ty itself? Iguess not since we point ty to our venv, but just wanted to double check.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesn't work. Probably because we point ty to uv's .venv, so it doesn't see modules added to prek's venv via additional_dependencies.

Comment thread .pre-commit-config.yaml Outdated
@hofbi hofbi merged commit 53243ef into hofbi:master Apr 28, 2026
12 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Migrate from mypy to ty

2 participants