From cf6f8a8452e672ca21d6d3465145459acf8c4246 Mon Sep 17 00:00:00 2001 From: Hajime Senuma Date: Thu, 12 Sep 2024 14:20:05 +0900 Subject: [PATCH] Fix and improve type hints (#76) --- CHANGELOG.md | 1 + README.md | 1 + src/mmh3/__init__.pyi | 60 ++++++++++++++++++++++++++----------------- 3 files changed, 38 insertions(+), 24 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..3243420 100644 --- a/src/mmh3/__init__.pyi +++ b/src/mmh3/__init__.pyi @@ -1,40 +1,52 @@ # 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, Union, final -class IntArrayLike(Protocol): - def __getitem__(self, index) -> int: ... +if TYPE_CHECKING: + from _typeshed import ReadableBuffer -Hashable = Union[bytes, bytearray, memoryview, IntArrayLike] -StrHashable = Union[str, Hashable] +HashableImmutable = Union[str, bytes] +HashableBuffer = Union[HashableImmutable, ReadableBuffer] -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: ReadableBuffer) -> None: ... def digest(self) -> bytes: ... def sintdigest(self) -> int: ... def uintdigest(self) -> int: ...