[PATCH 1/3] Use isinstance instead of type for doing type checks.

Chris Lumens clumens at redhat.com
Thu May 7 17:41:24 UTC 2015


pylint has gotten picky about this recently, and this is similar to other
patches I made for blivet and pyparted.  In a couple of these places, I
think we are being too clever, but that is likely a fair bit more work.
---
 pyanaconda/anaconda_argparse.py      |  3 ++-
 pyanaconda/flags.py                  |  3 ++-
 pyanaconda/iutil.py                  |  4 ++--
 pyanaconda/nm.py                     |  5 +++--
 pyanaconda/ui/tui/simpleline/base.py |  3 ++-
 scripts/makebumpver                  | 10 ++++------
 tests/pylint/markup.py               |  2 +-
 tests/pylint/pointless-override.py   |  2 +-
 8 files changed, 17 insertions(+), 15 deletions(-)

diff --git a/pyanaconda/anaconda_argparse.py b/pyanaconda/anaconda_argparse.py
index 916397f..a70365f 100644
--- a/pyanaconda/anaconda_argparse.py
+++ b/pyanaconda/anaconda_argparse.py
@@ -28,6 +28,7 @@ import os
 import sys
 import fcntl
 import termios
+import types
 import struct
 
 from argparse import ArgumentParser, ArgumentError, HelpFormatter, Namespace
@@ -178,7 +179,7 @@ class AnacondaArgumentParser(ArgumentParser):
         :rtype: Namespace
         """
         namespace = Namespace()
-        if boot_cmdline is None or type(boot_cmdline) is str:
+        if boot_cmdline is None or isinstance(boot_cmdline, types.StringType):
             bootargs = BootArgs(boot_cmdline)
         else:
             bootargs = boot_cmdline
diff --git a/pyanaconda/flags.py b/pyanaconda/flags.py
index 33d8d95..c817234 100644
--- a/pyanaconda/flags.py
+++ b/pyanaconda/flags.py
@@ -19,6 +19,7 @@
 
 import selinux
 import shlex
+import types
 import glob
 from pyanaconda.constants import SELINUX_DEFAULT, CMDLINE_APPEND
 from collections import OrderedDict
@@ -113,7 +114,7 @@ class BootArgs(OrderedDict):
         filenames can contain *, ?, and character ranges expressed with []
         """
         readfiles = []
-        if type(filenames) == str:
+        if isinstance(filenames, types.StringType):
             filenames = [filenames]
 
         # Expand any filename globs
diff --git a/pyanaconda/iutil.py b/pyanaconda/iutil.py
index 6798c41..30df3d5 100644
--- a/pyanaconda/iutil.py
+++ b/pyanaconda/iutil.py
@@ -1045,14 +1045,14 @@ _ASCIIlower_table = string.maketrans(string.ascii_uppercase, string.ascii_lowerc
 
 def _toASCII(s):
     """Convert a unicode string to ASCII"""
-    if type(s) == types.UnicodeType:
+    if isinstance(s, types.UnicodeType):
         # Decompose the string using the NFK decomposition, which in addition
         # to the canonical decomposition replaces characters based on
         # compatibility equivalence (e.g., ROMAN NUMERAL ONE has its own code
         # point but it's really just a capital I), so that we can keep as much
         # of the ASCII part of the string as possible.
         s = unicodedata.normalize('NKFD', s).encode('ascii', 'ignore')
-    elif type(s) != types.StringType:
+    elif isinstance(s, types.StringType):
         s = ''
     return s
 
diff --git a/pyanaconda/nm.py b/pyanaconda/nm.py
index 5cbf5d3..092dffd 100644
--- a/pyanaconda/nm.py
+++ b/pyanaconda/nm.py
@@ -23,6 +23,7 @@ from gi.repository import Gio, GLib
 from gi.repository import NetworkManager
 import struct
 import socket
+import types
 import logging
 log = logging.getLogger("anaconda")
 
@@ -984,9 +985,9 @@ def _gvariant_settings(settings, updated_key1, updated_key2, value, default_type
 
     if type_str is None:
         # infer the new value type for string and boolean
-        if type(value) is type(True):
+        if isinstance(value, types.BooleanType):
             type_str = 'b'
-        if type(value) is type(''):
+        elif isinstance(value, types.StringType):
             type_str = 's'
 
     if type_str is not None:
diff --git a/pyanaconda/ui/tui/simpleline/base.py b/pyanaconda/ui/tui/simpleline/base.py
index 952c5a8..991f659 100644
--- a/pyanaconda/ui/tui/simpleline/base.py
+++ b/pyanaconda/ui/tui/simpleline/base.py
@@ -25,6 +25,7 @@ import sys
 import Queue
 import getpass
 import threading
+import types
 import functools
 from pyanaconda.threads import threadMgr, AnacondaThread
 from pyanaconda.ui.communication import hubQ
@@ -580,7 +581,7 @@ class UIScreen(object):
                 w.render(self.app.width)
             if isinstance(w, Widget):
                 self._print_long_widget(w)
-            elif type(w) == str:
+            elif isinstance(w, types.StringType):
                 print(w.decode("utf-8"))
             else:
                 # not a widget, just print its unicode representation
diff --git a/scripts/makebumpver b/scripts/makebumpver
index f092bb4..558010c 100755
--- a/scripts/makebumpver
+++ b/scripts/makebumpver
@@ -146,9 +146,9 @@ class MakeBumpVer:
         ret = proc[0].strip('\n').split('\n')
 
         if len(ret) == 1 and ret[0].find('@') != -1:
-            ret = ret[0].split('@')[0]
+            ret = [ret[0].split('@')[0]]
         elif len(ret) == 1:
-            ret = ret[0]
+            ret = [ret[0]]
         else:
             ret = filter(lambda x: x != '', ret)
 
@@ -274,11 +274,9 @@ class MakeBumpVer:
             fields = line.split(' ')
             commit = fields[0]
 
-            summary = self._getCommitDetail(commit, "%s")
+            summary = self._getCommitDetail(commit, "%s")[0]
             body = self._getCommitDetail(commit, "%b")
-            if type(body) is not list:
-                body = [body]
-            author = self._getCommitDetail(commit, "%aE")
+            author = self._getCommitDetail(commit, "%aE")[0]
 
             if self.rhel:
                 rhbz = set()
diff --git a/tests/pylint/markup.py b/tests/pylint/markup.py
index 690eb13..4217472 100644
--- a/tests/pylint/markup.py
+++ b/tests/pylint/markup.py
@@ -112,7 +112,7 @@ class MarkupChecker(BaseChecker):
 
     @check_messages("invalid-markup", "invalid-markup-element", "unescaped-markup", "invalid-translated-markup", "invalid-translated-markup-element", "invalid-pango-translation", "unnecessary-markup")
     def visit_const(self, node):
-        if type(node.value) not in (types.StringType, types.UnicodeType):
+        if isinstance(node.value, types.StringType) or isinstance(node.value, types.UnicodeType):
             return
 
         if not is_markup(node.value):
diff --git a/tests/pylint/pointless-override.py b/tests/pylint/pointless-override.py
index c8d4776..d10ba42 100644
--- a/tests/pylint/pointless-override.py
+++ b/tests/pylint/pointless-override.py
@@ -153,7 +153,7 @@ class PointlessAssignment(PointlessData):
 
     @classmethod
     def check_equal(cls, node, other):
-        if type(node) != type(other):
+        if not isinstance(node, other.__class__):
             return False
         if isinstance(node, astroid.Const):
             return node.value == other.value
-- 
2.2.2



More information about the anaconda-patches mailing list