Skip to content
Myr

Fix bug which happens while using @click.pass_obj

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Rebased version of #410 for bugfix release When a function is decorated with @click.pass_obj, it should receive the ctx.obj content as first argument. Instead, it actually receives the ctx.obj content *and* ctx itself. This is an undocumented behavior, and it was introduced in click 5.0 with commit 695ca34. This commit fixes the decorator in order to restore the documented, pre-5.0 behavior, so the decorated function will receive only ctx.obj.

authored by Pietro Albini · committed by Markus Unterwaditzer · parent d549018 · Browse files · 3 files+23 added−1 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

3 files+23 added−1 removed

  • tests:+16 added−0 removed
  • other:+7 added−1 removed

3 files+23 added−1 removed

modified:click/decorators.py-1 removed
20 unmodified lines
21 """Similar to :func:`pass_context`, but only pass the object on the22 context onwards (:attr:`Context.obj`). This is useful if that object23 represents the state of a nested system.24 """25 @pass_context25 def new_func(*args, **kwargs):26 return f(get_current_context().obj, *args, **kwargs)27 return update_wrapper(new_func, f)28
275 unmodified lines
modified:tests/test_context.py+16 added
166 unmodified lines
167 # Internal168 assert ctx._depth == 1169170 assert rv == [42]171172173def test_pass_obj(runner):174 @click.group()175 @click.pass_context176 def cli(ctx):177 ctx.obj = 'test'178179 @cli.command()180 @click.pass_obj181 def test(obj):182 click.echo(obj)183184 result = runner.invoke(cli, ['test'])185 assert not result.exception186 assert result.output == 'test\n'
modified:CHANGES+7 added
1 unmodified line
2===============34This contains all major version changes between Click releases.56Version 5.17-----------89(bugfix release, release date to be determined)1011- Fix a bug in `pass_obj` that would accidentally pass the context too.1213Version 5.014-----------1516(codename "tok tok", released on 16th August 2015)
243 unmodified lines