git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH v2 18/21] git-p4: compare to singletons with "is" and "is not"

From
Joel Holdsworth <jholdsworth@nvidia.com>
Date
Jan 13, 2022, 13:47 UTC
Message-ID
<20220113134728.23192-19-jholdsworth@nvidia.com>
In-Reply-To
<20220113134728.23192-1-jholdsworth@nvidia.com>

PEP8 recommends that comparisons with singletons such as None should be done with "is" and "is not", and never equality operators.

This guideline is described here: https://www.python.org/dev/peps/pep-0008/#programming-recommendations

Signed-off-by: Joel Holdsworth <jholdsworth@nvidia.com>
---
The previous version of this patch replaced code of the form...
if foo == None:
    ...
...with code of the following form:
if not foo:
    ...

Eric Sunshine pointed out that the latter is not necessarily functionally equivalent to the former in cases where foo is "", [], False etc.

Therefore this version of the patch instead replaces the code with the follow ing:

if foo is None:
    ...
 git-p4.py | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
diff --git a/git-p4.py b/git-p4.py
index 1de5b1c49d..a42010fbdd 100755
--- a/git-p4.py
+++ b/git-p4.py
@@ -930,7 +930,7 @@ def p4Where(depotPath):
             if data[:space] == depotPath:
                 output = entry
                 break
-    if output == None:
+    if output is None:
         return ""
     if output["code"] == "error":
         return ""
@@ -952,7 +952,7 @@ def currentGitBranch():
 
 
 def isValidGitDir(path):
-    return git_dir(path) != None
+    return git_dir(path) is not None
 
 
 def parseRevision(ref):
@@ -4500,7 +4500,7 @@ def main():
     global verbose
     verbose = cmd.verbose
     if cmd.needsGit:
-        if cmd.gitdir == None:
+        if cmd.gitdir is None:
             cmd.gitdir = os.path.abspath(".git")
             if not isValidGitDir(cmd.gitdir):
                 # "rev-parse --git-dir" without arguments will try $PWD/.git
-- 
2.34.1
Previous: Joel HoldsworthNext: Joel Holdsworth
Message 17 of 24 in “git-p4: Various code tidy-ups”
  1. 00/21 git-p4: Various code tidy-upsJoel Holdsworth, Jan 13, 2022
  2. 01/21 git-p4: add blank lines between functions and class definitionsJoel Holdsworth, Jan 13, 2022
  3. 02/21 git-p4: remove unneeded semicolons from statementsJoel Holdsworth, Jan 13, 2022
  4. 03/21 git-p4: indent with 4-spacesJoel Holdsworth, Jan 13, 2022
  5. 04/21 git-p4: improve consistency of docstring formattingJoel Holdsworth, Jan 13, 2022
  6. 05/21 git-p4: convert descriptive class and function comments into docstringsJoel Holdsworth, Jan 13, 2022
  7. 06/21 git-p4: remove commented codeJoel Holdsworth, Jan 13, 2022
  8. 07/21 git-p4: sort and de-duplcate pylint disable listJoel Holdsworth, Jan 13, 2022
  9. 10/21 git-p4: removed brackets when assigning multiple return valuesJoel Holdsworth, Jan 13, 2022
  10. 08/21 git-p4: remove padding from lists, tuples and function argumentsJoel Holdsworth, Jan 13, 2022
  11. 11/21 git-p4: place a single space after every commaJoel Holdsworth, Jan 13, 2022
  12. 09/21 git-p4: remove spaces around default argumentsJoel Holdsworth, Jan 13, 2022
  13. 12/21 git-p4: remove extraneous spaces before function argumentsJoel Holdsworth, Jan 13, 2022
  14. 13/21 git-p4: remove redundant backslash-continuations inside bracketsJoel Holdsworth, Jan 13, 2022
  15. 14/21 git-p4: remove spaces between dictionary keys and colonsJoel Holdsworth, Jan 13, 2022
  16. 15/21 git-p4: ensure every comment has a single #Joel Holdsworth, Jan 13, 2022
  17. 18/21 git-p4: compare to singletons with "is" and "is not"Joel Holdsworth, Jan 13, 2022
  18. 16/21 git-p4: ensure there is a single space around all operatorsJoel Holdsworth, Jan 13, 2022
  19. 21/21 git-p4: seperate multiple statements onto seperate linesJoel Holdsworth, Jan 13, 2022
  20. 17/21 git-p4: normalize indentation of lines in conditionalsJoel Holdsworth, Jan 13, 2022
  21. 19/21 git-p4: only seperate code blocks by a single empty lineJoel Holdsworth, Jan 13, 2022
  22. 20/21 git-p4: move inline comments to line aboveJoel Holdsworth, Jan 13, 2022
  23. Junio C HamanoJan 13, 2022
  24. Joel HoldsworthJan 14, 2022

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.