[PATCH 2/4] selftests: openvswitch: fix misc pylint warnings in ovs-dpctl.py
From: Minxi Hou
Date: Sat Sep 05 2026 - 06:42:22 EST
Fix miscellaneous pylint warnings with no behavior change:
- W0611: remove unused import struct
- W0702: replace bare except with except Exception
- C0325: remove superfluous parentheses after return (3)
- R1705: remove unnecessary elif after return (3)
- W0108: replace unnecessary lambda with int
- R1714: merge comparisons with in operator
- W0719: replace raise Exception with raise ValueError
- C1802: use implicit boolean test instead of len()
- C0121: use is None instead of == None
- R1719: simplify if-expression to bool test
- R1703: simplify if/else to assignment expression
- W0612: remove unused variables (keybits, maskbits, lst)
- replace unused loop variable with underscore
Signed-off-by: Minxi Hou <houminxi@xxxxxxxxx>
---
base-commit: 9eab111e765729e93087ff86a2ec9b2ae42d0fa5
.../selftests/net/openvswitch/ovs-dpctl.py | 44 ++++++++-----------
1 file changed, 19 insertions(+), 25 deletions(-)
diff --git a/tools/testing/selftests/net/openvswitch/ovs-dpctl.py b/tools/testing/selftests/net/openvswitch/ovs-dpctl.py
index 9cd0d8f0ab23..6a02810fe4ea 100644
--- a/tools/testing/selftests/net/openvswitch/ovs-dpctl.py
+++ b/tools/testing/selftests/net/openvswitch/ovs-dpctl.py
@@ -11,7 +11,6 @@ import logging
import math
import multiprocessing
import re
-import struct
import sys
import time
import types
@@ -125,10 +124,7 @@ def parse_flags(flag_str, flag_vals):
maskResult = int(digits, 0)
while len(flag_str) > 0 and (flag_str[0] == "+" or flag_str[0] == "-"):
- if flag_str[0] == "+":
- setFlag = True
- elif flag_str[0] == "-":
- setFlag = False
+ setFlag = flag_str[0] == "+"
flag_str = flag_str[1:]
@@ -221,10 +217,9 @@ def convert_int(size):
if not value:
return 0, 0
- elif not mask:
+ if not mask:
return int(value, 0), pow(2, size) - 1
- else:
- return int(value, 0), int(mask, 0)
+ return int(value, 0), int(mask, 0)
return convert_int_sized
@@ -705,11 +700,11 @@ class ovsactions(nla):
parsed = True
else:
actstr = actstr[len("drop"): ]
- return (totallen - len(actstr))
+ return totallen - len(actstr)
elif parse_starts_block(actstr, r"^(\d+)", False, True):
actstr, output = parse_extract_field(
- actstr, None, r"(\d+)", lambda x: int(x), False, "0"
+ actstr, None, r"(\d+)", int, False, "0"
)
self["attrs"].append(["OVS_ACTION_ATTR_OUTPUT", output])
parsed = True
@@ -761,12 +756,12 @@ class ovsactions(nla):
vid = int(v, 0)
if vid < 0 or vid > 0xFFF:
raise ValueError(
- f"push_vlan(): vid={int(vid)} out of range (0-4095)")
+ f"push_vlan(): vid={vid} out of range (0-4095)")
elif k == "pcp":
pcp = int(v, 0)
if pcp < 0 or pcp > 7:
raise ValueError(
- f"push_vlan(): pcp={int(pcp)} out of range (0-7)")
+ f"push_vlan(): pcp={pcp} out of range (0-7)")
elif k == "tpid":
tpid = int(v, 0)
if tpid < 0 or tpid > 0xFFFF:
@@ -804,7 +799,6 @@ class ovsactions(nla):
subacts = ovsactions()
actstr = actstr[len("clone("):]
parsedLen = subacts.parse(actstr)
- lst = []
self["attrs"].append(("OVS_ACTION_ATTR_CLONE", subacts))
actstr = actstr[parsedLen:]
parsed = True
@@ -960,14 +954,14 @@ class ovsactions(nla):
actstr = actstr[1:]
if len(actstr) and actstr[0] == ")":
- return (totallen - len(actstr))
+ return totallen - len(actstr)
actstr = actstr[strspn(actstr, ", ") :]
if not parsed:
raise ValueError(f"Action str: '{actstr}' not supported")
- return (totallen - len(actstr))
+ return totallen - len(actstr)
# pyroute2 resolves nla_map types via getattr(self, name).
@@ -1057,8 +1051,6 @@ class ovskey(nla):
if flowstr.startswith("("):
flowstr = flowstr[1:]
- keybits = b""
- maskbits = b""
for f in self.fields_map:
if flowstr.startswith(f[1]):
# the following assumes that the field looks
@@ -1067,7 +1059,7 @@ class ovskey(nla):
flowstr = flowstr[len(f[1]) + 1 :]
splitchar = 0
for c in flowstr:
- if c == "," or c == ")":
+ if c in (",", ")"):
break
splitchar += 1
data = flowstr[:splitchar]
@@ -1631,7 +1623,7 @@ class ovskey(nla):
for prefix, regex, typ, attr_name, mask_val, default_val, v46_flag in fields:
flowstr, value = parse_extract_field(flowstr, prefix, regex, typ, False)
if not attr_name:
- raise Exception("Bad list value in tunnel fields")
+ raise ValueError("Bad list value in tunnel fields")
if value is None and attr_name in forced_include:
value = default_val
@@ -1714,7 +1706,7 @@ class ovskey(nla):
if not noprint:
print_str += ","
- if len(flagsattrs):
+ if flagsattrs:
print_str += f"flags({'|'.join(flagsattrs)})"
print_str += ")"
return print_str
@@ -2304,7 +2296,7 @@ class OvsDatapath(GenericNetlinkSocket):
nproc = multiprocessing.cpu_count()
procarray = []
- for i in range(1, nproc):
+ for _ in range(1, nproc):
procarray += [int(p.epid)]
msg["attrs"].append(["OVS_DP_ATTR_UPCALL_PID", procarray])
msg["attrs"].append(["OVS_DP_ATTR_USER_FEATURES", dpfeatures])
@@ -2379,15 +2371,17 @@ class OvsVport(GenericNetlinkSocket):
def type_to_str(vport_type):
if vport_type == OvsVport.OVS_VPORT_TYPE_NETDEV:
return "netdev"
- elif vport_type == OvsVport.OVS_VPORT_TYPE_INTERNAL:
+ if vport_type == OvsVport.OVS_VPORT_TYPE_INTERNAL:
return "internal"
+
raise ValueError(f"Unknown vport type:{int(vport_type)}")
def str_to_type(vport_type):
if vport_type in ["netdev", "gre", "vxlan", "geneve"]:
return OvsVport.OVS_VPORT_TYPE_NETDEV
- elif vport_type == "internal":
+ if vport_type == "internal":
return OvsVport.OVS_VPORT_TYPE_INTERNAL
+
raise ValueError(f"Unknown vport type: '{vport_type}'")
def __init__(self, packet=OvsPacket()):
@@ -2482,7 +2476,7 @@ class OvsVport(GenericNetlinkSocket):
msg["dpifindex"] = dpindex
msg["attrs"].append(["OVS_VPORT_ATTR_NAME", vport_ifname])
- if p == None:
+ if p is None:
p = self.upcall_packet
else:
self.upcall_packet = p
@@ -3068,7 +3062,7 @@ def main(argv):
return 1
rep = ovsflow.dump(rep["dpifindex"])
for flow in rep:
- print(flow.dpstr(True if args.verbose > 0 else False))
+ print(flow.dpstr(args.verbose > 0))
elif hasattr(args, "flbr"):
rep = ovsdp.info(args.flbr, 0)
if rep is None:
--
2.55.0