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

BUG: Type Annotations in PythonNode Wrong #654

Open
3 tasks done
axtimhaus opened this issue Dec 12, 2024 · 1 comment
Open
3 tasks done

BUG: Type Annotations in PythonNode Wrong #654

axtimhaus opened this issue Dec 12, 2024 · 1 comment
Labels
bug Something isn't working

Comments

@axtimhaus
Copy link

axtimhaus commented Dec 12, 2024

  • I have checked that this issue has not already been reported.
  • I have confirmed this bug exists on the latest version of pytask.
  • (optional) I have confirmed this bug exists on the main branch of pytask.

Code Sample, a copy-pastable example

This is the example from the docs https://pytask-dev.readthedocs.io/en/stable/how_to_guides/hashing_inputs_of_tasks.html.

import json
from pathlib import Path
from typing import Annotated
from typing import Any

from deepdiff import DeepHash
from pytask import Product
from pytask import PythonNode


def calculate_hash(x: Any) -> str:
    return DeepHash(x)[x]


node = PythonNode(value={"a": 1, "b": 2}, hash=calculate_hash)


def task_example(
    data: Annotated[dict[str, int], node],
    path: Annotated[Path, Product] = Path("file.txt"),
) -> None:
    path.write_text(json.dumps(data))

Problem description

When I do this example, my type checker complains about the type of the provided hash function.

Type annotation from current master:

hash: bool | Callable[[Any], bool] = False

I think bool as return type of the callable makes no sense here.
Running the code works perfectly fine, just the checker complaining.

Expected Output

By looking into the source Callable[[Any], Any] would work (because the result is passed to str()), or a more defined one like Callable[[Any], int | str | bytes] as these are common returns of hash functions.

@axtimhaus axtimhaus added the bug Something isn't working label Dec 12, 2024
@tobiasraabe
Copy link
Member

tobiasraabe commented Dec 12, 2024

Thanks for filing the issue, @axtimhaus! I will have a look soon. I am wondering whether we need the flexibility with the union return type. Maybe I find packages offering similar functionality for inspiration.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants