#21998·mypy

可能的错误 - mypy 拒绝在函数中对 `Any` 类型的 `*args` 项进行子类化 (Python 中是可以的; pyright 和 pyrefly 都表示可以),但却允许从关键字参数中进行相同的操作

作者: willy-b创建于 2026年9月16日更新于 2026年9月17日
标签bugtopic-calls

[case testVarargsArgumentTypeCheckingUsingArgumentTypeFromVarArgsForInheritanceExplicitCastOk]

flags: --Python-version 3.12

from typing import Any def fun_explicit_cast_varargs_element_to_type_used_for_inherit(*args: *tuple[int, Any]):

allowed

x: type = args[1] class Sub(x): pass fun_explicit_cast_varargs_element_to_type_used_for_inherit(1, str) [builtins fixtures/tuple.pyi] [case testVarargsArgumentTypeCheckingUsingArgumentTypeFromVarArgsForInheritance-xfail]

flags: --Python-version 3.12

from typing import Any def fun_varargs_element_to_type_used_for_inherit(*args: *tuple[int, Any]):

not allowed by mypy but allowed by pyright and others

x = args[1] class Sub(x): pass fun_varargs_element_to_type_used_for_inherit(1, str) [builtins fixtures/tuple.pyi] Note that passing a type via an Any typed keyword parameter is OK-ed by mypy: https://mypy-play.net/?gist=9ff16e8bc2f873f3fe5d0dcb4ff8baeb [liveuser@localhost-live mypy]$ cat ~/examplecomplex3.py from typing import Any def fun_keyword_parameter_to_type_used_for_inherit(*args: *tuple[int, Any], keywordarg: Any) -> Any: class Sub(keywordarg): pass return Sub() s = fun_keyword_parameter_to_type_used_for_inherit(1, 1, keywordarg=str) …