pythonpylintoptional-parametersunused-variables

Marking unused parameters in functions passed as arguments


I have a higher order function:

def mymap(f,...):
    ...
    x = f(a, logger)
    ...

and I need to pass to it f that needs only one argument:

def bigfun(...):
    ...
    def f(a, logger):
        return a
    mymap(f, ...)
    ...

the code above works fine, but pylint complains about

Unused argument 'logger' (unused-argument)

If I define f using _:

def bigfun(...):
    ...
    def f(a, _logger):
        return a
    mymap(f, ...)
    ...

the code breaks with

TypeError: bigfun.<locals>.f() got an unexpected keyword argument 'logger'

I can, of course, add # pylint: disable=unused-argument to f, but is this TRT?


Solution

  • It's not shown in the code you gave but in order to raise "TypeError: bigfun..f() got an unexpected keyword argument 'logger'", mymap(f, ...) must have been mymap(f, logger=my_logger) instead. If you change your function signature from logger to _logger, then the calling code with keyword args needs to use the new name (mymap(f, _logger = my_logger)) or become positional (mymap(f, my_logger)).