示例工程 10-type-annotations-mypy

10-type-annotations-mypy/05-reveal-type/README.md

markdown · 0.8 KB

# 05 · reveal-type
 
**演示**:`reveal_type(x)` 让 mypy 打印 `x` 的推断类型(note,不是 error);运行时用 `if TYPE_CHECKING` 跳过,避免 `typing.reveal_type` 的 stderr 输出。
**运行**:
 
```sh
uv run python examples/10-type-annotations-mypy/05-reveal-type/reveal_type.py
```
 
**预期输出**(mypy 2.4.1 + 3.12.9):
 
```text
mypy notes:
  reveal_type.py:16: note: Revealed type is "int"
  reveal_type.py:22: note: Revealed type is "bool"
  reveal_type.py:37: note: Revealed type is "int"
value: 3
maybe: 1
```
 
**重新捕获**:
 
```sh
cd examples/10-type-annotations-mypy/05-reveal-type
uv run mypy --strict reveal_type.py > mypy-output.txt 2>&1
```
 
**依赖**:仅标准库;捕获需要 mypy。
**注意**:行号会随文件改动变化;改完源码后重跑上面的命令即可。