示例工程 10-type-annotations-mypy

10-type-annotations-mypy/02-mypy-catches/README.md

markdown · 0.9 KB

# 02 · mypy-catches
 
**演示**:同一段逻辑在 `mypy --strict` 下的真实输出。`before.py.txt` 缺注解且把 `str` 赋给 `int`;`after.py.txt` 修正后通过。两份捕获由 `capture_mypy.py` 调用本机 mypy 生成。
**运行**:
 
```sh
uv run python examples/10-type-annotations-mypy/02-mypy-catches/mypy_catches.py
```
 
**预期输出**(mypy 2.4.1):
 
```text
before:
before.py:4: error: Function is missing a type annotation  [no-untyped-def]
before.py:12: error: Incompatible types in assignment (expression has type "str", variable has type "int")  [assignment]
Found 2 errors in 1 file (checked 1 source file)
after:
Success: no issues found in 1 source file
```
 
**依赖**:仅标准库运行时;重新生成捕获需要 `mypy`(`uv run python .../capture_mypy.py`)。
**注意**:`before.py.txt` 故意用 `.txt` 后缀,避免被 `verify.py` 的 `mypy --strict` 扫描;真正跑的是临时目录里的副本。