trace: avoid "is" with a literal Python 3.8 warnings
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
@@ -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
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.
Reported-by Daniel P. Berrangé <[email protected]>Signed-off-by Stefan Hajnoczi <[email protected]>--- 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 :|
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
Reported-by Daniel P. Berrangé <[email protected]>Signed-off-by Stefan Hajnoczi <[email protected]>
Quoted 24 lines
--- scripts/tracetool/__init__.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)scripts/tracetool/__init__.py +2
-2@@ -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):
Stefan Hajnoczi
AppliedQuoted 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.
Reported-by Daniel P. Berrangé <[email protected]>Signed-off-by Stefan Hajnoczi <[email protected]>--- 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
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.
Reported-by Daniel P. Berrangé <[email protected]>Signed-off-by Stefan Hajnoczi <[email protected]>--- scripts/tracetool/__init__.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)scripts/tracetool/__init__.py +0
-1@@ -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):