Skip to content

Commit 76c6b76

Browse files
authored
Port type checking from mypy to ty (#294)
1 parent c0e286f commit 76c6b76

10 files changed

Lines changed: 32 additions & 37 deletions

File tree

.github/PULL_REQUEST_TEMPLATE.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,5 @@ See [CONTRIBUTING.md](https://github.com/Azure-Samples/rag-postgres-openai-pytho
3131
- [ ] The current tests all pass (`python -m pytest`).
3232
- [ ] I added tests that prove my fix is effective or that my feature works
3333
- [ ] I ran `python -m pytest --cov` to verify 100% coverage of added lines
34-
- [ ] I ran `python -m mypy` to check for type errors
34+
- [ ] I ran `python -m ty check` to check for type errors
3535
- [ ] I either used the pre-commit hooks or ran `ruff` manually on my code.

.github/copilot-instructions.md

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -98,11 +98,9 @@ Use "Frontend & Backend" configuration in the VS Code Run & Debug menu.
9898
```bash
9999
ruff check . # Lint code (takes <1 second)
100100
ruff format . # Format code (takes <1 second)
101-
mypy . --python-version 3.12 # Type check (takes ~42 seconds)
101+
ty check . --python-version 3.12 # Type check
102102
```
103103

104-
**NOTE**: MyPy may show 1 minor import error in `evals/safety_evaluation.py` which is expected and safe to ignore.
105-
106104
### Testing (NEVER CANCEL - full test suite takes ~25 seconds)
107105
```bash
108106
pytest -s -vv --cov --cov-fail-under=85
@@ -123,7 +121,7 @@ pytest tests/e2e.py --tracing=retain-on-failure
123121
- **Dependencies install**: 90 seconds (use 180+ second timeout)
124122
- **Frontend npm install**: 22 seconds (use 60+ second timeout)
125123
- **Frontend build**: 12 seconds (use 30+ second timeout)
126-
- **MyPy type checking**: 42 seconds (use 90+ second timeout)
124+
- **ty type checking**: use 90+ second timeout
127125
- **Full test suite**: 25 seconds (use 60+ second timeout)
128126
- **Playwright E2E tests**: 2+ minutes (use 300+ second timeout)
129127

@@ -138,7 +136,7 @@ pytest tests/e2e.py --tracing=retain-on-failure
138136

139137
2. **Type check (if Python changes)**:
140138
```bash
141-
mypy . --python-version 3.12
139+
ty check . --python-version 3.12
142140
```
143141

144142
3. **Run relevant tests**:
@@ -194,7 +192,7 @@ pytest tests/e2e.py --tracing=retain-on-failure
194192
- `main.bicep` - Main infrastructure definition
195193

196194
### Configuration Files
197-
- `pyproject.toml` - Python project config (ruff, mypy, pytest)
195+
- `pyproject.toml` - Python project config (ruff, ty, pytest)
198196
- `requirements-dev.txt` - Development dependencies
199197
- `azure.yaml` - Azure Developer CLI configuration
200198
- `.env.sample` - Environment variable template
@@ -248,7 +246,7 @@ The GitHub Actions require:
248246
- Python 3.10+ with specific versions (3.10, 3.11, 3.12)
249247
- PostgreSQL with pgvector extension
250248
- Node.js 18+
251-
- All code passes `ruff check`, `ruff format --check`, and `mypy`
249+
- All code passes `ruff check`, `ruff format --check`, and `ty check`
252250

253251
## Load Testing
254252

.github/workflows/app-tests.yaml

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -114,14 +114,8 @@ jobs:
114114
npm install
115115
npm run build
116116
117-
- name: Setup mypy cache
118-
uses: actions/cache@0057852bfaa89a56745cba8c7296529d2fc39830 # v4.3.0
119-
with:
120-
path: ./.mypy_cache
121-
key: mypy${{ matrix.os }}-${{ matrix.python_version }}-${{ hashFiles('requirements-dev.txt', 'src/backend/requirements.txt', 'src/backend/pyproject.toml') }}
122-
123-
- name: Run MyPy
124-
run: python3 -m mypy . --python-version ${{ matrix.python_version }}
117+
- name: Run ty
118+
run: python3 -m ty check . --python-version ${{ matrix.python_version }} --output-format github
125119

126120
- name: Run Pytest
127121
run: python3 -m pytest -s -vv --cov --cov-fail-under=85

.gitignore

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -128,11 +128,6 @@ venv.bak/
128128
# mkdocs documentation
129129
/site
130130

131-
# mypy
132-
.mypy_cache/
133-
.dmypy.json
134-
dmypy.json
135-
136131
# Pyre type checker
137132
.pyre/
138133

.vscode/settings.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@
3434
".pytest_cache": true,
3535
"__pycache__": true,
3636
"htmlcov": true,
37-
".mypy_cache": true,
3837
".coverage": true
3938
},
4039
"python-envs.defaultEnvManager": "ms-python.python:system"

evals/generate_ground_truth.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ def generate_ground_truth_data(num_questions_total: int, num_questions_per_sourc
121121
{"role": "system", "content": generate_prompt},
122122
{"role": "user", "content": json.dumps(source)},
123123
],
124-
tools=[qa_pairs_tool(num_questions=2)], # type: ignore[list-item]
124+
tools=[qa_pairs_tool(num_questions=2)], # ty: ignore[invalid-argument-type]
125125
max_output_tokens=1000,
126126
store=False,
127127
)

pyproject.toml

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,22 @@ lint.select = ["E", "F", "I", "UP"]
55
lint.ignore = ["D203"]
66
lint.isort.known-first-party = ["fastapi_app"]
77

8-
[tool.mypy]
9-
check_untyped_defs = true
10-
exclude = [".venv/*"]
8+
[tool.ty.environment]
9+
root = ["./src/backend", "."]
10+
11+
[tool.ty.analysis]
12+
allowed-unresolved-imports = [
13+
"azure.ai.evaluation.**",
14+
"rich.**",
15+
"pgvector.**",
16+
"evaltools.**",
17+
]
1118

1219
[tool.pytest.ini_options]
1320
addopts = "-ra"
1421
testpaths = ["tests"]
1522
pythonpath = ['src/backend']
1623
filterwarnings = ["ignore::DeprecationWarning"]
1724

18-
[[tool.mypy.overrides]]
19-
module = [
20-
"pgvector.*",
21-
"evaltools.*"
22-
]
23-
ignore_missing_imports = true
24-
2525
[tool.coverage.report]
2626
show_missing = true

requirements-dev.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
-r src/backend/requirements.txt
22
ruff
3-
mypy
3+
ty
44
types-requests
55
pre-commit
66
pip-tools

src/backend/fastapi_app/rag_advanced.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
ItemHelpers,
88
ModelSettings,
99
OpenAIResponsesModel,
10+
RawResponsesStreamEvent,
1011
Runner,
1112
ToolCallOutputItem,
1213
function_tool,
@@ -198,6 +199,6 @@ async def answer_stream(
198199
)
199200

200201
async for event in run_results.stream_events():
201-
if event.type == "raw_response_event" and isinstance(event.data, ResponseTextDeltaEvent):
202+
if isinstance(event, RawResponsesStreamEvent) and isinstance(event.data, ResponseTextDeltaEvent):
202203
yield RetrievalResponseDelta(type="response.output_text.delta", delta=str(event.data.delta))
203204
return

src/backend/fastapi_app/rag_simple.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,15 @@
11
from collections.abc import AsyncGenerator
22
from typing import Optional
33

4-
from agents import Agent, ItemHelpers, ModelSettings, OpenAIResponsesModel, Runner, set_tracing_disabled
4+
from agents import (
5+
Agent,
6+
ItemHelpers,
7+
ModelSettings,
8+
OpenAIResponsesModel,
9+
RawResponsesStreamEvent,
10+
Runner,
11+
set_tracing_disabled,
12+
)
513
from openai import AsyncOpenAI
614
from openai.types.responses import ResponseInputItemParam, ResponseTextDeltaEvent
715

@@ -131,6 +139,6 @@ async def answer_stream(
131139
)
132140

133141
async for event in run_results.stream_events():
134-
if event.type == "raw_response_event" and isinstance(event.data, ResponseTextDeltaEvent):
142+
if isinstance(event, RawResponsesStreamEvent) and isinstance(event.data, ResponseTextDeltaEvent):
135143
yield RetrievalResponseDelta(type="response.output_text.delta", delta=str(event.data.delta))
136144
return

0 commit comments

Comments
 (0)