Skip to content

Commit 4f4d2e0

Browse files
maifeeulasadromanlutzCopilot
authored
MAINT Migration from mypy to ty (#1319)
Co-authored-by: Roman Lutz <romanlutz13@gmail.com> Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 40ec403 commit 4f4d2e0

210 files changed

Lines changed: 548 additions & 626 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.devcontainer/devcontainer.json

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,6 @@
1616
"python.analysis.extraPaths": [
1717
"/workspace"
1818
],
19-
"python.linting.mypyArgs": [
20-
"--cache-dir=.mypy_cache"
21-
],
2219
"python.analysis.typeCheckingMode": "basic",
2320
"python.analysis.diagnosticMode": "openFilesOnly",
2421
"python.analysis.autoSearchPaths": false,
@@ -52,7 +49,7 @@
5249
"**/dist/**": true,
5350
"**/pyrit/auxiliary_attacks/gcg/attack/**": true,
5451
"**/doc/**": true,
55-
"**/.mypy_cache/**": true,
52+
"**/.ty_cache/**": true,
5653
"**/frontend/node_modules/**": true,
5754
"**/frontend/dist/**": true,
5855
"**/dbdata/**": true

.devcontainer/devcontainer_setup.sh

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,7 @@
11
#!/bin/bash
22
set -e
33

4-
MYPY_CACHE="/workspace/.mypy_cache"
54
VIRTUAL_ENV="/opt/venv"
6-
# Create the mypy cache directory if it doesn't exist
7-
if [ ! -d "$MYPY_CACHE" ]; then
8-
echo "Creating mypy cache directory..."
9-
sudo mkdir -p $MYPY_CACHE
10-
sudo chown vscode:vscode $MYPY_CACHE
11-
sudo chmod 755 $MYPY_CACHE
12-
else
13-
# Check ownership
14-
OWNER=$(stat -c '%U:%G' $MYPY_CACHE)
15-
16-
if [ "$OWNER" != "vscode:vscode" ]; then
17-
echo "Fixing mypy cache directory ownership..."
18-
sudo chown -R vscode:vscode $MYPY_CACHE
19-
fi
20-
21-
# Check permissions
22-
PERMS=$(stat -c '%a' $MYPY_CACHE)
23-
24-
if [ "$PERMS" != "755" ]; then
25-
echo "Fixing mypy cache directory permissions..."
26-
sudo chmod -R 755 $MYPY_CACHE
27-
fi
28-
fi
295

306
# cleanup old extensions
317
sudo rm -rf /vscode/vscode-server/extensionsCache/github.copilot-*

.devcontainer/docker-compose.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ services:
1313
- pip-cache:/home/vscode/.cache/pip:cached
1414
- uv-cache:/home/vscode/.cache/uv:cached
1515
- precommit-cache:/home/vscode/.cache/pre-commit:cached
16-
- mypy-cache:/workspace/.mypy_cache:cached
16+
- ty-cache:/workspace/.ty_cache:cached
1717
- pylance-cache:/home/vscode/.cache/pylance:cached
1818
- node-modules:/workspace/frontend/node_modules:cached
1919
- ~/.pyrit:/home/vscode/.pyrit:cached
@@ -27,6 +27,6 @@ volumes:
2727
pip-cache:
2828
uv-cache:
2929
precommit-cache:
30-
mypy-cache:
30+
ty-cache:
3131
pylance-cache:
3232
node-modules:

.dockerignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ build/
1414
**/*.py[cod]
1515
**/*$py.class
1616
**/.pytest_cache/
17-
**/.mypy_cache/
17+
**/.ty_cache/
1818

1919
# Environment files with secrets
2020
.env

.gitignore

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -149,11 +149,6 @@ env-test
149149
# mkdocs documentation
150150
/site
151151

152-
# mypy
153-
.mypy_cache/
154-
.dmypy.json
155-
dmypy.json
156-
157152
# Pyre type checker
158153
.pyre/
159154

.pre-commit-config.yaml

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -79,14 +79,11 @@ repos:
7979
additional_dependencies: ['requests']
8080
exclude: (release_process.md|git.md|^doc/deployment/|tests|pyrit/prompt_converter/morse_converter.py|.github|pyrit/prompt_converter/emoji_converter.py|pyrit/score/markdown_injection.py|^pyrit/datasets/|^pyrit/auxiliary_attacks/gcg/)
8181

82-
- repo: https://github.com/pre-commit/mirrors-mypy
83-
rev: v1.15.0
82+
- repo: https://github.com/allganize/ty-pre-commit
83+
rev: v0.0.32
8484
hooks:
85-
- id: mypy
86-
name: mypy (strict)
87-
files: &strict_modules ^pyrit/
85+
- id: ty-check
86+
name: ty (type check)
87+
files: ^pyrit/
8888
exclude: ^pyrit/auxiliary_attacks/
89-
args: [--install-types, --non-interactive, --ignore-missing-imports, --sqlite-cache, --cache-dir=.mypy_cache, --strict]
90-
entry: mypy
91-
language: system
92-
types: [ python ]
89+
types: [python]

Makefile

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
.PHONY: all pre-commit mypy unit-test unit-test-junit unit-test-cov-html unit-test-cov-xml diff-cover unit-test-diff-cover
1+
.PHONY: all pre-commit ty unit-test unit-test-junit unit-test-cov-html unit-test-cov-xml diff-cover unit-test-diff-cover
22

33
CMD:=uv run -m
44
PYMODULE:=pyrit
@@ -14,8 +14,8 @@ pre-commit:
1414
$(CMD) isort --multi-line 3 --recursive $(PYMODULE) $(TESTS)
1515
pre-commit run --all-files
1616

17-
mypy:
18-
$(CMD) mypy $(PYMODULE) $(UNIT_TESTS)
17+
ty:
18+
$(CMD) ty check $(PYMODULE) $(UNIT_TESTS)
1919

2020
# Build the full documentation site:
2121
# 1. Generate API reference JSON from Python source (griffe)

doc/getting_started/install_local_dev.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ uv run ruff check --fix .
172172
#### Running Type Checker
173173

174174
```bash
175-
uv run mypy pyrit/
175+
uv run ty check pyrit/
176176
```
177177

178178
#### Pre-commit Hooks
@@ -188,7 +188,7 @@ If you prefer conda for environment management, you can use it to create a Pytho
188188

189189
### Prerequisites
190190

191-
1. **Conda or Miniconda**: Download from [https://docs.conda.io/en/latest/miniconda.html](https://docs.conda.io/en/latest/miniconda.html)
191+
1. **Conda or Miniconda**: Download from [https://docs.anaconda.com/free/miniconda/](https://docs.anaconda.com/free/miniconda/)
192192

193193
2. **Git**: Clone the repository:
194194
```bash

pyproject.toml

Lines changed: 35 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ dev = [
8080
"jupyter-book>=2.0.0",
8181
"jupytext>=1.17.1",
8282
"matplotlib>=3.10.0",
83-
"mypy>=1.16.0",
83+
"ty>=0.0.32",
8484
"mock-alchemy>=0.2.6",
8585
"pandas>=2.2.0",
8686
"pre-commit>=4.2.0",
@@ -167,17 +167,33 @@ pythonpath = ["."]
167167
asyncio_default_fixture_loop_scope = "function"
168168
asyncio_mode = "auto"
169169

170-
[tool.mypy]
171-
plugins = []
172-
ignore_missing_imports = true
173-
strict = true
174-
follow_imports = "silent"
175-
disable_error_code = ["empty-body"]
170+
[tool.ty]
171+
[tool.ty.rules]
172+
all = "error"
173+
# Suppress errors for missing third-party stubs
174+
unresolved-import = "ignore"
175+
# Tolerate None attribute access
176+
possibly-missing-attribute = "ignore"
177+
# Allow existing type: ignore comments without matching errors
178+
unused-ignore-comment = "warn"
179+
unused-type-ignore-comment = "warn"
180+
# Allow empty function bodies during development
181+
empty-body = "ignore"
182+
183+
[tool.ty.analysis]
184+
respect-type-ignore-comments = true
185+
# Treat colorama as Any — it has no type stubs and uses a dynamic pattern
186+
# (class attrs are int, __init__ converts to str via setattr) that ty can't follow
187+
replace-imports-with-any = ["colorama.**"]
188+
189+
[tool.ty.src]
176190
exclude = ["doc/code/", "pyrit/auxiliary_attacks/"]
177191

178-
[[tool.mypy.overrides]]
179-
module = "pyrit.prompt_target.hugging_face.*"
180-
disallow_untyped_calls = false
192+
# Relax type checking for HuggingFace module
193+
[[tool.ty.overrides]]
194+
include = ["pyrit/prompt_target/hugging_face/**"]
195+
[tool.ty.overrides.rules]
196+
invalid-argument-type = "warn"
181197

182198
[tool.uv]
183199
constraint-dependencies = [
@@ -297,8 +313,10 @@ select = [
297313
"UP", # https://docs.astral.sh/ruff/rules/#pyupgrade-up
298314
"W", # https://docs.astral.sh/ruff/rules/#pycodestyle-w
299315
"YTT", # https://docs.astral.sh/ruff/rules/#flake8-2020-ytt
316+
"ANN", # https://docs.astral.sh/ruff/rules/#flake8-annotations-ann
300317
]
301318
ignore = [
319+
"ANN401", # any-type (too strict — many legitimate uses of Any)
302320
"B903", # class-as-data-structure (test helper classes use @apply_defaults pattern)
303321
"D100", # Missing docstring in public module
304322
"D200", # One-line docstring should fit on one line
@@ -331,12 +349,12 @@ notice-rgx = "Copyright \\(c\\) Microsoft Corporation\\.\\s*\\n.*Licensed under
331349
# Ignore copyright, import-location (E402), line length (E501), type-checking, and blanket type-ignore (PGH003) rules in doc/ directory
332350
# E402: notebooks have imports in code cells, not at top of file
333351
# E501: markdown comments in notebooks can exceed line length
334-
# PGH003: blanket type: ignore in notebooks (not mypy-checked)
352+
# PGH003: blanket type: ignore in notebooks (not type-checked)
335353
# A: notebook code commonly uses builtins like id, dir, display
336354
# ERA001: jupytext markdown cells look like commented-out code
337355
# LOG: notebooks use root logger for convenience
338356
# PLE1142: top-level await is valid in Jupyter notebooks
339-
"doc/**" = ["CPY001", "TCH", "E402", "E501", "PGH003", "A", "ERA001", "LOG", "PLE1142"]
357+
"doc/**" = ["ANN", "CPY001", "TCH", "E402", "E501", "PGH003", "A", "ERA001", "LOG", "PLE1142"]
340358
"pyrit/{auxiliary_attacks,ui}/**/*.py" = ["B905", "D101", "D102", "D103", "D104", "D105", "D106", "D107", "D401", "D404", "D417", "D418", "DOC102", "DOC201", "DOC202", "DOC402", "DOC501", "N", "PERF", "SIM101", "SIM108"]
341359
# Backend API routes raise HTTPException handled by FastAPI, not true exceptions
342360
"pyrit/backend/**/*.py" = ["DOC501", "B008"]
@@ -346,7 +364,11 @@ notice-rgx = "Copyright \\(c\\) Microsoft Corporation\\.\\s*\\n.*Licensed under
346364
"pyrit/ui/**/*.py" = ["D", "DOC201", "DOC501"]
347365
"pyrit/__init__.py" = ["D104"]
348366
# Allow broad pytest.raises(Exception) in tests
349-
"tests/**/*.py" = ["B017"]
367+
"tests/**/*.py" = ["ANN", "B017"]
368+
# Infrastructure/utility scripts — not part of the library API
369+
"build_scripts/**/*.py" = ["ANN"]
370+
"docker/**/*.py" = ["ANN"]
371+
"frontend/dev.py" = ["ANN"]
350372

351373
[tool.ruff.lint.pydocstyle]
352374
convention = "google"

pyrightconfig.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
"**/dist",
1010
".venv",
1111
"pyrit.egg-info",
12-
".mypy_cache",
1312
".pytest_cache"
1413
],
1514
"reportMissingImports": true

0 commit comments

Comments
 (0)