Fix warnings in dial.py
authorChristian Herdtweck <christian.herdtweck@intra2net.com>
Tue, 3 Mar 2026 09:55:53 +0000 (10:55 +0100)
committerChristian Herdtweck <christian.herdtweck@intra2net.com>
Tue, 3 Mar 2026 10:25:27 +0000 (11:25 +0100)
Building dist showed a syntax warning in dial.py due to missing "r" prefix.
Remove out-dated treatment of missing ipaddress module (available since
py3.3)

src/dial.py

index 35144f2..f9aad32 100644 (file)
@@ -48,19 +48,13 @@ completion timeout is currently 10 seconds (see the definition of
 
 import re
 import io
-import time
 import logging
+import ipaddress
 log = logging.getLogger('pyi2ncommon.dial')
 
 from . import cnfvar
 from . import sysmisc
 
-HAVE_IPADDRESS = True
-try:
-    import ipaddress
-except ImportError:  # guest
-    HAVE_IPADDRESS = False
-
 __all__ = (
     "arnied_dial_hangup", "arnied_dial_doc", "arnied_dial_permanent", "dialout", "get_wan_address", "DIALOUT_MODE_PERMANENT", "DIALOUT_MODE_MANUAL", "DIALOUT_MODE_DEFAULT", "DIALOUT_MODE_BY_NAME"
 )
@@ -73,7 +67,7 @@ DIALOUT_MODE_CNF = {DIALOUT_MODE_PERMANENT: "ONLINE", DIALOUT_MODE_MANUAL: "MANU
 
 # compiling this regex needs the provider id and is postponed due to
 # the peculiar implementation of the connd online condition
-NEEDLE_MEMO = "  \[%s\] :(.*connected online fsm<online>.*)"
+NEEDLE_MEMO = r"  \[%s\] :(.*connected online fsm<online>.*)"
 NEEDLE_OFFLINE = re.compile("connection map:\nend of connection map")
 
 DIALTOOLS_HANGUP_BIN = "/usr/intranator/bin/hangup"
@@ -109,7 +103,7 @@ def arnied_dial_hangup(block=False):
     """
     log.debug("requested arnied_dial_hangup%s",
               " (blocking)" if block else "")
-    if block is False:
+    if not block:
         succ, _, _ = sysmisc.run_cmd_with_pipe([DIALTOOLS_HANGUP_BIN])
         return sysmisc.RUN_RESULT_OK if succ is True else sysmisc.RUN_RESULT_FAIL
 
@@ -132,7 +126,7 @@ def arnied_dial_doc(prid="P1", block=False):
     :rtype:         int (dial result as above)
     """
     log.debug("requested arnied_dial_doc%s", " (blocking)" if block else "")
-    if block is False:
+    if not block:
         succ, _, _ = sysmisc.run_cmd_with_pipe([DIALTOOLS_DOC_BIN, prid])
         return sysmisc.RUN_RESULT_OK if succ is True else sysmisc.RUN_RESULT_FAIL
     res, err = sysmisc.cmd_block_till([DIALTOOLS_DOC_BIN, prid],
@@ -169,7 +163,7 @@ def arnied_dial_permanent(prid="P1", block=False):
         store.commit(cnf)
         return True, "", None
 
-    if block is False:
+    if not block:
         succ, out, _ = aux()
         return sysmisc.RUN_RESULT_OK if succ is True else sysmisc.RUN_RESULT_FAIL
 
@@ -196,12 +190,12 @@ def dialout(mode=DIALOUT_MODE_DEFAULT, prid="P1", block=True):
     log.info("go online with provider")
 
     dmode = None
-    if isinstance(mode, int) is True:
+    if isinstance(mode, int):
         dmode = mode
-    elif isinstance(mode, str) is True:
+    elif isinstance(mode, str):
         try:
             dmode = DIALOUT_MODE_BY_NAME[mode]
-        except Exception:
+        except KeyError:
             log.error("invalid online mode name ā€œ%sā€ requested" % mode)
             pass
 
@@ -227,9 +221,8 @@ def get_wan_address(vm=None):
     :type      vm:  virttest.qemu_vm.VM | None
 
     :returns:       The IPv4 address. For correctness, it will use the
-                    ipaddress module if available. Otherwise it falls back
-                    on untyped data.
-    :rtype:         None | (ipaddress.IPv4Address | str)
+                    ipaddress module
+    :rtype:         None | ipaddress.IPv4Address
     """
     log.info("query current lease")
     if vm is None:
@@ -246,8 +239,5 @@ def get_wan_address(vm=None):
             break
         if l.find("connected online fsm<online> IP:") != -1:
             addr = l[l.find("IP:")+3:l.find(" )\n")]  # beurk
-            if HAVE_IPADDRESS is True:
-                return ipaddress.IPv4Address(str(addr))
-            else:
-                return addr
+            return ipaddress.IPv4Address(str(addr))
     return None