Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ANN001 for unused function argument #14663

Closed
towiat opened this issue Nov 28, 2024 · 6 comments · Fixed by #14888
Closed

ANN001 for unused function argument #14663

towiat opened this issue Nov 28, 2024 · 6 comments · Fixed by #14888
Labels
rule Implementing or modifying a lint rule

Comments

@towiat
Copy link

towiat commented Nov 28, 2024

Given the setup

[tool.ruff]
select = [
    "ANN001", # missing-type-function-argument
    "ARG001", # unused-function-argument
]

and the function

def callback(_unused, foo: str) -> None:
    print(foo)

ruff (version 0.8.0) does not raise ARG001 since the argument name _unused matches the dummy-variable-rgx pattern.

It does, however, raise ANN001 even though a type annotation for an unused argument doesn't make a lot of sense.

Shouldn't the check for ANN001 also take the dummy-variable-rgx pattern into account?

@MichaReiser
Copy link
Member

Thanks for opening this issue. This is an interesting question and we may have to consider this in a more complete example. E.g is callback overriding some method or why is the first argument unused?

I agree that the type annotation seems rather useless for the callback's body. However, annotating the parameter type is important to verify that the function is called with the right arguments. So there's value in having the annotation, but only if the signature can't be derived from e.g. a parent method.

@MichaReiser MichaReiser added the rule Implementing or modifying a lint rule label Nov 29, 2024
@towiat
Copy link
Author

towiat commented Nov 29, 2024

Thanks for looking into this. I'm also not completely sure, but here is some context:

I ran into this while writing a callback for a click option like this (snippet from the click documentation):

def print_version(ctx, param, value):
    if not value or ctx.resilient_parsing:
        return
    click.echo('Version 1.0')
    ctx.exit()

@click.command()
@click.option('--version', is_flag=True, callback=print_version,
              expose_value=False, is_eager=True)
def hello():
    click.echo('Hello World!')

Note that click calls the callback with positional parameters, so it doesn't care about the names.

In my use case I only need the value argument which leaves me with the following choices to annotate the callback:

def callback(ctx: click.Context, param: click.Parameter, value: str):    # raises ARG001
    do_something_with(value)

def callback(_ctx, _param, value: str):                                  # raises ANN001
    do_something_with(value)

def callback(_ctx: click.Context, _param: click.Parameter, value: str):  # works, but... hmmm
    do_something_with(value)

Pyright has a reportMissingParameterType check which has an exemption for unused arguments, although it only considers single underscore _ arguments as unused. See also this Pyright issue.

@mnesarco
Copy link

Hello I have the same issue, specially in methods that override parent methods, usually some arguments are unused in subclasses but they need to keep the same signature so I use single underscore prefix to mark as unused, but ANN001 keeps kiking my ass 😅

@MichaReiser
Copy link
Member

MichaReiser commented Dec 10, 2024

Oh, I just realized that ANN01 has a setting that disables the ANN rules for dummy variables.

[tool.ruff.lint.flake8-annotations]
suppress-dummy-args = true

I think that should solve both your use cases.

@towiat
Copy link
Author

towiat commented Dec 10, 2024

Huh, this works as described and does indeed solve my issue.

Since I somehow overlooked this despite studying the documentation (I swear!), would it make sense to mention this setting in the description of the affected rules (just like dummy-variable-rgx is mentioned in the ARG001 documentation)?

Other than that, this can be closed from my side. Thanks!

@MichaReiser
Copy link
Member

That's a good call out. Let me add it real quick.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
rule Implementing or modifying a lint rule
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants