Skip to content

Commit 220079e

Browse files
authored
Fix type annotation for hash function. (#655)
1 parent 5b5dbcf commit 220079e

File tree

4 files changed

+9
-5
lines changed

4 files changed

+9
-5
lines changed

docs/source/changes.md

+2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ releases are available on [PyPI](https://pypi.org/project/pytask) and
1111
- {pull}`640` stops the live display when an exception happened during the execution.
1212
- {pull}`646` adds a `.gitignore` to the `.pytask/` folder to exclude it from version
1313
control.
14+
- {pull}`656` fixes the return type of the hash function for {class}`PythonNode`s.
15+
Thanks to {user}`axtimhaus` for reporting the issue.
1416

1517
## 0.5.1 - 2024-07-20
1618

docs/source/how_to_guides/hashing_inputs_of_tasks.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,8 @@ $ pip install deepdiff
7272
$ conda install deepdiff
7373
```
7474

75-
Then, create the hash function and pass it to the node.
75+
Then, create the hash function and pass it to the node. Make sure it returns either an
76+
integer or a string.
7677

7778
`````{tab-set}
7879

docs_src/how_to_guides/hashing_inputs_of_tasks_example_3_py310.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from pytask import PythonNode
1010

1111

12-
def calculate_hash(x: Any) -> str:
12+
def calculate_hash(x: Any) -> int | str:
1313
return DeepHash(x)[x]
1414

1515

src/_pytask/nodes.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ class PythonNode(PNode):
216216
Whether the value should be hashed to determine the state. Use ``True`` for
217217
objects that are hashable like strings and tuples. For dictionaries and other
218218
non-hashable objects, you need to provide a function that can hash these
219-
objects.
219+
objects. The function should return either an integer or a string.
220220
node_info
221221
The infos acquired while collecting the node.
222222
@@ -235,7 +235,7 @@ class PythonNode(PNode):
235235

236236
name: str = ""
237237
value: Any | NoDefault = no_default
238-
hash: bool | Callable[[Any], bool] = False
238+
hash: bool | Callable[[Any], int | str] = False
239239
node_info: NodeInfo | None = None
240240

241241
@property
@@ -269,7 +269,8 @@ def state(self) -> str | None:
269269
If ``hash = False``, the function returns ``"0"``, a constant hash value, so the
270270
:class:`PythonNode` is ignored when checking for a changed state of the task.
271271
272-
If ``hash`` is a callable, then use this function to calculate a hash.
272+
If ``hash`` is a callable, then use this function to calculate a hash expecting
273+
an integer or string.
273274
274275
If ``hash = True``, the builtin ``hash()`` function (`link
275276
<https://docs.python.org/3.11/library/functions.html?highlight=hash#hash>`_) is

0 commit comments

Comments
 (0)