Why does this code work? Specifically, why can I call the run() function with no arguments when it specifically requires both count and name. The PyLinter complains about this code and says there is no function run() with 0 arguments. But then the code runs fine.
Is this because the decorators are adding an additional function of the same name "run" but without arguments?
import click
@click.command()
@click.option('--count', default=1, help='Number of greetings.')
@click.option('--name', prompt='Your name',
help='The person to greet.')
def run(count, name):
"""Simple program that greets NAME for a total of COUNT times."""
for x in range(count):
click.echo('Hello %s!' % name)
if __name__ == "__main__":
run()
The decorator syntax
@decorator
def func():
pass
is actually just syntactic sugar for:
def func():
pass
func = decorator(func)
IOW, it rebinds the function name to the return value of the decorator - which, most often, returns another function that wraps the decorated one.
So in your example, the run()
function you're calling in your main section is indeed not the run()
function you defined, but the function that has been returned by your decorators - which doesn't excpects arguments instead, since the point of the decorators is to collect and provides those arguments to your original function ;-)