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

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

Closed
wbolster opened this issue Jul 30, 2020 · 6 comments · Fixed by #143
Closed

Comments

@wbolster
Copy link

wbolster commented Jul 30, 2020

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 wbolster changed the title not all ‘pass’ statements are useless. a ‘pass’ statement in an intentionally empty function with a docstring is not useless Jul 30, 2020
@grthr
Copy link

grthr commented Mar 22, 2021

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.

@kamalmarhubi
Copy link

kamalmarhubi commented Mar 28, 2021

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

@edvardm
Copy link
Contributor

edvardm commented May 7, 2021

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.

@wbolster
Copy link
Author

wbolster commented May 7, 2021

... 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.

@edvardm
Copy link
Contributor

edvardm commented May 10, 2021

Good explanation, thanks!

@PhilippSelenium
Copy link

Will this be fixed?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants