autoflake icon indicating copy to clipboard operation
autoflake copied to clipboard

a ‘pass’ statement in an intentionally empty function with a docstring is not useless

Open wbolster opened this issue 4 years ago • 6 comments

not all pass statements occurring in places where they are not technically required are useless.

consider for instance a command line app written using the popular click library, featuring some subcommands (see docs). for the sake of argument, consider a hypothetical git-like application supporting a command like git remote add, which would look like this:

@click.group()
def remote_group(...):
    pass

@remote_group.command():
def add(...):
    ...  # implementation goes here

now, let's add docstrings, which in the case of click, also appear in --help output:

@click.group()
def remote_group(...):
    """
    Manage set of tracked repositories.
    """
    pass

@remote_group.command()
def add(...):
    """
    Add a new remote.
    """
    ...  # implementation goes here

technically, the presence of a docstring means the pass is not needed to make it valid python. but the pass shows the intent to the reader. without it i would immediately wonder if someone forget to write some code, or if some git merge went wrong. in this specific example, click actually supports shared code in functions decorated with click.group(). the presence of pass signals that this function does not need that, and is intended as an empty body.

so, in short: not all pass statements that are strictly technically unnecessary are to be considered ‘useless’.

my suggestion would be to treat the ‘function body, with only a docstring and a pass statement’ as an extra special case, and always leave the pass statements in place.

wbolster avatar Jul 30 '20 14:07 wbolster

I just came across this tool and was very exited. But this is a showstopper.

Can you make it configurable to remove 'useless' pass statements or not? If I run autoflake with --remove-all-unused-imports I expect to not make any other changes except to the imports.

grthr avatar Mar 22 '21 15:03 grthr

This also happens if you use methods decorated with @abc.abstractmethod: they will typically have a docstring but no implementation.

kamalmarhubi avatar Mar 28 '21 15:03 kamalmarhubi

Sometimes people use ellipsis ... in empty functions to mark it as implementation missing, though not sure is that more of a hack, as it has varying semantics depending on where it's used.

Still, I actually prefer using ellipsis in these cases, as it stands out more clear that method/function doesn't have implementation, and autoflake doesn't seem to complain about it either.

edvardm avatar May 07 '21 08:05 edvardm

... reads as a placeholder for code that needs to be written, or that it's a ~‘template’~ ‘blueprint’, e.g. in .pyi stub files.

pass in an exception class body means it's intentionally empty. another use case is the click example and its rationale in my earlier comment.

wbolster avatar May 07 '21 13:05 wbolster

Good explanation, thanks!

edvardm avatar May 10 '21 16:05 edvardm

Will this be fixed?

PhilippSelenium avatar Nov 24 '21 13:11 PhilippSelenium