diff --git a/docs/source/changes.md b/docs/source/changes.md index 5b9107f4..2f0362b0 100644 --- a/docs/source/changes.md +++ b/docs/source/changes.md @@ -11,6 +11,8 @@ releases are available on [PyPI](https://pypi.org/project/pytask) and - {pull}`640` stops the live display when an exception happened during the execution. - {pull}`646` adds a `.gitignore` to the `.pytask/` folder to exclude it from version control. +- {pull}`656` fixes the return type of the hash function for {class}`PythonNode`s. + Thanks to {user}`axtimhaus` for reporting the issue. ## 0.5.1 - 2024-07-20 diff --git a/docs/source/how_to_guides/hashing_inputs_of_tasks.md b/docs/source/how_to_guides/hashing_inputs_of_tasks.md index 04a6d204..d589d079 100644 --- a/docs/source/how_to_guides/hashing_inputs_of_tasks.md +++ b/docs/source/how_to_guides/hashing_inputs_of_tasks.md @@ -72,7 +72,8 @@ $ pip install deepdiff $ conda install deepdiff ``` -Then, create the hash function and pass it to the node. +Then, create the hash function and pass it to the node. Make sure it returns either an +integer or a string. `````{tab-set} diff --git a/docs_src/how_to_guides/hashing_inputs_of_tasks_example_3_py310.py b/docs_src/how_to_guides/hashing_inputs_of_tasks_example_3_py310.py index 3640417f..1cda54be 100644 --- a/docs_src/how_to_guides/hashing_inputs_of_tasks_example_3_py310.py +++ b/docs_src/how_to_guides/hashing_inputs_of_tasks_example_3_py310.py @@ -9,7 +9,7 @@ from pytask import PythonNode -def calculate_hash(x: Any) -> str: +def calculate_hash(x: Any) -> int | str: return DeepHash(x)[x] diff --git a/src/_pytask/nodes.py b/src/_pytask/nodes.py index a0a1c0c3..3c230a34 100644 --- a/src/_pytask/nodes.py +++ b/src/_pytask/nodes.py @@ -216,7 +216,7 @@ class PythonNode(PNode): Whether the value should be hashed to determine the state. Use ``True`` for objects that are hashable like strings and tuples. For dictionaries and other non-hashable objects, you need to provide a function that can hash these - objects. + objects. The function should return either an integer or a string. node_info The infos acquired while collecting the node. @@ -235,7 +235,7 @@ class PythonNode(PNode): name: str = "" value: Any | NoDefault = no_default - hash: bool | Callable[[Any], bool] = False + hash: bool | Callable[[Any], int | str] = False node_info: NodeInfo | None = None @property @@ -269,7 +269,8 @@ def state(self) -> str | None: If ``hash = False``, the function returns ``"0"``, a constant hash value, so the :class:`PythonNode` is ignored when checking for a changed state of the task. - If ``hash`` is a callable, then use this function to calculate a hash. + If ``hash`` is a callable, then use this function to calculate a hash expecting + an integer or string. If ``hash = True``, the builtin ``hash()`` function (`link `_) is