[英]Type hinting Callable with no parameters
I want to use type hinting for a function with no parameters我想对没有参数的函数使用类型提示
from typing import Callable
def no_parameters_returns_int() -> int:
return 7
def get_int_returns_int(a: int) -> int:
return a
def call_function(next_method: Callable[[], int]):
print(next_method())
call_function(no_parameters_returns_int) # no indication of error from IDE expected
call_function(get_int_returns_int) # expected an indication of error from IDE
I expected PyCharm to mark the line when I pass a function that does take parameters.当我传递一个带参数的函数时,我希望 PyCharm 标记该行。 Also tried Callabale[[None], int]
and Callabale[[...], int]
.还尝试了Callabale[[None], int]
和Callabale[[...], int]
。 However the first one hinting the passed function to receive a None
type argument, second one hinting the passed function to receive at least one argument.然而,第一个提示传递的函数接收None
类型参数,第二个提示传递的函数接收至少一个参数。
Is it possible to hint that the passed function receives no arguments?是否可以暗示传递的函数不接收参数?
Is it possible to hint that the passed function receives no arguments?是否可以暗示传递的函数不接收参数?
The correct way to type hint a Callable
without arguments is stated in:在没有参数的情况下键入提示Callable
的正确方法如下:
"Fundamental building blocks" , PEP 483 “基本构建块” ,PEP 483
Callable[[t1, t2, ..., tn], tr]
.Callable[[t1, t2, ..., tn], tr]
。 A function with positional argument typest1
etc., and return typetr
.具有位置参数类型t1
等的函数,并返回类型tr
。 The argument list may be empty n==0 .参数列表可能为空 n==0 。
An explicit example is given in:一个明确的例子在:
"Covariance and Contravariance" , PEP 483 “协方差和逆变” ,PEP 483
- Callable[[], int] is a subtype of Callable[[], float]. - Callable[[], Manager] is a subtype of Callable[[], Employee].
And also in:还有:
"Callable" , PEP 484 “可调用” ,PEP 484
from typing import Callable def feeder(get_next_item: Callable[[], str]) -> None: # Body
The built-in name None
should be distinguished from the type None (the first is used to access the second):内置名称None
应该与None类型区分开来(第一个用于访问第二个):
3.2. 3.2. The standard type hierarchy , Data Model 标准类型层次结构,数据模型
None没有任何
- This type has a single value.这种类型只有一个值。 There is a single object with this value.有一个具有此值的对象。 This object is accessed through the built-in name
None
.该对象通过内置名称None
。
The syntax and meaning of the built-in name None
used as a type hint is a special case:用作类型提示的内置名称None
的语法和含义是一种特殊情况:
"Using None" , PEP 484 “不使用” ,PEP 484
When used in a type hint, the expression
None
is considered equivalent totype(None)
.在类型提示中使用时,表达式None
被认为等效于type(None)
。
Considering the above, it's less of a surprise the following two ways -of trying to write a Callable
type hint of a function without arguments- are wrong:考虑到上述情况,以下两种方法 - 尝试编写没有参数的函数的Callable
类型提示 - 是错误的也就不足为奇了:
Callable[[None], tr]
Callable[[type(None)], tr]
The Ellipsis in a Callable
type hint simply means: Callable
类型提示中的省略号仅表示:
"Callable" , PEP 484 “可调用” ,PEP 484
Note that there are no square brackets around the ellipsis.请注意,省略号周围没有方括号。 The arguments of the callback are completely unconstrained in this case (and keyword arguments are acceptable).在这种情况下,回调的参数完全不受约束(并且可以接受关键字参数)。
Since it is "unconstrained" the following is unlikely to cause the static type checker to issue any warnings because of arguments:由于它是“不受约束的”,因此以下不太可能导致静态类型检查器因参数而发出任何警告:
Callable[..., tr]
Worth noting, the relation between Callable
, Any
and ...
(Ellipsis).值得注意的是, Callable
, Any
和...
(省略号)之间的关系。
"The
Any
type" , PEP 484 “Any
类型” ,PEP 484As well, a bare Callable in an annotation is equivalent to Callable[..., Any]同样,注释中的裸 Callable 等效于 Callable[..., Any]
Finally, if you run your code through MyPy the expected warning is in fact issued:最后,如果您通过MyPy运行您的代码,实际上会发出预期的警告:
main.py:13: error: Argument 1 to "call_function" has incompatible type "Callable[[int], int]"; main.py:13: 错误:“call_function”的参数 1 具有不兼容的类型“Callable[[int], int]”; expected "Callable[[], int]" Found 1 error in 1 file (checked 1 source file)预期“Callable[[], int]”在 1 个文件中发现 1 个错误(检查 1 个源文件)
I checked your example in PyCharm 2020.2 Pro and the IDE does not issue the above warning.我在PyCharm 2020.2 Pro 中检查了您的示例,IDE 没有发出上述警告。 Notice that PyCharm uses its own implementation of PEP 484 , and their static type checker has been know to have bugs .请注意, PyCharm 使用其自己的 PEP 484 实现,并且已知它们的静态类型检查器存在错误。
I think you found a bug...我想你发现了一个错误......
Final Note: Running type(None)
gives NoneType
.最后注意:运行type(None)
给出NoneType
。 In Python 3 NoneType
isn't exposed for import although in Python 2 it was importable .在Python 3 中NoneType
没有公开以供导入,尽管在 Python 2 中它是可导入的。
EDIT: For some reason Python 3.10 is reintroducing types.NoneType
.编辑:出于某种原因,Python 3.10 正在 重新引入types.NoneType
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.