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

@dataloader decorator doesn't work when using full type annotations #1260

Closed
jlopezpena opened this issue Dec 17, 2024 · 1 comment
Closed
Labels
triage label for issues that need to be triaged.

Comments

@jlopezpena
Copy link

Current behavior

If I use full type annotations for return types, like dict[str, Any] instead of dict, the @dataloader decorator raises an error

Stack Traces

Traceback (most recent call last):
  File "/Users/jlopezpena/Code/RA/pipelines/main.py", line 3, in <module>
    import functions
  File "/Users/jlopezpena/Code/RA/pipelines/functions.py", line 6, in <module>
    @dataloader()
     ~~~~~~~~~~^^
  File "/Users/jlopezpena/Code/RA/pipelines/.venv/lib/python3.13/site-packages/hamilton/function_modifiers/base.py", line 60, in replace__call__
    return call_fn(self, fn)
  File "/Users/jlopezpena/Code/RA/pipelines/.venv/lib/python3.13/site-packages/hamilton/function_modifiers/base.py", line 102, in __call__
    self.validate(fn)
    ~~~~~~~~~~~~~^^^^
  File "/Users/jlopezpena/Code/RA/pipelines/.venv/lib/python3.13/site-packages/hamilton/function_modifiers/adapters.py", line 752, in validate
    raise InvalidDecoratorException(
        f"Function: {fn.__qualname__} must return a tuple of type (SOME_TYPE, dict)."
    )
hamilton.function_modifiers.base.InvalidDecoratorException: Function: sample_dataloader must return a tuple of type (SOME_TYPE, dict).

Screenshots

image

Steps to replicate behavior

Simple reproducing example:

# functions.py

from typing import Any

from hamilton.function_modifiers import dataloader


@dataloader()
def sample_dataloader() -> tuple[list[str], dict[str, Any]]:
    return ["a", "b", "c"], {}
# main.py

from hamilton import driver

import functions

if __name__ == "__main__":
    dr = driver.Builder().with_modules(functions)

Then running the main script with python main.py raises the error shown in stacktrace and screenshot.

Library & System Information

Python 3.12.7, Hamilton 1.85.0, error reproduced in Mac OS and Ubuntu Linux, running in a virtual environment that just contains hamilton and its dependencies

Expected behavior

The decorator accepts the stricter type as valid

Additional context

Not a big deal, but generally annoying in projects that use strict typing, as making hamilton happy forces to add a # type: ignore comment in every decorated function to prevent mypy from complaining.

@jlopezpena jlopezpena added the triage label for issues that need to be triaged. label Dec 17, 2024
@elijahbenizzy
Copy link
Collaborator

Hey! Fix is released in 1.85.1. Closing -- open up if you need anything else!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
triage label for issues that need to be triaged.
Projects
None yet
Development

No branches or pull requests

2 participants