[ovs-dev] [PATCH 05/11] python: Resolve pep8 comparison errors.

Russell Bryant russell at ovn.org
Tue Dec 22 17:17:27 UTC 2015


Resolve pep8 errors:

  E711 comparison to None should be 'if cond is None:'

The reason comparing against None with "is None" is preferred over
"== None" is because a class can define its own equality operator and
produce bizarre unexpected behavior.  Using "is None" has a very
explicit meaning that can not be overridden.

  E721 do not compare types, use 'isinstance()'

This one is actually a mistake by the tool in most cases.
'from ovs.db import types' looks just like types from the Python stdlib.
In those cases, use the full ovs.db.types name.  Fix one case where it
actually was types from the stdlib.

Signed-off-by: Russell Bryant <russell at ovn.org>
---
 Makefile.am              |  2 +-
 debian/ovs-monitor-ipsec |  4 ++--
 python/ovs/db/idl.py     |  2 +-
 python/ovs/db/parser.py  |  2 +-
 python/ovs/db/schema.py  | 10 ++++++----
 python/ovs/json.py       |  2 +-
 python/ovs/jsonrpc.py    |  2 +-
 tests/test-ovsdb.py      | 31 ++++++++++++++++---------------
 tests/test-reconnect.py  |  4 ++--
 vtep/ovs-vtep            |  4 ++--
 10 files changed, 33 insertions(+), 30 deletions(-)

diff --git a/Makefile.am b/Makefile.am
index 90070ca..9734b93 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -342,7 +342,7 @@ endif
 if HAVE_FLAKE8
 ALL_LOCAL += pep8-check
 pep8-check: $(FLAKE8_PYFILES)
-	if flake8 $^ --ignore=E111,E112,E113,E123,E126,E127,E128,E129,E131,E201,E203,E222,E225,E226,E231,E241,E251,E261,E262,E265,E271,E501,E502,E703,E711,E713,E721,W601; then touch $@; else exit 1; fi
+	if flake8 $^ --ignore=E111,E112,E113,E123,E126,E127,E128,E129,E131,E201,E203,E222,E225,E226,E231,E241,E251,E261,E262,E265,E271,E501,E502,E703,E713,W601; then touch $@; else exit 1; fi
 endif
 
 include $(srcdir)/manpages.mk
diff --git a/debian/ovs-monitor-ipsec b/debian/ovs-monitor-ipsec
index 50b3013..e79c755 100755
--- a/debian/ovs-monitor-ipsec
+++ b/debian/ovs-monitor-ipsec
@@ -209,9 +209,9 @@ path certificate "%s";
         if host in self.psk_hosts:
             raise error.Error("host %s already defined for psk" % host)
 
-        if vals["certificate"] == None:
+        if vals["certificate"] is None:
             raise error.Error("'certificate' not defined for %s" % host)
-        elif vals["private_key"] == None:
+        elif vals["private_key"] is None:
             # Assume the private key is stored in the same PEM file as
             # the certificate.  We make a copy of "vals" so that we don't
             # modify the original "vals", which would cause the script
diff --git a/python/ovs/db/idl.py b/python/ovs/db/idl.py
index f9fd928..1bae57d 100644
--- a/python/ovs/db/idl.py
+++ b/python/ovs/db/idl.py
@@ -179,7 +179,7 @@ class Idl(object):
             if (msg.type == ovs.jsonrpc.Message.T_NOTIFY
                 and msg.method == "update"
                 and len(msg.params) == 2
-                and msg.params[0] == None):
+                and msg.params[0] is None):
                 # Database contents changed.
                 self.__parse_update(msg.params[1])
             elif (msg.type == ovs.jsonrpc.Message.T_REPLY
diff --git a/python/ovs/db/parser.py b/python/ovs/db/parser.py
index 2556bec..81caedb 100644
--- a/python/ovs/db/parser.py
+++ b/python/ovs/db/parser.py
@@ -80,7 +80,7 @@ def is_identifier(s):
 
 
 def json_type_to_string(type_):
-    if type_ == None:
+    if type_ is None:
         return "null"
     elif type_ == bool:
         return "boolean"
diff --git a/python/ovs/db/schema.py b/python/ovs/db/schema.py
index bc86232..263907e 100644
--- a/python/ovs/db/schema.py
+++ b/python/ovs/db/schema.py
@@ -17,7 +17,7 @@ import sys
 
 from ovs.db import error
 import ovs.db.parser
-from ovs.db import types
+import ovs.db.types
 
 
 def _check_id(name, json):
@@ -101,7 +101,8 @@ class DbSchema(object):
         return DbSchema.from_json(self.to_json())
 
     def __follow_ref_table(self, column, base, base_name):
-        if not base or base.type != types.UuidType or not base.ref_table_name:
+        if (not base or base.type != ovs.db.types.UuidType
+                or not base.ref_table_name):
             return
 
         base.ref_table = self.tables.get(base.ref_table_name)
@@ -181,7 +182,7 @@ class TableSchema(object):
         indexes_json = parser.get_optional("indexes", [list], [])
         parser.finish()
 
-        if max_rows == None:
+        if max_rows is None:
             max_rows = sys.maxint
         elif max_rows <= 0:
             raise error.Error("maxRows must be at least 1", json)
@@ -257,7 +258,8 @@ class ColumnSchema(object):
         parser = ovs.db.parser.Parser(json, "schema for column %s" % name)
         mutable = parser.get_optional("mutable", [bool], True)
         ephemeral = parser.get_optional("ephemeral", [bool], False)
-        type_ = types.Type.from_json(parser.get("type", [dict, str, unicode]))
+        type_ = ovs.db.types.Type.from_json(parser.get("type",
+                                                       [dict, str, unicode]))
         parser.finish()
 
         return ColumnSchema(name, mutable, not ephemeral, type_)
diff --git a/python/ovs/json.py b/python/ovs/json.py
index d329ee4..58daf9e 100644
--- a/python/ovs/json.py
+++ b/python/ovs/json.py
@@ -579,7 +579,7 @@ class Parser(object):
         elif self.parse_state != Parser.__parse_end:
             self.__error("unexpected end of input")
 
-        if self.error == None:
+        if self.error is None:
             assert len(self.stack) == 1
             return self.stack.pop()
         else:
diff --git a/python/ovs/jsonrpc.py b/python/ovs/jsonrpc.py
index d54d74b..f2873a4 100644
--- a/python/ovs/jsonrpc.py
+++ b/python/ovs/jsonrpc.py
@@ -490,7 +490,7 @@ class Session(object):
                 request.id = "echo"
                 self.rpc.send(request)
         else:
-            assert action == None
+            assert action is None
 
     def wait(self, poller):
         if self.rpc is not None:
diff --git a/tests/test-ovsdb.py b/tests/test-ovsdb.py
index 6ddc6b4..f121a0c 100644
--- a/tests/test-ovsdb.py
+++ b/tests/test-ovsdb.py
@@ -23,7 +23,7 @@ from ovs.db import error
 import ovs.db.idl
 import ovs.db.schema
 from ovs.db import data
-from ovs.db import types
+import ovs.db.types
 import ovs.ovsuuid
 import ovs.poller
 import ovs.util
@@ -37,8 +37,8 @@ def unbox_json(json):
 
 
 def do_default_atoms():
-    for type_ in types.ATOMIC_TYPES:
-        if type_ == types.VoidType:
+    for type_ in ovs.db.types.ATOMIC_TYPES:
+        if type_ == ovs.db.types.VoidType:
             continue
 
         sys.stdout.write("%s: " % type_.to_string())
@@ -54,15 +54,16 @@ def do_default_atoms():
 def do_default_data():
     any_errors = False
     for n_min in 0, 1:
-        for key in types.ATOMIC_TYPES:
-            if key == types.VoidType:
+        for key in ovs.db.types.ATOMIC_TYPES:
+            if key == ovs.db.types.VoidType:
                 continue
-            for value in types.ATOMIC_TYPES:
-                if value == types.VoidType:
+            for value in ovs.db.types.ATOMIC_TYPES:
+                if value == ovs.db.types.VoidType:
                     valueBase = None
                 else:
-                    valueBase = types.BaseType(value)
-                type_ = types.Type(types.BaseType(key), valueBase, n_min, 1)
+                    valueBase = ovs.db.types.BaseType(value)
+                type_ = ovs.db.types.Type(ovs.db.types.BaseType(key),
+                                          valueBase, n_min, 1)
                 assert type_.is_valid()
 
                 sys.stdout.write("key %s, value %s, n_min %d: "
@@ -80,25 +81,25 @@ def do_default_data():
 
 def do_parse_atomic_type(type_string):
     type_json = unbox_json(ovs.json.from_string(type_string))
-    atomic_type = types.AtomicType.from_json(type_json)
+    atomic_type = ovs.db.types.AtomicType.from_json(type_json)
     print ovs.json.to_string(atomic_type.to_json(), sort_keys=True)
 
 
 def do_parse_base_type(type_string):
     type_json = unbox_json(ovs.json.from_string(type_string))
-    base_type = types.BaseType.from_json(type_json)
+    base_type = ovs.db.types.BaseType.from_json(type_json)
     print ovs.json.to_string(base_type.to_json(), sort_keys=True)
 
 
 def do_parse_type(type_string):
     type_json = unbox_json(ovs.json.from_string(type_string))
-    type_ = types.Type.from_json(type_json)
+    type_ = ovs.db.types.Type.from_json(type_json)
     print ovs.json.to_string(type_.to_json(), sort_keys=True)
 
 
 def do_parse_atoms(type_string, *atom_strings):
     type_json = unbox_json(ovs.json.from_string(type_string))
-    base = types.BaseType.from_json(type_json)
+    base = ovs.db.types.BaseType.from_json(type_json)
     for atom_string in atom_strings:
         atom_json = unbox_json(ovs.json.from_string(atom_string))
         try:
@@ -110,7 +111,7 @@ def do_parse_atoms(type_string, *atom_strings):
 
 def do_parse_data(type_string, *data_strings):
     type_json = unbox_json(ovs.json.from_string(type_string))
-    type_ = types.Type.from_json(type_json)
+    type_ = ovs.db.types.Type.from_json(type_json)
     for datum_string in data_strings:
         datum_json = unbox_json(ovs.json.from_string(datum_string))
         datum = data.Datum.from_json(type_, datum_json)
@@ -119,7 +120,7 @@ def do_parse_data(type_string, *data_strings):
 
 def do_sort_atoms(type_string, atom_strings):
     type_json = unbox_json(ovs.json.from_string(type_string))
-    base = types.BaseType.from_json(type_json)
+    base = ovs.db.types.BaseType.from_json(type_json)
     atoms = [data.Atom.from_json(base, atom_json)
              for atom_json in unbox_json(ovs.json.from_string(atom_strings))]
     print ovs.json.to_string([data.Atom.to_json(atom)
diff --git a/tests/test-reconnect.py b/tests/test-reconnect.py
index c478c2a..dbe266a 100644
--- a/tests/test-reconnect.py
+++ b/tests/test-reconnect.py
@@ -130,7 +130,7 @@ def diff_stats(old, new, delta):
         print("  %sconnected" % negate)
 
     if (old.last_connected != new.last_connected or
-        (new.msec_since_connect != None and
+        (new.msec_since_connect is not None and
          old.msec_since_connect != new.msec_since_connect - delta) or
         (old.total_connected_duration != new.total_connected_duration - delta
             and not (old.total_connected_duration == 0 and
@@ -139,7 +139,7 @@ def diff_stats(old, new, delta):
               % (new.msec_since_connect, new.total_connected_duration))
 
     if (old.last_disconnected != new.last_disconnected or
-        (new.msec_since_disconnect != None and
+        (new.msec_since_disconnect is not None and
          old.msec_since_disconnect != new.msec_since_disconnect - delta)):
         print("  disconnected at %d ms (%d ms ago)"
               % (new.last_disconnected, new.msec_since_disconnect))
diff --git a/vtep/ovs-vtep b/vtep/ovs-vtep
index 1811e29..47ef65c 100755
--- a/vtep/ovs-vtep
+++ b/vtep/ovs-vtep
@@ -53,7 +53,7 @@ bfd_ref = {}
 def call_prog(prog, args_list):
     cmd = [prog, "-vconsole:off"] + args_list
     output = subprocess.Popen(cmd, stdout=subprocess.PIPE).communicate()
-    if len(output) == 0 or output[0] == None:
+    if len(output) == 0 or output[0] is None:
         output = ""
     else:
         output = output[0].strip()
@@ -100,7 +100,7 @@ class Logical_Switch(object):
         column = vtep_ctl("--columns=tunnel_key find logical_switch "
                               "name=%s" % self.name)
         tunnel_key = column.partition(":")[2].strip()
-        if (tunnel_key and type(eval(tunnel_key)) == types.IntType):
+        if tunnel_key and isinstance(eval(tunnel_key), types.IntType):
             self.tunnel_key = tunnel_key
             vlog.info("using tunnel key %s in %s"
                       % (self.tunnel_key, self.name))
-- 
2.5.0




More information about the dev mailing list