Typing: Correctly set types when getting `Record` fields
维护者通常 1 天内回复
还没有人认领这个 Issue。
评估
调研方向
从 conn.fetch(..., record_class=Row) 示例以及 issue 中展示的 asyncpg.Record 字段访问开始。研究声明的 Row.num 注解可能会如何影响 row["num"] 的推断类型。完成标准是示例中的 num 被推断为 int,并且不再产生 mypy 的 Any 返回错误。
由索引模型根据 Issue 内容生成。
描述
Consider the following:
import asyncio
import asyncpg
async def main() -> int:
connection_string = "postgresql://postgres:[email protected]:5432/postgres"
conn = await asyncpg.connect(connection_string)
result = await conn.fetch("SELECT 1 + 1 AS num", record_class=Row)
row = result[0]
num = row["num"]
print(type(num))
return num # mypy error: Returning Any from function declared to return "int"
class Row(asyncpg.Record):
num: int
if __name__ == "__main__":
asyncio.run(main())
In essence, the type of num is Any instead of int. I tried to make Row inherit from TypedDict or use a dataclass, both failed.
I'll also be happy to help develop this feature with some general guidance.
- 主要语言
- Python
- 星标
- 8.1k
- 派生
- 474
- 平均合并
- 3 天 14 小时
- 30 天内合并 PR
- 17
环境准备
这个项目没有提供开发容器、Dockerfile 或贡献指南,环境需要你自己搭建:先看它的 README,通用步骤见我们的新手贡献指南。
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
MagicStack/asyncpg 的其他 Issue
-
难度 3/5 1-2 天 新手友好度 72/100
MagicStack/asyncpg#1342 ·
维护者通常 1 天内回复
-
难度 3/5 1-2 天 新手友好度 56/100
MagicStack/asyncpg#1340 · 1 条评论 ·
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 28/100
MagicStack/asyncpg#1337 ·
维护者通常 1 天内回复
-
难度 4/5 3-5 天 新手友好度 42/100
MagicStack/asyncpg#1330 · 1 条评论 ·
维护者通常 1 天内回复
-
难度 5/5 一周以上 新手友好度 38/100
MagicStack/asyncpg#1322 ·
维护者通常 1 天内回复
查看 MagicStack/asyncpg 的全部 Issue
相似的 Issue
-
bug frontend
难度 2/5 1-3 小时 新手友好度 85/100
PedestrianDynamics/pyFDS-Evac#552 ·
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 84/100
resend/resend-skills#144 ·
维护者通常 1 天内回复
-
good first issue
难度 1/5 1 小时以内 新手友好度 68/100
-
Bug
难度 2/5 1-3 小时 新手友好度 72/100
GNS3/gns3-server#2935 · 1 条评论 ·
维护者通常 1 天内回复
-
bug
难度 2/5 1-3 小时 新手友好度 82/100
TauricResearch/TradingAgents#1476 ·
维护者通常 2 天内回复