如何子类化字典以支持泛型类型提示?

问题描述 投票:0回答:2

如何对字典进行子类化以使子类支持泛型类型提示?它需要在各方面表现得像字典,并支持键和值的类型提示。 子类将添加访问和操作字典数据的函数。例如,它将有一个

valueat(self, idx:int)
函数,返回给定索引处的字典值。

它不需要

OrderedDict
作为其基类,但字典确实需要有一个可预测的顺序。由于
OrderedDict
维护插入顺序并支持类型提示,因此它似乎是一个合理的起点。 这是我尝试过的:

from collections import OrderedDict

class ApplicationSpecificDict(OrderedDict[str, int]):
    ...

但是,它失败并出现错误:

TypeError: 'type' object is not subscriptable

Python 3.7+ 不支持此功能,还是我遗漏了什么?

python dictionary python-typing mypy
2个回答
14
投票

typing包提供了与collections.abccollections中的非泛型类相对应的泛型类。这些通用类可以用作基类来创建用户定义的通用类,例如自定义通用字典。

collections.abc
中类型对应的泛型类示例:

  • typing.AbstractSet(Sized, Collection[T_co])
  • typing.Container(Generic[T_co])
  • typing.Mapping(Sized, Collection[KT], Generic[VT_co])
  • typing.MutableMapping(Mapping[KT, VT])
  • typing.MutableSequence(Sequence[T])
  • typing.MutableSet(AbstractSet[T])
  • typing.Sequence(Reversible[T_co], Collection[T_co])

collections
中类型对应的泛型类示例:

  • typing.DefaultDict(collections.defaultdict, MutableMapping[KT, VT])
  • typing.OrderedDict(collections.OrderedDict, MutableMapping[KT, VT])
  • typing.ChainMap(collections.ChainMap, MutableMapping[KT, VT])
  • typing.Counter(collections.Counter, Dict[T, int])
  • typing.Deque(deque, MutableSequence[T])

实现自定义通用词典

实现自定义通用字典有很多选项。但是,需要注意的是,除非用户定义的类显式继承自

Mapping
MutableMapping
,否则像 mypy 这样的静态类型检查器不会将该类视为映射。

用户定义的通用词典示例

from collections import abc  # Used for isinstance check in `update()`.
from typing import Dict, Iterator, MutableMapping, TypeVar

KT = TypeVar('KT')
VT = TypeVar('VT')


class MyDict(MutableMapping[KT, VT]):

    def __init__(self, dictionary=None, /, **kwargs) -> None:
        self.data: Dict[KT, VT] = {}
        if dictionary is not None:
            self.update(dictionary)
        if kwargs:
            self.update(kwargs)
    
    def __contains__(self, key: KT) -> bool:
        return key in self.data

    def __delitem__(self, key: KT) -> None:
        del self.data[key]

    def __getitem__(self, key: KT) -> VT:
        if key in self.data:
            return self.data[key]
        raise KeyError(key)

    def __len__(self) -> int:
        return len(self.data)

    def __iter__(self) -> Iterator[KT]:
        return iter(self.data)

    def __setitem__(self, key: KT, value: VT) -> None:
        self.data[key] = value
    
    @classmethod
    def fromkeys(cls, iterable: Iterable[KT], value: VT) -> "MyDict":
        """Create a new dictionary with keys from `iterable` and values set 
        to `value`.

        Args:
            iterable: A collection of keys.
            value: The default value. All of the values refer to just a single 
                instance, so it generally does not make sense for `value` to be a 
                mutable object such as an empty list. To get distinct values, use 
                a dict comprehension instead.

        Returns:
            A new instance of MyDict.
        """
        d = cls()
        for key in iterable:
            d[key] = value
        return d

    def update(self, other=(), /, **kwds) -> None:
        """Updates the dictionary from an iterable or mapping object."""
        if isinstance(other, abc.Mapping):
            for key in other:
                self.data[key] = other[key]
        elif hasattr(other, "keys"):
            for key in other.keys():
                self.data[key] = other[key]
        else:
            for key, value in other:
                self.data[key] = value
        for key, value in kwds.items():
            self.data[key] = value


8
投票

我发布了this问题,你的问题可能是一个骗局,但我也会将其包含在这里,因为我在谷歌搜索如何做到这一点时发现了这两个问题。

基本上,您需要使用打字映射通用 这是 dict 使用的通用注释,因此您可以定义其他类型,例如

MyDict[str, int]

如何:

import typing
from collections import OrderedDict

# these are generic type vars to tell mutable-mapping 
# to accept any type vars when creating a sub-type of your generic dict
_KT = typing.TypeVar("_KT") #  key type
_VT = typing.TypeVar("_VT") #  value type


# `typing.MutableMapping` requires you to implement certain functions like __getitem__
# You can get around this by just subclassing OrderedDict first.
# Note: The generic you're subclassing needs to come BEFORE
# the `typing.MutableMapping` subclass or accessing indices won't work.

class ApplicationSpecificDict(
        OrderedDict, 
        typing.MutableMapping[_KT, _VT]
):
    """Your special dict"""
    ...

# Now define the key, value types for sub-types of your dict
RequestDict = ApplicationSpecificDict[str, typing.Tuple[str, str]]
ModelDict = ApplicationSpecificDict[str, typing.Any]

现在使用您的子类型字典的自定义类型:

from my_project.custom_typing import ApplicationSpecificDict #  Import your custom type

def make_request() -> ApplicationSpecificDict:
    request = ApplicationSpecificDict()
    request["test"] = ("sierra", "117")
    return request

print(make_request())

将输出为

{ "test": ("sierra", "117") }

© www.soinside.com 2019 - 2024. All rights reserved.