Invoke: TypeError: Tasks must have an initial Context argument!

Created on 18 Jun 2016  路  6Comments  路  Source: pyinvoke/invoke

Upgrading from v0.12.2 to v0.13.0 gives causes my task to error with TypeError: Tasks must have an initial Context argument!.

https://github.com/pyinvoke/invoke/compare/0ce6220...7f8fdc3#diff-a2c23243533cad12946af2f1122bb4ebR144

$ invoke test
Traceback (most recent call last):
  File "/home/travis/virtualenv/python3.4.2/lib/python3.4/site-packages/invoke/tasks.py", line 141, in argspec
    context_arg = arg_names.pop(0)
IndexError: pop from empty list
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
  File "/home/travis/virtualenv/python3.4.2/bin/invoke", line 11, in <module>
    sys.exit(program.run())
  File "/home/travis/virtualenv/python3.4.2/lib/python3.4/site-packages/invoke/program.py", line 269, in run
    self._parse(argv)
  File "/home/travis/virtualenv/python3.4.2/lib/python3.4/site-packages/invoke/program.py", line 325, in _parse
    self.load_collection()
  File "/home/travis/virtualenv/python3.4.2/lib/python3.4/site-packages/invoke/program.py", line 473, in load_collection
    coll = loader.load(coll_name) if coll_name else loader.load()
  File "/home/travis/virtualenv/python3.4.2/lib/python3.4/site-packages/invoke/loader.py", line 53, in load
    module = imp.load_module(name, fd, path, desc)
  File "/home/travis/virtualenv/python3.4.2/lib/python3.4/imp.py", line 235, in load_module
    return load_source(name, filename, file)
  File "/home/travis/virtualenv/python3.4.2/lib/python3.4/imp.py", line 171, in load_source
    module = methods.load()
  File "<frozen importlib._bootstrap>", line 1220, in load
  File "<frozen importlib._bootstrap>", line 1200, in _load_unlocked
  File "<frozen importlib._bootstrap>", line 1129, in _exec
  File "<frozen importlib._bootstrap>", line 1471, in exec_module
  File "<frozen importlib._bootstrap>", line 321, in _call_with_frames_removed
  File "/home/travis/build/deckar01/marshmallow/tasks.py", line 27, in <module>
    @task
  File "/home/travis/virtualenv/python3.4.2/lib/python3.4/site-packages/invoke/tasks.py", line 267, in task
    return Task(args[0], **kwargs)
  File "/home/travis/virtualenv/python3.4.2/lib/python3.4/site-packages/invoke/tasks.py", line 58, in __init__
    self.positional = self.fill_implicit_positionals(positional)
  File "/home/travis/virtualenv/python3.4.2/lib/python3.4/site-packages/invoke/tasks.py", line 149, in fill_implicit_positionals
    args, spec_dict = self.argspec(self.body)
  File "/home/travis/virtualenv/python3.4.2/lib/python3.4/site-packages/invoke/tasks.py", line 144, in argspec
    raise TypeError("Tasks must have an initial Context argument!")
TypeError: Tasks must have an initial Context argument!

Most helpful comment

I'm very new to Invoke (10 minutes experience), but for those like me running into this bug, a temporary workaround is to add a positional argument to the tasks, e.g.:

@task
def build(ctx):
    print("Building!")

All 6 comments

I'm very new to Invoke (10 minutes experience), but for those like me running into this bug, a temporary workaround is to add a positional argument to the tasks, e.g.:

@task
def build(ctx):
    print("Building!")

Thanks for your fresh eyes @kynikos. 馃憖

I also had to pass the context object along to child tasks.

@task
def build(ctx):
    foo(ctx)

@task
def foo(ctx):
    print("Building!")

It is not so much a temporary work around as it is using the 0.13 API for invoke. I will leave this issue open for now and suggest documenting breaking changes in a change log or upgrade notes.

It is worth noting that the readme example shows calling run on the context instance, but the library still allows importing invoke.run and not using the context object.

It would be nice if the task implementation created a new context instance for you when initializing a task and when calling a subtask.

Hi @deckar01

In the Changelog for 0.13.0 you can see that [Feature] #114 has a backwards incompatible change. Now, all tasks are contextualized and ctask is not needed anymore.

It also broke my invoke program, thanks to unit testing, I could easily discover this change and fix it :)

You can check the new documentation , you basically already fixed the problem

Since i'ts in the change log and no backwards compatibility is being enforced pre 1.0.0, I will close this issue. I will have to be more strict with my version pinning in the future.

It's really neccesary making ctx a required argument? I'm not using it in most on my methods so I get

"parameter 'ctx' value is not used"

lint error all the time.

EDIT

I've found this workaround to silence the linter error only for ctx.

@task
def create_application(ctx):
    del ctx

    # ...
Was this page helpful?
0 / 5 - 0 ratings

Related issues

saulshanabrook picture saulshanabrook  路  4Comments

StephenCarboni picture StephenCarboni  路  4Comments

lsh-0 picture lsh-0  路  5Comments

PatrickMassot picture PatrickMassot  路  6Comments

hindman picture hindman  路  12Comments