简体   繁体   English

支持__getitem__的类的Python类型提示

[英]Python type hint for classes that support __getitem__

I want to add type hints to a function that will accept any object with a __getitem__ method. 我想将类型提示添加到一个函数,该函数将接受具有__getitem__方法的任何对象。 For instance, in 例如,在

def my_function(hasitems, locator):
    hasitems[locator]

I don't want to restrict hasitems to be a specific type like list or dict . 我不想将hasitems限制为像listdict这样的特定类型。 As long as it supports __getitem__ , it's an appropriate argument to my_function . 只要它支持__getitem__ ,它就是my_function的合适参数。 How can I annotate its type without being unnecessarily restrictive? 如何在不受不必要限制的情况下对其类型进行注释?

Edit: apparently PyCharm can deduce the appropriate hint in a number of common cases, but not in my actual use case. 编辑:显然PyCharm可以在许多常见案例中推断出适当的提示,但不是在我的实际用例中。 I can't post the code since it's for work, and I haven't been able to find a nonproprietary minimal example where PyCharm fails. 我不能发布代码,因为它是为了工作,我无法找到PyCharm失败的非专有的最小例子。 In any case, the original question doesn't reference PyCharm and it is still a valid use case for type hints. 在任何情况下,原始问题都没有引用PyCharm,它仍然是类型提示的有效用例。

This will work for dict and list, but not for any generic type: 这适用于dict和list,但不适用于任何泛型类型:

from typing import Any, Mapping, Sequence, Union

def my_function(hasitems: Union[Mapping, Sequence], locator: Any) -> Any:
    return hasitems[locator]

If you're willing to install a not-quite-offical extension to typing , typing-extensions , you can use a Protocol , which should be an implementation of PEP-0544 : 如果您愿意安装一个非常官方的扩展名来typing输入扩展名 ,您可以使用一个Protocol ,它应该是PEP-0544的一个实现:

from typing_extensions import Protocol
from typing import Any

class GetItem(Protocol):
    def __getitem__(self: 'Getitem', key: Any) -> Any: pass

class BadGetItem:
    def __getitem__(self, a: int, b: int) -> Any: pass

def do_thing(arg: GetItem):
    pass

do_thing(dict())  # OK
do_thing(BadGetItem())  # Fails with explanation of correct signature
do_thing(1)  # Fails

It sounds like you essentially want to define your own abstract base class (abc) . 听起来你基本上想要定义自己的抽象基类(abc)

Following the documentation above, you can define a custom abc that only dictates the presence of __getitem__ , but let's use a predefined one for an example. 按照上面的文档,您可以定义一个仅指示__getitem__存在的自定义abc,但让我们使用预定义的abc作为示例。 The Mapping abc consists of __getitem__ and a few other magic methods. Mapping abc由__getitem__和其他一些魔术方法组成。 You can use abcs in isinstance , but you can also directly use them as a type annotations: 您可以在isinstance使用abcs,但您也可以直接将它们用作类型注释:

def foo(bar: Mapping):
    pass

Or, using the extended type hinting support of ABCs do even fancies things, which you already saw in other answers: 或者,使用ABCs扩展类型提示支持甚至可以说是你在其他答案中已经看到过的东西:

def foo(bar: Mapping[Any, Any]):
    pass

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM