Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unexpected keyword argument in python click

@click.group(context_settings=dict(help_option_names=['-h', '--help']))
def plot_glm():
    pass

@plot_glm.command()
@click.argument('path_nc')
@click.argument('out_path')
@click.argument('var_name')
@click.option('--xaxis_min', default=0.0, help='')
@click.option('--xaxis_max', default=1.1, help='')
@click.option('--xaxis_step', default=0.1, help='')
@click.option('--annotate_date', help='')
@click.option('--yr', default=0, help='')
@click.option('--date', default=-1, help='')
@click.option('--xlabel', default='', help='')
@click.option('--title', default='', help='')
@click.option('--tme_name', default='time', help='')
@click.option('--show_plot', help='')
@click.option('--any_time_data', help='')
@click.option('--format', default='%.2f', help='')
@click.option('--land_bg', help='')
@click.option('--cmap', default=plt.cm.RdBu, help='')
@click.option('--grid', help='')
@click.option('--fill_mask', help='')
def plot_map_from_nc(path_nc, out_path, var_name, xaxis_min=0.0, xaxis_max=1.1, xaxis_step=0.1,
                     annotate_date=False, yr=0, date=-1, xlabel='', title='', tme_name='time', show_plot=False,
                     any_time_data=True, format='%.2f', land_bg=True, cmap=plt.cm.RdBu, grid=False, fill_mask=False)

if __name__ == '__main__':
    plot_glm()

I get this error when using python click library (python version 2.7.11, windows 10, click version 6.6):

    ctx = Context(self, info_name=info_name, parent=parent, **extra)
TypeError: __init__() got an unexpected keyword argument 'any_time_data'

What can I do to fix this error?

like image 428
user308827 Avatar asked May 05 '16 02:05

user308827


1 Answers

It seems that you try to call plot_map_from_nc or plot_glm with actual arguments somewhere in your code like this:

plot_map_from_nc(any_time_data=False)
plot_glm(any_time_data=False)

which will generate the same error message that you got.

  File "testClick.py", line 39, in <module>
    plot_glm(any_time_data=False)
  File "c:\winPython\python-2.7.10.amd64\lib\site-packages\click\core.py", line 716, in __call__
    return self.main(*args, **kwargs)
  File "c:\winPython\python-2.7.10.amd64\lib\site-packages\click\core.py", line 695, in main
    with self.make_context(prog_name, args, **extra) as ctx:
  File "c:\winPython\python-2.7.10.amd64\lib\site-packages\click\core.py", line 618, in make_context
    ctx = Context(self, info_name=info_name, parent=parent, **extra)
TypeError: __init__() got an unexpected keyword argument 'any_time_data'

Reason for the error

That is because plot_map_from_nc and plot_glm are not normal functions after those click decorators. They are callable objects, whose signature becomes

plot_map_from_nc(args=None, prog_name=None, complete_var=None, standalone_mode=True, **extra)

The type of plot_map_from_nc is click.core.Command and every arguments passed to it will be redirected to click.core.Command.main()

Solution

The correct way to invoke these callable objects is

plot_map_from_nc(sys.argv[1:]) # or
plot_map_from_nc()

If you want to use plot_map_from_nc normally in your code, define it with a different name:

def __plot_map_from_nc__(... , any_time_data=True, ...):
    do_your_job_here
# create an alias
plot_map_from_nc = __plot_map_from_nc__ 
# pass this alias to click
@plot_glm.command()
@click.argument('path_nc') # ...
@click.option('--xaxis_min', default=0.0, help='') # ...
plot_map_from_nc

# Now  plot_map_from_nc becomes a   'click.core.Command'   object  while
# __plot_map_from_nc__ is still a normal function which can be invoke as
__plot_map_from_nc__(... , any_time_data=True, ...)
like image 99
gdlmx Avatar answered Sep 20 '22 03:09

gdlmx