question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

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.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:7
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

5reactions
grthrcommented, 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.

4reactions
wbolstercommented, 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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Empty class with comment same as pass? - python
A docstring is a string literal that occurs as the first statement in a module, function, class, or method definition.
Read more >
The pass Statement: How to Do Nothing in Python
In Python, the pass keyword is an entire statement in itself. This statement doesn't do anything: it's discarded during the byte-compile phase.
Read more >
New check: useless-pass · Issue #2208 · PyCQA/pylint - GitHub
The docstring is used to describe a method. Its no-op behavior is coincidental. pass is used to explicitly specify no-ops. That's a fair...
Read more >
Bountysource
a 'pass ' statement in an intentionally empty function with a docstring is not useless.
Read more >
Python pass (Do Nothing): When And How To Use
Python has a special keyword called pass . The Python pass keyword tells Python to do nothing at all. In other words: just...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found