pyinvoke / invoke

Pythonic task management & command execution.
http://pyinvoke.org
BSD 2-Clause "Simplified" License
4.31k stars 365 forks source link

task decorator is wrongly typed #946

Open kasium opened 1 year ago

kasium commented 1 year ago
from invoke.tasks import task
from invoke.collection import Collection

@task
def foo(ctx):
    pass

ns = Collection()
ns.add_task(foo)

leads to an mypy error:

error: Argument 1 to "add_task" of "Collection" has incompatible type "Callable[..., Any]"; expected "Task[Any]"  [arg-type]
    ns.add_task(foo)
4sushi commented 1 year ago

Your code works well on the last version of invoke:

In [1]: from invoke.tasks import task
   ...: from invoke.collection import Collection
   ...: 
   ...: @task
   ...: def foo(ctx):
   ...:     pass
   ...: 
   ...: ns = Collection()
   ...: ns.add_task(foo)

In [2]: ns
Out[2]: <Collection None: foo>

In [3]: ns.tasks
Out[3]: {'foo': <Task 'foo'>}

Which version of invoke are you using? Maybe you may consider to upgrade it:

pip install invoke --upgrade
python -m invoke --version
# Your version need to be >= 2.1.3

Also, based on the documentation, you may simplify your imports:

from invoke import task
from invoke import Collection
kasium commented 1 year ago

@4sushi the code works but mypy complains as I said

kuwv commented 12 months ago

956