You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何为可接受常规类型与联合类型的函数编写正确Type hint?

类型标注疑问

当函数参数的类型标注为type时,代码实际可以正常接收常规类型与联合类型,但Pylance会将传入联合类型的操作标记为错误:

def foo(t: type):
    print(t)

foo(int)
# 正常运行

foo(int | str)
# Pylance提示错误:
# Argument of type "type[int] | type[str]"
# cannot be assigned to parameter "t" of type "type"
# in function "foo"

请问:能接受包含联合类型在内的任意类型的正确类型标注是什么?

更新:
这是Pyright的已知问题(编号#7110),该问题已在新版本中修复。


内容的提问来源于stack exchange,提问作者Maksim Zholudev

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.29 07:17:03