Skip to content

Commit

Permalink
Merge pull request #70 from gauravmm/feature-typeannotations
Browse files Browse the repository at this point in the history
Added type annotations to pyi interface file.
  • Loading branch information
Marco-Sulla authored Apr 26, 2023
2 parents 28de798 + 5226d25 commit 0800598
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 8 deletions.
2 changes: 1 addition & 1 deletion MANIFEST.in
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
recursive-include src/frozendict/c_src *
include test/*
include src/frozendict/py.typed
include src/frozendict/frozendict.pyi
include src/frozendict/__init__.pyi
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
54 changes: 54 additions & 0 deletions src/frozendict/__init__.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
from collections.abc import Hashable
from typing import NoReturn, TypeVar, overload, Type, Optional

try:
from typing import Mapping, Sequence, Iterable, Iterator
except ImportError:
from collections.abc import Mapping, Sequence, Iterable, Iterator

K = TypeVar("K")
V = TypeVar("V")
KV = TypeVar("KV", K, V)
T = TypeVar("T", Mapping[K, V])

class frozendict(Mapping[K, V]):
# Fake __init__ to describe what __new__ does:
@overload
def __init__(self, **kwargs: V) -> None: ...
@overload
def __init__(self, mapping: Mapping[K, V]) -> None: ...
@overload
def __init__(self, iterable: Iterable[Sequence[KV]]) -> None: ...

# Magic Methods:
def __getitem__(self, key: K) -> V: ...
def __len__(self) -> int: ...
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: ...
# 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]: ...

@classmethod
def fromkeys(
cls: Type[T],
seq: Iterable[K],
value: Optional[V] = None
) -> "frozendict[K, V]": ...

# Blacklisted methods:
def __setattr__(self, *a, **kw) -> NoReturn: ...
def __delattr__(self, *a, **kw) -> NoReturn: ...


FrozenOrderedDict = frozendict
6 changes: 0 additions & 6 deletions src/frozendict/frozendict.pyi

This file was deleted.

0 comments on commit 0800598

Please sign in to comment.