Skip to content

Commit b20c759

Browse files
committed
update docs
1 parent 543b08a commit b20c759

File tree

8 files changed

+685
-584
lines changed

8 files changed

+685
-584
lines changed

docs/.vuepress/theme.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,23 @@ export const myTheme: Theme = plumeTheme({
1111
docsRepo: 'https://github.com/fastapi-practices/fastapi_best_architecture_docs',
1212
docsBranch: 'master',
1313
docsDir: 'docs',
14-
blog: {
15-
include: ['blog/**/*.md'],
16-
},
14+
collections: [
15+
{
16+
type: 'post',
17+
dir: 'blog',
18+
title: '博客',
19+
include: ['**/*.md'],
20+
}
21+
],
1722
profile: {
18-
name: 'null',
23+
name: 'wu-clan',
1924
description: 'An open-source enthusiast, creator and contributor',
2025
avatar: 'https://wu-clan.github.io/picx-images-hosting/avatar.gif',
2126
location: '郑州,河南',
2227
organization: 'FastAPI Practices',
2328
circle: true,
2429
layout: 'right',
2530
},
26-
notes: false,
2731
bulletin: myBulletin,
2832
sidebar: mySidebar,
2933
sidebarScrollbar: false,

docs/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ config:
5353
icon: 🐳
5454
details: 提供 Docker compose 一键部署方案
5555
- type: custom
56+
title: .
5657
---
5758

5859
<script setup lang="ts">
File renamed without changes.
File renamed without changes.
File renamed without changes.

docs/blog/typing-cast.md

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
---
2+
title: Typing Cast:静态类型安全的“逃生舱”?
3+
createTime: 2025-9-11 12:30
4+
tags:
5+
- Typing
6+
---
7+
8+
在 Python 3.5 引入类型提示以来,渐进式类型系统让我们的代码更易维护、更易协作。但类型检查器有时太“聪明”了,会因为动态数据或第三方库而迷失方向。
9+
10+
`typing.cast` 就像一个“类型声明器”,帮你明确告诉检查器:“嘿,这个值就是这个类型,别多想了!”
11+
12+
## 什么是 typing.cast
13+
14+
`typing.cast` 是 Python 标准库 `typing` 模块中的一个辅助函数。它的核心作用是**在静态类型检查阶段“强制”指定一个值的类型**
15+
,但在运行时,它什么都不做——只是原封不动地返回输入的值。 这设计非常巧妙:它确保了零运行时开销,同时提升了代码的静态安全性
16+
17+
简单来说:
18+
19+
- **静态时**:类型检查器(如 mypy)会认为返回值就是你指定的类型,从而正确推断后续代码
20+
- **运行时**:Python 继续它的鸭子类型哲学,一切照旧
21+
22+
这不同于真正的类型转换(如 `int("123")`),它更像是一个“类型断言”,专为类型提示生态设计
23+
24+
## 如何使用 typing.cast
25+
26+
使用 `typing.cast` 非常简单。它的签名是:
27+
28+
```python
29+
from typing import cast
30+
31+
result = cast(目标类型, 值)
32+
```
33+
34+
- `目标类型`:可以是任何有效的类型提示,如 `int``List[str]``Optional[Dict[str, int]]`
35+
- ``:你要“转换”的对象,运行时它不会变
36+
37+
让我们通过几个例子来看看它怎么玩转类型提示。
38+
39+
### 示例 1:处理 Any 类型
40+
41+
`Any` 类型是类型提示中的“万金油”,但它会让类型检查器变得宽松。假如你从外部 API 获取数据,知道它其实是 `List[int]`,但检查器只看到
42+
`Any`
43+
44+
```python
45+
from typing import Any, cast, List
46+
47+
def process_scores(data: Any) -> List[int]:
48+
# 假设我们已经验证了 data 是整数列表
49+
scores: List[int] = cast(List[int], data)
50+
return [score * 2 for score in scores] # 现在检查器知道 scores 是 List[int],不会报错
51+
52+
# 使用
53+
raw_data = [1, 2, 3] # 来自 API 的数据
54+
doubled = process_scores(raw_data)
55+
```
56+
57+
没有 `cast`,mypy 可能会抱怨 `scores` 的类型不明,导致后续列表推导式报错
58+
59+
### 示例 2:从 object 窄化类型
60+
61+
有时函数参数是 `object`(Python 的万能基类),但你知道具体类型:
62+
63+
```python
64+
from typing import cast
65+
66+
def get_length(item: object) -> int:
67+
# 假设 item 已被检查为 str
68+
length: int = len(cast(str, item)) # 告诉检查器:item 是 str
69+
return length
70+
71+
# 使用
72+
result = get_length("hello") # 运行正常,检查器也满意
73+
```
74+
75+
### 示例 3:第三方库集成
76+
77+
集成像 `requests` 这样的库时,返回值往往是 `Any`。用 `cast` 可以快速窄化:
78+
79+
```python
80+
import requests
81+
from typing import cast, Dict, Any
82+
83+
response = requests.get("https://api.example.com/data")
84+
data: Dict[str, int] = cast(Dict[str, int], response.json()) # 假设我们知道 JSON 是这个结构
85+
total = sum(data.values()) # 检查器现在知道 data 是 Dict[str, int]
86+
```
87+
88+
这些例子展示了 `cast` 如何在不改动运行逻辑的情况下,提升代码的可读性和工具支持
89+
90+
## 实际应用场景
91+
92+
`typing.cast` 最常出现在这些地方:
93+
94+
- **动态数据处理**:如 JSON 解析、配置文件读取
95+
- **遗留代码迁移**:逐步添加类型提示时,桥接动态和静态部分
96+
- **低级 API**:如 C 扩展或网络协议解析,类型不明显
97+
- **测试与模拟**:mock 对象需要精确类型
98+
99+
在大型项目中,它能减少类型检查器的噪音,让开发者专注于真正的问题
100+
101+
## 注意事项
102+
103+
`cast` 虽然强大,但也有风险:
104+
105+
- **无运行时保护**:它不会验证类型,如果你的假设是错误的(如 `cast(int, "abc")`),运行时会炸锅
106+
- **滥用风险**:过度使用会隐藏真实类型错误,降低代码质量。记住,它是“逃生舱”,不是日常工具
107+
- **最佳实践**:优先用条件检查(如 `isinstance`)或更精确的类型提示。只有当检查器“顽固”时,才祭出 `cast`。另外,从 Python 3.11
108+
开始,还有 `typing.assert_type` 可以辅助验证,但它也只在静态阶段生效

package.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "fastapi_best_architecture_docs",
3-
"version": "1.8.1",
3+
"version": "1.8.2",
44
"description": "fastapi_best_architecture 官方文档",
55
"main": "index.js",
66
"scripts": {
@@ -21,14 +21,14 @@
2121
"@vuepress/plugin-baidu-analytics": "2.0.0-rc.112",
2222
"@vuepress/plugin-llms": "2.0.0-rc.112",
2323
"@vuepress/plugin-umami-analytics": "2.0.0-rc.112",
24-
"vue": "^3.5.21",
24+
"vue": "^3.5.22",
2525
"vuepress": "2.0.0-rc.24",
26-
"vuepress-theme-plume": "1.0.0-rc.164"
26+
"vuepress-theme-plume": "1.0.0-rc.169"
2727
},
2828
"dependencies": {
29-
"@iconify/json": "^2.2.386",
29+
"@iconify/json": "^2.2.394",
3030
"@vueuse/core": "^12.8.2",
3131
"mermaid": "^11.12.0",
32-
"swiper": "^11.2.10"
32+
"swiper": "^12.0.2"
3333
}
3434
}

0 commit comments

Comments
 (0)