francois
francois

Reputation: 135

Why does mypy error when using OrderedDict with a TypeVar?

i have some problem to put type hinting in my python program. it is from python 3.5.

With this example:

# -*- coding: utf-8 -*-
import collections
import typing

XV = typing.TypeVar('XV')


class Coll(collections.OrderedDict[str, XV]):

    def sorted(self) -> collections.OrderedDict[str, XV]:
        dict_sorted = collections.OrderedDict()  # type: collections.OrderedDict[str,XV]
        for key in sorted(self.keys()):
            dict_sorted[key] = self[key]
        return dict_sorted

    def __str__(self) -> str:
        retour = ""  # type:str
        if len(self) == 0:
            return ""
        test = self.sorted()  # type: collections.OrderedDict[str,XV]
        for l in test:
            if retour:
                retour += "\n{0!s}".format(self[l])
            else:
                retour = "{0!s}".format(self[l])
        return retour

    def __repr__(self) -> str:
        return self.__str__()

when i run mypy, i have the following:

example.py:8: error: Invalid type "example.XV"
example.py: note: In function "__str__":
example.py:20: error: Invalid type "example.XV"

the thing i don't understand is why i have those errors.

Upvotes: 0

Views: 844

Answers (1)

smarie
smarie

Reputation: 5226

OrderedDict is not a subclass of typing.Generic and therefore cannot be parametrized. However you may easily define the corresponding type as follows:

from typing import MutableMapping, TypeVar
from collections import OrderedDict

KT = TypeVar('KT')  # Key type.
VT = TypeVar('VT')  # Value type.

class OrderedDictType(OrderedDict, MutableMapping[KT, VT], extra=OrderedDict):
    __slots__ = ()

    def __new__(cls, *args, **kwds):
        raise TypeError("Type OrderedDictType cannot be instantiated;" +
                        " use OrderedDict() instead")

# isinstance check
a = OrderedDict()
assert isinstance(a, OrderedDictType)

You may then use it in all type hints : OrderedDictType[str,XV] instead of OrderedDict[str,XV].

See https://docs.python.org/3/library/typing.html#user-defined-generic-types and typing.py source for details and examples (I used the typing.List class as an example).

Upvotes: 2

Related Questions