Skip to content

Commit

Permalink
Enum deterministic hashing (#9212)
Browse files Browse the repository at this point in the history
  • Loading branch information
faulaire committed Jun 24, 2022
1 parent 8f5d2c1 commit 0ee07e3
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
6 changes: 6 additions & 0 deletions dask/base.py
Expand Up @@ -13,6 +13,7 @@
from collections.abc import Callable, Iterator, Mapping
from concurrent.futures import Executor
from contextlib import contextmanager
from enum import Enum
from functools import partial
from numbers import Integral, Number
from operator import getitem
Expand Down Expand Up @@ -999,6 +1000,11 @@ def normalize_range(r):
return list(map(normalize_token, [r.start, r.stop, r.step]))


@normalize_token.register(Enum)
def normalize_enum(e):
return type(e).__name__, e.name, e.value


@normalize_token.register(object)
def normalize_object(o):
method = getattr(o, "__dask_tokenize__", None)
Expand Down
11 changes: 11 additions & 0 deletions dask/tests/test_base.py
Expand Up @@ -7,6 +7,7 @@
import time
from collections import OrderedDict
from concurrent.futures import Executor
from enum import Enum, Flag, IntEnum, IntFlag
from operator import add, mul
from typing import Union

Expand Down Expand Up @@ -425,6 +426,16 @@ def test_tokenize_ordered_dict():
assert tokenize(a) != tokenize(c)


@pytest.mark.parametrize("enum_type", [Enum, IntEnum, IntFlag, Flag])
def test_tokenize_enum(enum_type):
class Color(enum_type):
RED = 1
BLUE = 2

assert tokenize(Color.RED) == tokenize(Color.RED)
assert tokenize(Color.RED) != tokenize(Color.BLUE)


ADataClass = dataclasses.make_dataclass("ADataClass", [("a", int)])
BDataClass = dataclasses.make_dataclass("BDataClass", [("a", Union[int, float])]) # type: ignore

Expand Down

0 comments on commit 0ee07e3

Please sign in to comment.