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

Wrong type hint for return types from a task #16502

Open
gorootde opened this issue Dec 25, 2024 · 1 comment
Open

Wrong type hint for return types from a task #16502

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

Comments

@gorootde
Copy link

gorootde commented Dec 25, 2024

Bug summary

Any task's return type hint is lost when annotating it with @task.

Example 1:

class Foo:
    def foo(self):
        pass   

@task
def get_foo() -> Foo:
  return Foo()

@task
def get_list() -> List[Any]:
  ...

@flow
def run():
   result = get_foo() #Type of result is None and not Foo
   result2 = get_list() # Type of result2 is None and not List[Any]

Version info

Version:             3.1.10
API version:         0.8.4
Python version:      3.11.10
Git commit:          b11b56fc
Built:               Tue, Dec 24, 2024 10:58 PM
OS/Arch:             darwin/arm64
Profile:             local
Server type:         server
Pydantic version:    2.10.4

Additional context

No response

@gorootde gorootde added the bug Something isn't working label Dec 25, 2024
@gorootde gorootde changed the title Wrong type hint when returning classes from a task Wrong type hint for return types from a task Dec 25, 2024
@zzstoatzz
Copy link
Collaborator

zzstoatzz commented Dec 26, 2024

hi @gorootde - can you say more about how you're seeing a loss of type information? ie what is reporting this?

  • I am not able to reproduce the problem with get_foo
  • get_list is returning None because you have ... in place of the implementation
from typing import Any, reveal_type

from prefect import flow, task


class Foo:
    def foo(self):
        pass


@task
def get_foo() -> Foo:
    return Foo()


@task
def get_list() -> list[Any]: ...


@task
def actually_get_list() -> list[Any]:
    return [1, 2, 3]


@flow
def run():
    result = get_foo()
    assert isinstance(result, Foo)
    result2 = get_list()
    assert result2 is None
    result3 = actually_get_list()
    assert isinstance(result3, list)
    reveal_type(result)  # Runtime type is 'Foo'
    reveal_type(result2)  # Runtime type is 'NoneType'
    reveal_type(result3)  # Runtime type is 'list'


if __name__ == "__main__":
    run()

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