Skip to content

Fix type annotation for hash function. #655

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

Merged
merged 2 commits into from
Dec 16, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs/source/changes.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
3 changes: 2 additions & 1 deletion docs/source/how_to_guides/hashing_inputs_of_tasks.md
Original file line number Diff line number Diff line change
Expand Up @@ -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}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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]


Expand Down
7 changes: 4 additions & 3 deletions src/_pytask/nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand All @@ -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
Expand Down Expand Up @@ -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
<https://docs.python.org/3.11/library/functions.html?highlight=hash#hash>`_) is
Expand Down
Loading