Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enum deterministic hashing #9212

Merged
merged 2 commits into from Jun 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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