PyCQA / PyCQA/pycodestyle

Consistent use of "return None" in functions

Open
#399 4 comments 6 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

feature needs patch
Dominant language
Python
Stars
5.2k
Forks
754
PR merge metrics
No merged PRs in 30d

Description

Could you add this rule to pep8? I just updated the official PEP 8 to include this (I'm surprised it wasn't already there):

  • Be consistent in return statements. Either all return statements in a
    function should return an expression, or none of them should. If any return
    statement returns an expression, any return statements where no value is
    returned should explicitly state this as return None, and an explicit
    return statement should be present at the end of the function (if
    reachable).
   Yes:

   def foo(x):
       if x >= 0:
           return math.sqrt(x)
       else:
           return None

   def bar(x):
       if x < 0:
           return None
       return math.sqrt(x)

   No:

   def foo(x):
       if x >= 0:
           return math.sqrt(x)

   def bar(x):
       if x < 0:
           return
       return math.sqrt(x)

https://mail.python.org/pipermail/python-dev/2015-April/139054.html

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by locating the pycodestyle implementation and its tests, then compare the current behavior with the quoted PEP 8 examples. Done means the checker enforces the shown Yes/No return-statement distinctions and the relevant regression coverage passes.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
tooling
Issue type
Feature
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.