From f5f56274a564c53b9e84ff09f51d52520690a5ac Mon Sep 17 00:00:00 2001 From: Gaurav Manek Date: Tue, 20 Dec 2022 03:12:32 -0500 Subject: [PATCH 01/27] Added type annotations to interface. --- frozendict/frozendict.pyi | 58 +++++++++++++++++++++++++++++++++++---- 1 file changed, 53 insertions(+), 5 deletions(-) diff --git a/frozendict/frozendict.pyi b/frozendict/frozendict.pyi index e43b742..8581eab 100644 --- a/frozendict/frozendict.pyi +++ b/frozendict/frozendict.pyi @@ -1,6 +1,54 @@ -try: - from ._frozendict import * -except ImportError: - from .core import * +import collections +from _typeshed import Self +from collections.abc import Iterable, Iterator, Mapping +from typing import Any, Generic, NoReturn, Tuple, TypeVar, Union, overload -FrozenOrderedDict = frozendict +_KT = TypeVar("_KT") +_VT = TypeVar("_VT") + +def frozendict_or( + self: Mapping[_KT, _VT], other: Mapping[_KT, _VT] +) -> "frozendict[_KT, _VT]": ... + +class frozendict(Mapping[_KT, _VT], Generic[_KT, _VT]): + # Fake __init__ to describe what __new__ does: + dict_cls: type[dict[Any, Any]] = ... + @overload + def __init__(self, **kwargs: _VT) -> None: ... + @overload + def __init__(self, mapping: Mapping[_KT, _VT]) -> None: ... + @overload + def __init__(self, iterable: Iterable[tuple[_KT, _VT]]) -> None: ... + + # Magic Methods: + def __hash__(self) -> int: ... + def __repr__(self) -> str: ... + def copy(self: Self) -> Self: ... + def __copy__(self: Self) -> Self: ... + def __deepcopy__(self: Self) -> Self: ... + # Omit __reduce__, its used for Pickle and we don't need the annotation in code. + def set(self, key: _KT, value: _VT) -> frozendict[_KT, _VT]: ... + # This weird type annotation is to support subclassing frozendict. It should + # resolve to `frozendict[_KT, _VT]`. + def setdefault(self: Self, key: _KT, default: _VT) -> Union[Self, frozendict[_KT, _VT]]: ... + def delete(self, key: _KT) -> frozendict[_KT, _VT]: ... + def key(self, index: int) -> _KT: ... + def value(self, index: int) -> _VT: ... + def item(self, index: int) -> tuple[_KT, _VT]: ... + def __or__(self: Mapping[_KT, _VT], other: Mapping[_KT, _VT]) -> frozendict[_KT, _VT]: ... + def __ior__(self: Mapping[_KT, _VT], other: Mapping[_KT, _VT]) -> frozendict[_KT, _VT]: ... + def __reversed__(self) -> Iterator[Tuple[_KT, _VT]]: ... + + # Blacklisted methods: + def __setitem__(self, *a, **kw) -> NoReturn: ... + def __delitem__(self, *a, **kw) -> NoReturn: ... + def __setattr__(self, *a, **kw) -> NoReturn: ... + def __delattr__(self, *a, **kw) -> NoReturn: ... + def clear(self, *a, **kw) -> NoReturn: ... + def pop(self, *a, **kw) -> NoReturn: ... + def popitem(self, *a, **kw) -> NoReturn: ... + def update(self, *a, **kw) -> NoReturn: ... + + +class FrozenOrderedDict(frozendict[_KT, _VT]): + dict_cls: type[collections.OrderedDict[Any, Any]] = ... From 7f9555c5c5568974700abe42076d1072644c6501 Mon Sep 17 00:00:00 2001 From: Gaurav Manek Date: Tue, 20 Dec 2022 03:42:16 -0500 Subject: [PATCH 02/27] Update frozendict.pyi Mypy was complaining, so I added a few more methods. --- frozendict/frozendict.pyi | 3 +++ 1 file changed, 3 insertions(+) diff --git a/frozendict/frozendict.pyi b/frozendict/frozendict.pyi index 8581eab..7073294 100644 --- a/frozendict/frozendict.pyi +++ b/frozendict/frozendict.pyi @@ -21,6 +21,9 @@ class frozendict(Mapping[_KT, _VT], Generic[_KT, _VT]): def __init__(self, iterable: Iterable[tuple[_KT, _VT]]) -> None: ... # Magic Methods: + def __getitem__(self, __key: _KT) -> _VT: ... + def __len__(self) -> int: ... + def __iter__(self) -> Iterator[_KT]: ... def __hash__(self) -> int: ... def __repr__(self) -> str: ... def copy(self: Self) -> Self: ... From bbdd1233e3498858947ce45bf9912c4bf35f7134 Mon Sep 17 00:00:00 2001 From: Marco Sulla Date: Tue, 20 Dec 2022 21:18:06 +0100 Subject: [PATCH 03/27] removed typeshed --- frozendict/frozendict.pyi | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/frozendict/frozendict.pyi b/frozendict/frozendict.pyi index 7073294..cca89b9 100644 --- a/frozendict/frozendict.pyi +++ b/frozendict/frozendict.pyi @@ -1,7 +1,6 @@ import collections -from _typeshed import Self from collections.abc import Iterable, Iterator, Mapping -from typing import Any, Generic, NoReturn, Tuple, TypeVar, Union, overload +from typing import Any, Generic, NoReturn, Tuple, TypeVar, overload _KT = TypeVar("_KT") _VT = TypeVar("_VT") @@ -26,14 +25,12 @@ class frozendict(Mapping[_KT, _VT], Generic[_KT, _VT]): def __iter__(self) -> Iterator[_KT]: ... def __hash__(self) -> int: ... def __repr__(self) -> str: ... - def copy(self: Self) -> Self: ... - def __copy__(self: Self) -> Self: ... - def __deepcopy__(self: Self) -> Self: ... + def copy(self: frozendict[_KT, _VT]) -> frozendict[_KT, _VT]: ... + def __copy__(self: frozendict[_KT, _VT]) -> frozendict[_KT, _VT]: ... + def __deepcopy__(self: frozendict[_KT, _VT]) -> frozendict[_KT, _VT]: ... # Omit __reduce__, its used for Pickle and we don't need the annotation in code. def set(self, key: _KT, value: _VT) -> frozendict[_KT, _VT]: ... - # This weird type annotation is to support subclassing frozendict. It should - # resolve to `frozendict[_KT, _VT]`. - def setdefault(self: Self, key: _KT, default: _VT) -> Union[Self, frozendict[_KT, _VT]]: ... + def setdefault(self: frozendict[_KT, _VT], key: _KT, default: _VT) -> frozendict[_KT, _VT]: ... def delete(self, key: _KT) -> frozendict[_KT, _VT]: ... def key(self, index: int) -> _KT: ... def value(self, index: int) -> _VT: ... From 0a69ed3ee86c6d7347814d213b36e3d8bc293981 Mon Sep 17 00:00:00 2001 From: Marco Sulla Date: Tue, 20 Dec 2022 21:20:28 +0100 Subject: [PATCH 04/27] Lazier TypeVar variable names --- frozendict/frozendict.pyi | 46 +++++++++++++++++++-------------------- 1 file changed, 23 insertions(+), 23 deletions(-) diff --git a/frozendict/frozendict.pyi b/frozendict/frozendict.pyi index cca89b9..ce760cf 100644 --- a/frozendict/frozendict.pyi +++ b/frozendict/frozendict.pyi @@ -2,42 +2,42 @@ import collections from collections.abc import Iterable, Iterator, Mapping from typing import Any, Generic, NoReturn, Tuple, TypeVar, overload -_KT = TypeVar("_KT") -_VT = TypeVar("_VT") +_K = TypeVar("_K") +_V = TypeVar("_V") def frozendict_or( - self: Mapping[_KT, _VT], other: Mapping[_KT, _VT] -) -> "frozendict[_KT, _VT]": ... + self: Mapping[_K, _V], other: Mapping[_K, _V] +) -> "frozendict[_K, _V]": ... -class frozendict(Mapping[_KT, _VT], Generic[_KT, _VT]): +class frozendict(Mapping[_K, _V], Generic[_K, _V]): # Fake __init__ to describe what __new__ does: dict_cls: type[dict[Any, Any]] = ... @overload - def __init__(self, **kwargs: _VT) -> None: ... + def __init__(self, **kwargs: _V) -> None: ... @overload - def __init__(self, mapping: Mapping[_KT, _VT]) -> None: ... + def __init__(self, mapping: Mapping[_K, _V]) -> None: ... @overload - def __init__(self, iterable: Iterable[tuple[_KT, _VT]]) -> None: ... + def __init__(self, iterable: Iterable[tuple[_K, _V]]) -> None: ... # Magic Methods: - def __getitem__(self, __key: _KT) -> _VT: ... + def __getitem__(self, __key: _K) -> _V: ... def __len__(self) -> int: ... - def __iter__(self) -> Iterator[_KT]: ... + def __iter__(self) -> Iterator[_K]: ... def __hash__(self) -> int: ... def __repr__(self) -> str: ... - def copy(self: frozendict[_KT, _VT]) -> frozendict[_KT, _VT]: ... - def __copy__(self: frozendict[_KT, _VT]) -> frozendict[_KT, _VT]: ... - def __deepcopy__(self: frozendict[_KT, _VT]) -> frozendict[_KT, _VT]: ... + def copy(self: frozendict[_K, _V]) -> frozendict[_K, _V]: ... + def __copy__(self: frozendict[_K, _V]) -> frozendict[_K, _V]: ... + def __deepcopy__(self: frozendict[_K, _V]) -> frozendict[_K, _V]: ... # Omit __reduce__, its used for Pickle and we don't need the annotation in code. - def set(self, key: _KT, value: _VT) -> frozendict[_KT, _VT]: ... - def setdefault(self: frozendict[_KT, _VT], key: _KT, default: _VT) -> frozendict[_KT, _VT]: ... - def delete(self, key: _KT) -> frozendict[_KT, _VT]: ... - def key(self, index: int) -> _KT: ... - def value(self, index: int) -> _VT: ... - def item(self, index: int) -> tuple[_KT, _VT]: ... - def __or__(self: Mapping[_KT, _VT], other: Mapping[_KT, _VT]) -> frozendict[_KT, _VT]: ... - def __ior__(self: Mapping[_KT, _VT], other: Mapping[_KT, _VT]) -> frozendict[_KT, _VT]: ... - def __reversed__(self) -> Iterator[Tuple[_KT, _VT]]: ... + def set(self, key: _K, value: _V) -> frozendict[_K, _V]: ... + def setdefault(self: frozendict[_K, _V], key: _K, default: _V) -> frozendict[_K, _V]: ... + def delete(self, key: _K) -> frozendict[_K, _V]: ... + def key(self, index: int) -> _K: ... + def value(self, index: int) -> _V: ... + def item(self, index: int) -> tuple[_K, _V]: ... + def __or__(self: Mapping[_K, _V], other: Mapping[_K, _V]) -> frozendict[_K, _V]: ... + def __ior__(self: Mapping[_K, _V], other: Mapping[_K, _V]) -> frozendict[_K, _V]: ... + def __reversed__(self) -> Iterator[Tuple[_K, _V]]: ... # Blacklisted methods: def __setitem__(self, *a, **kw) -> NoReturn: ... @@ -50,5 +50,5 @@ class frozendict(Mapping[_KT, _VT], Generic[_KT, _VT]): def update(self, *a, **kw) -> NoReturn: ... -class FrozenOrderedDict(frozendict[_KT, _VT]): +class FrozenOrderedDict(frozendict[_K, _V]): dict_cls: type[collections.OrderedDict[Any, Any]] = ... From 620997f26c72c2c29b54b3073cf36858106345a7 Mon Sep 17 00:00:00 2001 From: Marco Sulla Date: Tue, 20 Dec 2022 21:22:35 +0100 Subject: [PATCH 05/27] No more dict_cls And FrozenOrderedDict only a legacy alias --- frozendict/frozendict.pyi | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/frozendict/frozendict.pyi b/frozendict/frozendict.pyi index ce760cf..794d30b 100644 --- a/frozendict/frozendict.pyi +++ b/frozendict/frozendict.pyi @@ -11,7 +11,6 @@ def frozendict_or( class frozendict(Mapping[_K, _V], Generic[_K, _V]): # Fake __init__ to describe what __new__ does: - dict_cls: type[dict[Any, Any]] = ... @overload def __init__(self, **kwargs: _V) -> None: ... @overload @@ -50,5 +49,4 @@ class frozendict(Mapping[_K, _V], Generic[_K, _V]): def update(self, *a, **kw) -> NoReturn: ... -class FrozenOrderedDict(frozendict[_K, _V]): - dict_cls: type[collections.OrderedDict[Any, Any]] = ... +FrozenOrderedDict = frozendict From 836baf0d5580ea4c8893d759ea2fce06343fe10a Mon Sep 17 00:00:00 2001 From: Marco Sulla Date: Tue, 20 Dec 2022 21:27:31 +0100 Subject: [PATCH 06/27] removed blacklisted methods removed blacklisted methods not present in the C Extension --- frozendict/frozendict.pyi | 6 ------ 1 file changed, 6 deletions(-) diff --git a/frozendict/frozendict.pyi b/frozendict/frozendict.pyi index 794d30b..238da76 100644 --- a/frozendict/frozendict.pyi +++ b/frozendict/frozendict.pyi @@ -39,14 +39,8 @@ class frozendict(Mapping[_K, _V], Generic[_K, _V]): def __reversed__(self) -> Iterator[Tuple[_K, _V]]: ... # Blacklisted methods: - def __setitem__(self, *a, **kw) -> NoReturn: ... - def __delitem__(self, *a, **kw) -> NoReturn: ... def __setattr__(self, *a, **kw) -> NoReturn: ... def __delattr__(self, *a, **kw) -> NoReturn: ... - def clear(self, *a, **kw) -> NoReturn: ... - def pop(self, *a, **kw) -> NoReturn: ... - def popitem(self, *a, **kw) -> NoReturn: ... - def update(self, *a, **kw) -> NoReturn: ... FrozenOrderedDict = frozendict From 658e91d575d05f4dd6e8f928c917a7b633417d7c Mon Sep 17 00:00:00 2001 From: Marco Sulla Date: Tue, 20 Dec 2022 22:00:37 +0100 Subject: [PATCH 07/27] changed tuple with generic Sequence --- frozendict/frozendict.pyi | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/frozendict/frozendict.pyi b/frozendict/frozendict.pyi index 238da76..713ccb6 100644 --- a/frozendict/frozendict.pyi +++ b/frozendict/frozendict.pyi @@ -1,9 +1,10 @@ import collections -from collections.abc import Iterable, Iterator, Mapping -from typing import Any, Generic, NoReturn, Tuple, TypeVar, overload +from collections.abc import Iterable, Iterator, Mapping, Sequence +from typing import Any, Generic, NoReturn, TypeVar, overload _K = TypeVar("_K") _V = TypeVar("_V") +_KV = TypeVar("_V", _K, _V) def frozendict_or( self: Mapping[_K, _V], other: Mapping[_K, _V] @@ -16,7 +17,7 @@ class frozendict(Mapping[_K, _V], Generic[_K, _V]): @overload def __init__(self, mapping: Mapping[_K, _V]) -> None: ... @overload - def __init__(self, iterable: Iterable[tuple[_K, _V]]) -> None: ... + def __init__(self, iterable: Iterable[Sequence[_KV]]) -> None: ... # Magic Methods: def __getitem__(self, __key: _K) -> _V: ... @@ -33,10 +34,10 @@ class frozendict(Mapping[_K, _V], Generic[_K, _V]): def delete(self, key: _K) -> frozendict[_K, _V]: ... def key(self, index: int) -> _K: ... def value(self, index: int) -> _V: ... - def item(self, index: int) -> tuple[_K, _V]: ... + def item(self, index: int) -> Sequence[_KV]: ... def __or__(self: Mapping[_K, _V], other: Mapping[_K, _V]) -> frozendict[_K, _V]: ... def __ior__(self: Mapping[_K, _V], other: Mapping[_K, _V]) -> frozendict[_K, _V]: ... - def __reversed__(self) -> Iterator[Tuple[_K, _V]]: ... + def __reversed__(self) -> Iterator[Sequence[_KV]]: ... # Blacklisted methods: def __setattr__(self, *a, **kw) -> NoReturn: ... From d820cf92fd22d27c241c66b3a8e5c33a36dc0845 Mon Sep 17 00:00:00 2001 From: Marco Sulla Date: Tue, 20 Dec 2022 22:05:19 +0100 Subject: [PATCH 08/27] key type must be hashable --- frozendict/frozendict.pyi | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/frozendict/frozendict.pyi b/frozendict/frozendict.pyi index 713ccb6..b2acfb8 100644 --- a/frozendict/frozendict.pyi +++ b/frozendict/frozendict.pyi @@ -1,8 +1,8 @@ import collections -from collections.abc import Iterable, Iterator, Mapping, Sequence +from collections.abc import Iterable, Iterator, Mapping, Sequence, Hashable from typing import Any, Generic, NoReturn, TypeVar, overload -_K = TypeVar("_K") +_K = TypeVar("_K", Hashable) _V = TypeVar("_V") _KV = TypeVar("_V", _K, _V) From 6843d2faadf924446dfec49ba33292a03f739732 Mon Sep 17 00:00:00 2001 From: Marco Sulla Date: Tue, 20 Dec 2022 22:05:52 +0100 Subject: [PATCH 09/27] typo --- frozendict/frozendict.pyi | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/frozendict/frozendict.pyi b/frozendict/frozendict.pyi index b2acfb8..66d1497 100644 --- a/frozendict/frozendict.pyi +++ b/frozendict/frozendict.pyi @@ -4,7 +4,7 @@ from typing import Any, Generic, NoReturn, TypeVar, overload _K = TypeVar("_K", Hashable) _V = TypeVar("_V") -_KV = TypeVar("_V", _K, _V) +_KV = TypeVar("_KV", _K, _V) def frozendict_or( self: Mapping[_K, _V], other: Mapping[_K, _V] From 07d3d5af61526d415358b31d5a1a6c88a53373d1 Mon Sep 17 00:00:00 2001 From: Marco Sulla Date: Tue, 20 Dec 2022 22:12:30 +0100 Subject: [PATCH 10/27] fixed __reversed__ return type --- frozendict/frozendict.pyi | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/frozendict/frozendict.pyi b/frozendict/frozendict.pyi index 66d1497..aced0e1 100644 --- a/frozendict/frozendict.pyi +++ b/frozendict/frozendict.pyi @@ -4,7 +4,7 @@ from typing import Any, Generic, NoReturn, TypeVar, overload _K = TypeVar("_K", Hashable) _V = TypeVar("_V") -_KV = TypeVar("_KV", _K, _V) +_KV = TypeVar("_V", _K, _V) def frozendict_or( self: Mapping[_K, _V], other: Mapping[_K, _V] @@ -37,7 +37,7 @@ class frozendict(Mapping[_K, _V], Generic[_K, _V]): def item(self, index: int) -> Sequence[_KV]: ... def __or__(self: Mapping[_K, _V], other: Mapping[_K, _V]) -> frozendict[_K, _V]: ... def __ior__(self: Mapping[_K, _V], other: Mapping[_K, _V]) -> frozendict[_K, _V]: ... - def __reversed__(self) -> Iterator[Sequence[_KV]]: ... + def __reversed__(self) -> Iterator[_K]: ... # Blacklisted methods: def __setattr__(self, *a, **kw) -> NoReturn: ... From 915ce02debea5aca3fe4fe551f8d50c6a2c3e702 Mon Sep 17 00:00:00 2001 From: Marco Sulla Date: Tue, 20 Dec 2022 22:23:19 +0100 Subject: [PATCH 11/27] support python < 3.9 --- frozendict/frozendict.pyi | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/frozendict/frozendict.pyi b/frozendict/frozendict.pyi index aced0e1..d49e60d 100644 --- a/frozendict/frozendict.pyi +++ b/frozendict/frozendict.pyi @@ -1,7 +1,11 @@ -import collections -from collections.abc import Iterable, Iterator, Mapping, Sequence, Hashable +from collections.abc import Hashable from typing import Any, Generic, NoReturn, TypeVar, overload +try: + from typing import Mapping, Sequence, Iterable, Iterator +except ImportError: + from collections.abc import Mapping, Sequence, Iterable, Iterator + _K = TypeVar("_K", Hashable) _V = TypeVar("_V") _KV = TypeVar("_V", _K, _V) From 6a377cbaa77eea94a7d71e5b79667b881751cfbd Mon Sep 17 00:00:00 2001 From: Marco Sulla Date: Tue, 20 Dec 2022 22:35:55 +0100 Subject: [PATCH 12/27] Fixed frozendict returns and self --- frozendict/frozendict.pyi | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/frozendict/frozendict.pyi b/frozendict/frozendict.pyi index d49e60d..9c8e012 100644 --- a/frozendict/frozendict.pyi +++ b/frozendict/frozendict.pyi @@ -29,17 +29,17 @@ class frozendict(Mapping[_K, _V], Generic[_K, _V]): def __iter__(self) -> Iterator[_K]: ... def __hash__(self) -> int: ... def __repr__(self) -> str: ... - def copy(self: frozendict[_K, _V]) -> frozendict[_K, _V]: ... - def __copy__(self: frozendict[_K, _V]) -> frozendict[_K, _V]: ... - def __deepcopy__(self: frozendict[_K, _V]) -> frozendict[_K, _V]: ... + def copy(self) -> "frozendict[_K, _V]": ... + def __copy__(self) -> "frozendict[_K, _V]": ... + def __deepcopy__(self) -> "frozendict[_K, _V]": ... # Omit __reduce__, its used for Pickle and we don't need the annotation in code. - def set(self, key: _K, value: _V) -> frozendict[_K, _V]: ... - def setdefault(self: frozendict[_K, _V], key: _K, default: _V) -> frozendict[_K, _V]: ... - def delete(self, key: _K) -> frozendict[_K, _V]: ... + def set(self, key: _K, value: _V) -> "frozendict[_K, _V]": ... + def setdefault(self, key: _K, default: _V) -> "frozendict[_K, _V]": ... + def delete(self, key: _K) -> "frozendict[_K, _V]": ... def key(self, index: int) -> _K: ... def value(self, index: int) -> _V: ... def item(self, index: int) -> Sequence[_KV]: ... - def __or__(self: Mapping[_K, _V], other: Mapping[_K, _V]) -> frozendict[_K, _V]: ... + def __or__(self: Mapping[_K, _V], other: Mapping[_K, _V]) -> "frozendict[_K, _V]": ... def __ior__(self: Mapping[_K, _V], other: Mapping[_K, _V]) -> frozendict[_K, _V]: ... def __reversed__(self) -> Iterator[_K]: ... From 273ce749ebb9270daa691f66676da2f9c5c5ae3f Mon Sep 17 00:00:00 2001 From: Marco Sulla Date: Tue, 20 Dec 2022 22:37:05 +0100 Subject: [PATCH 13/27] No __ior__ for an immutable --- frozendict/frozendict.pyi | 1 - 1 file changed, 1 deletion(-) diff --git a/frozendict/frozendict.pyi b/frozendict/frozendict.pyi index 9c8e012..ad66091 100644 --- a/frozendict/frozendict.pyi +++ b/frozendict/frozendict.pyi @@ -40,7 +40,6 @@ class frozendict(Mapping[_K, _V], Generic[_K, _V]): def value(self, index: int) -> _V: ... def item(self, index: int) -> Sequence[_KV]: ... def __or__(self: Mapping[_K, _V], other: Mapping[_K, _V]) -> "frozendict[_K, _V]": ... - def __ior__(self: Mapping[_K, _V], other: Mapping[_K, _V]) -> frozendict[_K, _V]: ... def __reversed__(self) -> Iterator[_K]: ... # Blacklisted methods: From 6aa1e2721376e005c9f31c7f3ca8886675db90ab Mon Sep 17 00:00:00 2001 From: Marco Sulla Date: Tue, 20 Dec 2022 22:40:01 +0100 Subject: [PATCH 14/27] minor --- frozendict/frozendict.pyi | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/frozendict/frozendict.pyi b/frozendict/frozendict.pyi index ad66091..c2fc638 100644 --- a/frozendict/frozendict.pyi +++ b/frozendict/frozendict.pyi @@ -1,5 +1,5 @@ from collections.abc import Hashable -from typing import Any, Generic, NoReturn, TypeVar, overload +from typing import Generic, NoReturn, TypeVar, overload try: from typing import Mapping, Sequence, Iterable, Iterator From 566f5086fe3942282778758588d324c5a22bad57 Mon Sep 17 00:00:00 2001 From: Marco Sulla Date: Tue, 20 Dec 2022 23:03:39 +0100 Subject: [PATCH 15/27] support subclassing --- frozendict/frozendict.pyi | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/frozendict/frozendict.pyi b/frozendict/frozendict.pyi index c2fc638..601ac02 100644 --- a/frozendict/frozendict.pyi +++ b/frozendict/frozendict.pyi @@ -9,6 +9,7 @@ except ImportError: _K = TypeVar("_K", Hashable) _V = TypeVar("_V") _KV = TypeVar("_V", _K, _V) +_T = TypeVar("_T", Mapping[_K, _V]) def frozendict_or( self: Mapping[_K, _V], other: Mapping[_K, _V] @@ -29,17 +30,17 @@ class frozendict(Mapping[_K, _V], Generic[_K, _V]): def __iter__(self) -> Iterator[_K]: ... def __hash__(self) -> int: ... def __repr__(self) -> str: ... - def copy(self) -> "frozendict[_K, _V]": ... - def __copy__(self) -> "frozendict[_K, _V]": ... - def __deepcopy__(self) -> "frozendict[_K, _V]": ... + def copy(self: _T) -> _T: ... + def __copy__(self: _T) -> _T: ... + def __deepcopy__(self: _T) -> _T: ... # Omit __reduce__, its used for Pickle and we don't need the annotation in code. - def set(self, key: _K, value: _V) -> "frozendict[_K, _V]": ... - def setdefault(self, key: _K, default: _V) -> "frozendict[_K, _V]": ... - def delete(self, key: _K) -> "frozendict[_K, _V]": ... + def set(self: _T, key: _K, value: _V) -> _T: ... + def setdefault(self: _T, key: _K, default: _V) -> _T: ... + def delete(self: _T, key: _K) -> _T: ... def key(self, index: int) -> _K: ... def value(self, index: int) -> _V: ... def item(self, index: int) -> Sequence[_KV]: ... - def __or__(self: Mapping[_K, _V], other: Mapping[_K, _V]) -> "frozendict[_K, _V]": ... + def __or__(self: _T, other: Mapping[_K, _V]) -> _T: ... def __reversed__(self) -> Iterator[_K]: ... # Blacklisted methods: From 2df287ff7d449f690ed8108d586afbed26de452b Mon Sep 17 00:00:00 2001 From: Marco Sulla Date: Tue, 20 Dec 2022 23:07:28 +0100 Subject: [PATCH 16/27] added fromkeys --- frozendict/frozendict.pyi | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/frozendict/frozendict.pyi b/frozendict/frozendict.pyi index 601ac02..7e0c91c 100644 --- a/frozendict/frozendict.pyi +++ b/frozendict/frozendict.pyi @@ -1,5 +1,5 @@ from collections.abc import Hashable -from typing import Generic, NoReturn, TypeVar, overload +from typing import Generic, NoReturn, TypeVar, overload, Type, Optional try: from typing import Mapping, Sequence, Iterable, Iterator @@ -42,6 +42,13 @@ class frozendict(Mapping[_K, _V], Generic[_K, _V]): def item(self, index: int) -> Sequence[_KV]: ... def __or__(self: _T, other: Mapping[_K, _V]) -> _T: ... def __reversed__(self) -> Iterator[_K]: ... + + @classmethod + def fromkeys( + cls: Type[_T], + seq: Iterable[_K], + value: Optional[_V] = None + ) -> _T: ... # Blacklisted methods: def __setattr__(self, *a, **kw) -> NoReturn: ... From 3d35a8e6d8c8c00d92133d11d6be9335a6c216a0 Mon Sep 17 00:00:00 2001 From: Marco Sulla Date: Tue, 20 Dec 2022 23:15:32 +0100 Subject: [PATCH 17/27] removing Generic (not sure) --- frozendict/frozendict.pyi | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/frozendict/frozendict.pyi b/frozendict/frozendict.pyi index 7e0c91c..5d45f8d 100644 --- a/frozendict/frozendict.pyi +++ b/frozendict/frozendict.pyi @@ -1,5 +1,5 @@ from collections.abc import Hashable -from typing import Generic, NoReturn, TypeVar, overload, Type, Optional +from typing import NoReturn, TypeVar, overload, Type, Optional try: from typing import Mapping, Sequence, Iterable, Iterator @@ -15,7 +15,7 @@ def frozendict_or( self: Mapping[_K, _V], other: Mapping[_K, _V] ) -> "frozendict[_K, _V]": ... -class frozendict(Mapping[_K, _V], Generic[_K, _V]): +class frozendict(Mapping[_K, _V]): # Fake __init__ to describe what __new__ does: @overload def __init__(self, **kwargs: _V) -> None: ... From 2ba8db70d39e1816c835596b819949c608305d3b Mon Sep 17 00:00:00 2001 From: Marco Sulla Date: Tue, 20 Dec 2022 23:18:27 +0100 Subject: [PATCH 18/27] removed frozendict_or --- frozendict/frozendict.pyi | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/frozendict/frozendict.pyi b/frozendict/frozendict.pyi index 5d45f8d..ba30a37 100644 --- a/frozendict/frozendict.pyi +++ b/frozendict/frozendict.pyi @@ -8,13 +8,9 @@ except ImportError: _K = TypeVar("_K", Hashable) _V = TypeVar("_V") -_KV = TypeVar("_V", _K, _V) +_KV = TypeVar("_KV", _K, _V) _T = TypeVar("_T", Mapping[_K, _V]) -def frozendict_or( - self: Mapping[_K, _V], other: Mapping[_K, _V] -) -> "frozendict[_K, _V]": ... - class frozendict(Mapping[_K, _V]): # Fake __init__ to describe what __new__ does: @overload From 1d81d08f580981f42223b72a02eab4386661ce30 Mon Sep 17 00:00:00 2001 From: Marco Sulla Date: Tue, 20 Dec 2022 23:21:23 +0100 Subject: [PATCH 19/27] removed IMHO unnecessary underscores --- frozendict/frozendict.pyi | 50 +++++++++++++++++++-------------------- 1 file changed, 25 insertions(+), 25 deletions(-) diff --git a/frozendict/frozendict.pyi b/frozendict/frozendict.pyi index ba30a37..f069b03 100644 --- a/frozendict/frozendict.pyi +++ b/frozendict/frozendict.pyi @@ -6,45 +6,45 @@ try: except ImportError: from collections.abc import Mapping, Sequence, Iterable, Iterator -_K = TypeVar("_K", Hashable) -_V = TypeVar("_V") -_KV = TypeVar("_KV", _K, _V) -_T = TypeVar("_T", Mapping[_K, _V]) +K = TypeVar("K", Hashable) +V = TypeVar("V") +KV = TypeVar("KV", K, V) +T = TypeVar("T", Mapping[K, V]) -class frozendict(Mapping[_K, _V]): +class frozendict(Mapping[K, V]): # Fake __init__ to describe what __new__ does: @overload - def __init__(self, **kwargs: _V) -> None: ... + def __init__(self, **kwargs: V) -> None: ... @overload - def __init__(self, mapping: Mapping[_K, _V]) -> None: ... + def __init__(self, mapping: Mapping[K, V]) -> None: ... @overload - def __init__(self, iterable: Iterable[Sequence[_KV]]) -> None: ... + def __init__(self, iterable: Iterable[Sequence[KV]]) -> None: ... # Magic Methods: - def __getitem__(self, __key: _K) -> _V: ... + def __getitem__(self, key: K) -> V: ... def __len__(self) -> int: ... - def __iter__(self) -> Iterator[_K]: ... + def __iter__(self) -> Iterator[K]: ... def __hash__(self) -> int: ... def __repr__(self) -> str: ... - def copy(self: _T) -> _T: ... - def __copy__(self: _T) -> _T: ... - def __deepcopy__(self: _T) -> _T: ... + def copy(self: T) -> T: ... + def __copy__(self: T) -> T: ... + def __deepcopy__(self: T) -> T: ... # Omit __reduce__, its used for Pickle and we don't need the annotation in code. - def set(self: _T, key: _K, value: _V) -> _T: ... - def setdefault(self: _T, key: _K, default: _V) -> _T: ... - def delete(self: _T, key: _K) -> _T: ... - def key(self, index: int) -> _K: ... - def value(self, index: int) -> _V: ... - def item(self, index: int) -> Sequence[_KV]: ... - def __or__(self: _T, other: Mapping[_K, _V]) -> _T: ... - def __reversed__(self) -> Iterator[_K]: ... + def set(self: T, key: K, value: V) -> T: ... + def setdefault(self: T, key: K, default: V) -> T: ... + def delete(self: T, key: K) -> T: ... + def key(self, index: int) -> K: ... + def value(self, index: int) -> V: ... + def item(self, index: int) -> Sequence[KV]: ... + def __or__(self: T, other: Mapping[K, V]) -> T: ... + def __reversed__(self) -> Iterator[K]: ... @classmethod def fromkeys( - cls: Type[_T], - seq: Iterable[_K], - value: Optional[_V] = None - ) -> _T: ... + cls: Type[T], + seq: Iterable[K], + value: Optional[V] = None + ) -> T: ... # Blacklisted methods: def __setattr__(self, *a, **kw) -> NoReturn: ... From 4dc946283c08bc76e30c46b16fe7bd6db9097aa8 Mon Sep 17 00:00:00 2001 From: Marco Sulla Date: Wed, 26 Apr 2023 19:40:22 +0200 Subject: [PATCH 20/27] Rename frozendict.pyi to __init__.pyi It should be __init__, since it's a module --- frozendict/{frozendict.pyi => __init__.pyi} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename frozendict/{frozendict.pyi => __init__.pyi} (100%) diff --git a/frozendict/frozendict.pyi b/frozendict/__init__.pyi similarity index 100% rename from frozendict/frozendict.pyi rename to frozendict/__init__.pyi From 2da7ed9a97b9fb6fecfd5763f3be325c6a2c37de Mon Sep 17 00:00:00 2001 From: Marco Sulla Date: Wed, 26 Apr 2023 20:03:34 +0200 Subject: [PATCH 21/27] Update __init__.pyi fix fromkeys return type --- frozendict/__init__.pyi | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/frozendict/__init__.pyi b/frozendict/__init__.pyi index f069b03..d085d3d 100644 --- a/frozendict/__init__.pyi +++ b/frozendict/__init__.pyi @@ -44,7 +44,7 @@ class frozendict(Mapping[K, V]): cls: Type[T], seq: Iterable[K], value: Optional[V] = None - ) -> T: ... + ) -> "frozendict[K, V]": ... # Blacklisted methods: def __setattr__(self, *a, **kw) -> NoReturn: ... From 272b2a39263e472c2bf929c336662b0412c64da4 Mon Sep 17 00:00:00 2001 From: Marco Sulla Date: Wed, 26 Apr 2023 20:21:36 +0200 Subject: [PATCH 22/27] key can'b be Hashable.... See https://github.com/python/mypy/issues/9698 --- frozendict/__init__.pyi | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/frozendict/__init__.pyi b/frozendict/__init__.pyi index d085d3d..198da31 100644 --- a/frozendict/__init__.pyi +++ b/frozendict/__init__.pyi @@ -6,7 +6,7 @@ try: except ImportError: from collections.abc import Mapping, Sequence, Iterable, Iterator -K = TypeVar("K", Hashable) +K = TypeVar("K") V = TypeVar("V") KV = TypeVar("KV", K, V) T = TypeVar("T", Mapping[K, V]) From ab94ddadafe4ccce2fd5bd43d7611f7c616e8765 Mon Sep 17 00:00:00 2001 From: Marco Sulla Date: Wed, 26 Apr 2023 20:33:30 +0200 Subject: [PATCH 23/27] renamed pyi --- setup.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setup.py b/setup.py index e11f1b7..a5d0787 100755 --- a/setup.py +++ b/setup.py @@ -11,7 +11,7 @@ readme_filename = "README.md" version_filename = "version.py" py_typed_filename = "py.typed" -mypy_filename = "frozendict.pyi" +mypy_filename = "__init__.pyi" main_url = "https://github.com/Marco-Sulla/python-frozendict" bug_url = "https://github.com/Marco-Sulla/python-frozendict/issues" author = "Marco Sulla" From 79788c7d229063d0b2b8eba6d4b0786253ba8653 Mon Sep 17 00:00:00 2001 From: Marco Sulla Date: Wed, 26 Apr 2023 20:34:17 +0200 Subject: [PATCH 24/27] renamed pyi --- MANIFEST.in | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/MANIFEST.in b/MANIFEST.in index 5ed17d1..eee3195 100644 --- a/MANIFEST.in +++ b/MANIFEST.in @@ -1,4 +1,4 @@ recursive-include frozendict/src * include test/* include frozendict/py.typed -include frozendict/frozendict.pyi +include frozendict/__init__.pyi From d9e7792883ab495c4070420d6198a130a1b09ec9 Mon Sep 17 00:00:00 2001 From: Marco Sulla Date: Wed, 26 Apr 2023 21:32:11 +0200 Subject: [PATCH 25/27] Rename frozendict/__init__.pyi to src/frozendict/__init.py --- frozendict/__init__.pyi => src/frozendict/__init.py | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename frozendict/__init__.pyi => src/frozendict/__init.py (100%) diff --git a/frozendict/__init__.pyi b/src/frozendict/__init.py similarity index 100% rename from frozendict/__init__.pyi rename to src/frozendict/__init.py From 31cae8b4abf5b0ded45b6f2f8216fb66d4169b78 Mon Sep 17 00:00:00 2001 From: Marco Sulla Date: Wed, 26 Apr 2023 21:33:39 +0200 Subject: [PATCH 26/27] Rename __init.py to __init__.pyi --- src/frozendict/{__init.py => __init__.pyi} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename src/frozendict/{__init.py => __init__.pyi} (100%) diff --git a/src/frozendict/__init.py b/src/frozendict/__init__.pyi similarity index 100% rename from src/frozendict/__init.py rename to src/frozendict/__init__.pyi From 5226d25137ef1eb911f44f7eeda253103b5c3090 Mon Sep 17 00:00:00 2001 From: Marco Sulla Date: Wed, 26 Apr 2023 21:43:14 +0200 Subject: [PATCH 27/27] Delete frozendict.pyi --- src/frozendict/frozendict.pyi | 6 ------ 1 file changed, 6 deletions(-) delete mode 100644 src/frozendict/frozendict.pyi diff --git a/src/frozendict/frozendict.pyi b/src/frozendict/frozendict.pyi deleted file mode 100644 index e43b742..0000000 --- a/src/frozendict/frozendict.pyi +++ /dev/null @@ -1,6 +0,0 @@ -try: - from ._frozendict import * -except ImportError: - from .core import * - -FrozenOrderedDict = frozendict