From 0f5e573e3c2aa0d5545736995929051f84f251d0 Mon Sep 17 00:00:00 2001 From: Nathaniel Starkman Date: Fri, 28 Jun 2024 12:53:19 -0400 Subject: [PATCH] feat: deprecate multihash (#175) Signed-off-by: nstarman --- plum/__init__.py | 3 +++ plum/signature.py | 4 ++-- plum/util.py | 12 +++++++++--- 3 files changed, 14 insertions(+), 5 deletions(-) diff --git a/plum/__init__.py b/plum/__init__.py index 9570b4ab..543a620a 100644 --- a/plum/__init__.py +++ b/plum/__init__.py @@ -23,6 +23,9 @@ from .type import resolve_type_hint from .util import * # noqa: F401, F403 +# Deprecated +from .util import multihash # noqa: F401, F403 + # Ensure that type checking is always entirely correct! The default O(1) strategy # is super fast, but might yield unpredictable dispatch behaviour. The O(n) strategy # actually is not yet available, but we can already opt in to use it. diff --git a/plum/signature.py b/plum/signature.py index 9cdbeb90..fd9fb23e 100644 --- a/plum/signature.py +++ b/plum/signature.py @@ -13,7 +13,7 @@ from .repr import repr_short, rich_repr from .type import is_faithful, resolve_type_hint from .typing import get_type_hints -from .util import Comparable, Missing, TypeHint, multihash, wrap_lambda +from .util import Comparable, Missing, TypeHint, wrap_lambda __all__ = ["Signature", "append_default_args"] @@ -131,7 +131,7 @@ def __eq__(self, other): return False def __hash__(self): - return multihash(Signature, *self.types, self.varargs) + return hash((Signature, *self.types, self.varargs)) def expand_varargs(self, n: int) -> Tuple[TypeHint, ...]: """Expand variable arguments. diff --git a/plum/util.py b/plum/util.py index 3e57cc33..fb6ad303 100644 --- a/plum/util.py +++ b/plum/util.py @@ -1,6 +1,7 @@ import abc import sys -from typing import List, Sequence +import warnings +from typing import Hashable, List, Sequence if sys.version_info.minor <= 8: # pragma: specific no cover 3.9 3.10 3.11 from typing import Callable @@ -11,7 +12,6 @@ "Callable", "TypeHint", "Missing", - "multihash", "Comparable", "wrap_lambda", "is_in_class", @@ -47,7 +47,7 @@ def __init__(self): raise TypeError("`Missing` cannot be instantiated.") -def multihash(*args): +def multihash(*args: Hashable) -> int: """Multi-argument order-sensitive hash. Args: @@ -56,6 +56,12 @@ def multihash(*args): Returns: int: Hash. """ + warnings.warn( + "The function `multihash` is deprecated and will be removed in a future " + "version. Please use `hash(tuple(*args))` instead.", + DeprecationWarning, + stacklevel=2, + ) return hash(args)