-
Since pyright was made stricter in a recent release I have encountered import collections.abc
import itertools
import typing as t
K = t.TypeVar("K")
V = t.TypeVar("V")
class FrozenMap(t.Generic[K, V], collections.abc.Mapping[K, V]):
"""An immutable dict implementation"""
__slots__ = ("_dict", "_hash")
_dict: dict[K, V]
_hash: int | None
@t.overload
def __init__(self) -> None: ...
@t.overload
def __init__(self, mapping: collections.abc.Mapping[K, V]) -> None: ...
@t.overload
def __init__(self, it: collections.abc.Iterable[tuple[K, V]]) -> None: ...
@t.overload
def __init__(self: "FrozenMap[str, V]", **kwargs: V) -> None: ...
def __init__(self, *args, **kwargs) -> None: # type: ignore
self._dict = dict(*args, **kwargs)
self._hash = None
...
I'm using strict mode. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
The typing spec was recently updated to clarify that class-scoped type variables cannot be used in the You can use method-scoped type variables in this case, so that's the approach I'd recommend. Create a new type variable (say, @t.overload
def __init__(self: "FrozenMap[str, T]", **kwargs: T) -> None: ... |
Beta Was this translation helpful? Give feedback.
The typing spec was recently updated to clarify that class-scoped type variables cannot be used in the
self
parameter of an__init__
method, so pyright's error is correct here.You can use method-scoped type variables in this case, so that's the approach I'd recommend. Create a new type variable (say,
T
), and change your last overload signature to be: