From ca6fc81ec789b33f21257f7eeacacb42e1bac008 Mon Sep 17 00:00:00 2001 From: Hajime Senuma Date: Thu, 12 Sep 2024 07:14:20 +0900 Subject: [PATCH 1/2] Fix type hints --- CHANGELOG.md | 1 + README.md | 1 + src/mmh3/__init__.pyi | 59 +++++++++++++++++++++++++++---------------- 3 files changed, 39 insertions(+), 22 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f40360f..15fa32b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -30,6 +30,7 @@ This project has adhered to - Fix a reference leak in the `hash_from_buffer()` function ([#75](https://github.com/hajimes/mmh3/issues/75)). +- Fix type hints. ## [4.1.0] - 2024-01-09 diff --git a/README.md b/README.md index adbf8fc..1074efc 100644 --- a/README.md +++ b/README.md @@ -155,6 +155,7 @@ complete changelog. - Fix a reference leak in the `hash_from_buffer()` function ([#75](https://github.com/hajimes/mmh3/issues/75)). +- Fix type hints. ### [4.1.0] - 2024-01-09 diff --git a/src/mmh3/__init__.pyi b/src/mmh3/__init__.pyi index 5edddf8..9425f86 100644 --- a/src/mmh3/__init__.pyi +++ b/src/mmh3/__init__.pyi @@ -1,40 +1,55 @@ # to use list, tuple, dict ... in Python 3.7 and 3.8 from __future__ import annotations -from typing import Protocol, Union, final +from typing import TYPE_CHECKING, Protocol, Union, final + +if TYPE_CHECKING: + from _typeshed import ReadableBuffer class IntArrayLike(Protocol): def __getitem__(self, index) -> int: ... -Hashable = Union[bytes, bytearray, memoryview, IntArrayLike] -StrHashable = Union[str, Hashable] +HashableImmutable = Union[str, bytes] +HashableBuffer = Union[HashableImmutable, ReadableBuffer, IntArrayLike] -def hash(key: StrHashable, seed: int = 0, signed: bool = True) -> int: ... -def hash_from_buffer(key: StrHashable, seed: int = 0, signed: bool = True) -> int: ... +def hash(key: HashableImmutable, seed: int = 0, signed: bool = True) -> int: ... +def hash_from_buffer( + key: HashableBuffer, seed: int = 0, signed: bool = True +) -> int: ... def hash64( - key: StrHashable, seed: int = 0, x64arch: bool = True, signed: bool = True + key: HashableImmutable, seed: int = 0, x64arch: bool = True, signed: bool = True ) -> tuple[int, int]: ... def hash128( - key: StrHashable, seed: int = 0, x64arch: bool = True, signed: bool = False + key: HashableImmutable, seed: int = 0, x64arch: bool = True, signed: bool = False ) -> int: ... -def hash_bytes(key: StrHashable, seed: int = 0, x64arch: bool = True) -> bytes: ... -def mmh3_32_digest(key: StrHashable, seed: int = 0) -> bytes: ... -def mmh3_32_sintdigest(key: StrHashable, seed: int = 0) -> int: ... -def mmh3_32_uintdigest(key: StrHashable, seed: int = 0) -> int: ... -def mmh3_x64_128_digest(key: StrHashable, seed: int = 0) -> bytes: ... -def mmh3_x64_128_sintdigest(key: StrHashable, seed: int = 0) -> int: ... -def mmh3_x64_128_uintdigest(key: StrHashable, seed: int = 0) -> int: ... -def mmh3_x64_128_stupledigest(key: StrHashable, seed: int = 0) -> tuple[int, int]: ... -def mmh3_x64_128_utupledigest(key: StrHashable, seed: int = 0) -> tuple[int, int]: ... -def mmh3_x86_128_digest(key: StrHashable, seed: int = 0) -> bytes: ... -def mmh3_x86_128_sintdigest(key: StrHashable, seed: int = 0) -> int: ... -def mmh3_x86_128_uintdigest(key: StrHashable, seed: int = 0) -> int: ... -def mmh3_x86_128_stupledigest(key: StrHashable, seed: int = 0) -> tuple[int, int]: ... -def mmh3_x86_128_utupledigest(key: StrHashable, seed: int = 0) -> tuple[int, int]: ... +def hash_bytes( + key: HashableImmutable, seed: int = 0, x64arch: bool = True +) -> bytes: ... +def mmh3_32_digest(key: HashableBuffer, seed: int = 0) -> bytes: ... +def mmh3_32_sintdigest(key: HashableBuffer, seed: int = 0) -> int: ... +def mmh3_32_uintdigest(key: HashableBuffer, seed: int = 0) -> int: ... +def mmh3_x64_128_digest(key: HashableBuffer, seed: int = 0) -> bytes: ... +def mmh3_x64_128_sintdigest(key: HashableBuffer, seed: int = 0) -> int: ... +def mmh3_x64_128_uintdigest(key: HashableBuffer, seed: int = 0) -> int: ... +def mmh3_x64_128_stupledigest( + key: HashableBuffer, seed: int = 0 +) -> tuple[int, int]: ... +def mmh3_x64_128_utupledigest( + key: HashableBuffer, seed: int = 0 +) -> tuple[int, int]: ... +def mmh3_x86_128_digest(key: HashableBuffer, seed: int = 0) -> bytes: ... +def mmh3_x86_128_sintdigest(key: HashableBuffer, seed: int = 0) -> int: ... +def mmh3_x86_128_uintdigest(key: HashableBuffer, seed: int = 0) -> int: ... +def mmh3_x86_128_stupledigest( + key: HashableBuffer, seed: int = 0 +) -> tuple[int, int]: ... +def mmh3_x86_128_utupledigest( + key: HashableBuffer, seed: int = 0 +) -> tuple[int, int]: ... class Hasher: def __init__(self, seed: int = 0) -> None: ... - def update(self, input: Hashable) -> None: ... + def update(self, input: HashableBuffer) -> None: ... def digest(self) -> bytes: ... def sintdigest(self) -> int: ... def uintdigest(self) -> int: ... From df14c927bd67610ac75550727f6c55b83a7299c0 Mon Sep 17 00:00:00 2001 From: Hajime Senuma Date: Thu, 12 Sep 2024 13:56:30 +0900 Subject: [PATCH 2/2] Improve type hints --- src/mmh3/__init__.pyi | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/src/mmh3/__init__.pyi b/src/mmh3/__init__.pyi index 9425f86..3243420 100644 --- a/src/mmh3/__init__.pyi +++ b/src/mmh3/__init__.pyi @@ -1,16 +1,13 @@ # to use list, tuple, dict ... in Python 3.7 and 3.8 from __future__ import annotations -from typing import TYPE_CHECKING, Protocol, Union, final +from typing import TYPE_CHECKING, Union, final if TYPE_CHECKING: from _typeshed import ReadableBuffer -class IntArrayLike(Protocol): - def __getitem__(self, index) -> int: ... - HashableImmutable = Union[str, bytes] -HashableBuffer = Union[HashableImmutable, ReadableBuffer, IntArrayLike] +HashableBuffer = Union[HashableImmutable, ReadableBuffer] def hash(key: HashableImmutable, seed: int = 0, signed: bool = True) -> int: ... def hash_from_buffer( @@ -49,7 +46,7 @@ def mmh3_x86_128_utupledigest( class Hasher: def __init__(self, seed: int = 0) -> None: ... - def update(self, input: HashableBuffer) -> None: ... + def update(self, input: ReadableBuffer) -> None: ... def digest(self) -> bytes: ... def sintdigest(self) -> int: ... def uintdigest(self) -> int: ...