Skip to contentSkip to conversation

trace: avoid "is" with a literal Python 3.8 warnings

PATCH Applied Stefan Hajnoczi 4 participants 5 messages Oct 10, 2019 – Oct 11, 2019

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 trace: avoid "is" with a literal Python 3.8 warnings

The following statement produces a SyntaxWarning with Python 3.8:

if len(format) is 0:
scripts/tracetool/__init__.py:459: SyntaxWarning: "is" with a literal. Did you mean "=="?

Use the conventional len(x) == 0 syntax instead.

---
 scripts/tracetool/__init__.py | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
scripts/tracetool/__init__.py +2 -2
index 04279fa62e..44c118bc2a 100644
@@ -456,12 +456,12 @@ def generate(events, group, format, backends,
     import tracetool
 
     format = str(format)
-    if len(format) is 0:
+    if len(format) == 0:
         raise TracetoolError("format not set")
     if not tracetool.format.exists(format):
         raise TracetoolError("unknown format: %s" % format)
 
-    if len(backends) is 0:
+    if len(backends) == 0:
         raise TracetoolError("no backends specified")
     for backend in backends:
         if not tracetool.backend.exists(backend):
Signature · 1 line
2.21.0
Reviewed-by on v1 1/1
#1 lore raw
Quoted 5 lines · Stefan Hajnoczi

The following statement produces a SyntaxWarning with Python 3.8:

if len(format) is 0:
scripts/tracetool/__init__.py:459: SyntaxWarning: "is" with a literal. Did you mean "=="?

Quoted 7 lines

Use the conventional len(x) == 0 syntax instead.

---
 scripts/tracetool/__init__.py | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

Regards,
Daniel

Signature · 3 lines
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|
Reviewed-by on v1 1/1
#2 lore raw
In reply to Stefan Hajnoczi
Quoted 6 lines · Stefan Hajnoczi

The following statement produces a SyntaxWarning with Python 3.8:

if len(format) is 0:
scripts/tracetool/__init__.py:459: SyntaxWarning: "is" with a literal. Did you mean "=="?

Use the conventional len(x) == 0 syntax instead.

Ah this is a new change from 3.8...
https://docs.python.org/3.9/whatsnew/3.8.html

From https://bugs.python.org/issue34850

Quoted 2 lines
Quoted 24 lines
---
  scripts/tracetool/__init__.py | 4 ++--
  1 file changed, 2 insertions(+), 2 deletions(-)
scripts/tracetool/__init__.py +2 -2
index 04279fa62e..44c118bc2a 100644
@@ -456,12 +456,12 @@ def generate(events, group, format, backends,
      import tracetool
 
      format = str(format)
-    if len(format) is 0:
+    if len(format) == 0:
          raise TracetoolError("format not set")
      if not tracetool.format.exists(format):
          raise TracetoolError("unknown format: %s" % format)
 
-    if len(backends) is 0:
+    if len(backends) == 0:
          raise TracetoolError("no backends specified")
      for backend in backends:
          if not tracetool.backend.exists(backend):
author Applied
#3 lore raw
In reply to Stefan Hajnoczi
Quoted 5 lines · Stefan Hajnoczi

The following statement produces a SyntaxWarning with Python 3.8:

if len(format) is 0:
scripts/tracetool/__init__.py:459: SyntaxWarning: "is" with a literal. Did you mean "=="?

Quoted 7 lines

Use the conventional len(x) == 0 syntax instead.

---
 scripts/tracetool/__init__.py | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

Thanks, applied to my tracing tree:
https://github.com/stefanha/qemu/commits/tracing

Stefan

#4 lore raw
In reply to Stefan Hajnoczi
Quoted 22 lines · Stefan Hajnoczi

The following statement produces a SyntaxWarning with Python 3.8:

if len(format) is 0:
scripts/tracetool/__init__.py:459: SyntaxWarning: "is" with a literal. Did you mean "=="?

Use the conventional len(x) == 0 syntax instead.

---
 scripts/tracetool/__init__.py | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
scripts/tracetool/__init__.py +0 -1
index 04279fa62e..44c118bc2a 100644
@@ -456,12 +456,12 @@ def generate(events, group, format, backends,
     import tracetool
 
     format = str(format)
-    if len(format) is 0:

scripts/tracetool/__init__.py def generate(events, group, format, backends, · line 467

Quoted 10 lines
scripts/tracetool/__init__.py +2 -1
+    if len(format) == 0:
         raise TracetoolError("format not set")
     if not tracetool.format.exists(format):
         raise TracetoolError("unknown format: %s" % format)
 
-    if len(backends) is 0:
+    if len(backends) == 0:
         raise TracetoolError("no backends specified")
     for backend in backends:
         if not tracetool.backend.exists(backend):

For what it's worth, PEP 8 advises

For sequences, (strings, lists, tuples), use the fact that empty sequences are false.

Yes: if not seq:
if seq:

No: if len(seq):
if not len(seq):