-
I was trying to create subclasses of
MCVE: from __future__ import annotations
from abc import ABC
from collections.abc import ItemsView, Set
from typing import Any, Generic, Iterable, Iterator, Tuple, TypeVar
T = TypeVar("T")
KT = TypeVar("KT")
VT = TypeVar("VT")
class EmptySet(Set[T], Generic[T]):
def __and__(self: EmptySet[T], other: Iterable[Any], /) -> EmptySet[T]:
return self
def __contains__(self, value: Any) -> bool:
return False
def __iter__(self) -> Iterator[T]:
x: list[T] = []
return iter(x)
def __len__(self) -> int:
return 0
x: Set[Tuple[int, int]] = EmptySet[Tuple[int, int]]()
class EmptyItemsView(ItemsView[KT, VT], EmptySet[Tuple[KT, VT]], ABC, Generic[KT, VT]):
def __and__(self: EmptyItemsView[KT, VT], other: Iterable[Any], /) -> EmptySet[Tuple[KT, VT]]:
... I assume this is just some strange behavior with nested type variables. How would I go about fixing this? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
The problem is that I'll also note that the |
Beta Was this translation helpful? Give feedback.
The problem is that
EmptySet
derives fromcollections.abc.Set[T]
rather thantyping.Set[T]
orset[T]
. The typeshed stubs definecollections.abc.Set
differently from the builtinsset
. This is probably a bug in the stubs. I think these should be the same.I'll also note that the
__and__
method in the base classItemsView
has another
parameter that can be specified as a keyword, so pyright flags your use of a position-only parameter separator (/
) as invalid when overriding this method. I don't think mypy performs this check.