Replace type() equality checks with isinstance

This commit is contained in:
Jon Dufresne 2016-10-30 17:43:32 -07:00
parent bb2f479c62
commit e44bb42ae9
11 changed files with 20 additions and 22 deletions

View File

@ -879,7 +879,7 @@ class Image(object):
trns_im = Image()._new(core.new(self.mode, (1, 1))) trns_im = Image()._new(core.new(self.mode, (1, 1)))
if self.mode == 'P': if self.mode == 'P':
trns_im.putpalette(self.palette) trns_im.putpalette(self.palette)
if type(t) == tuple: if isinstance(t, tuple):
try: try:
t = trns_im.palette.getcolor(t) t = trns_im.palette.getcolor(t)
except: except:

View File

@ -208,12 +208,12 @@ class ImageDraw(object):
def _multiline_check(self, text): def _multiline_check(self, text):
"""Draw text.""" """Draw text."""
split_character = "\n" if isinstance(text, type("")) else b"\n" split_character = "\n" if isinstance(text, str) else b"\n"
return split_character in text return split_character in text
def _multiline_split(self, text): def _multiline_split(self, text):
split_character = "\n" if isinstance(text, type("")) else b"\n" split_character = "\n" if isinstance(text, str) else b"\n"
return text.split(split_character) return text.split(split_character)

View File

@ -278,12 +278,12 @@ class IFDRational(Rational):
self._numerator = value self._numerator = value
self._val = float(1) self._val = float(1)
if type(value) == Fraction: if isinstance(value, Fraction):
self._numerator = value.numerator self._numerator = value.numerator
self._denominator = value.denominator self._denominator = value.denominator
self._val = value self._val = value
if type(value) == IFDRational: if isinstance(value, IFDRational):
self._denominator = value.denominator self._denominator = value.denominator
self._numerator = value.numerator self._numerator = value.numerator
self._val = value._val self._val = value._val
@ -294,7 +294,7 @@ class IFDRational(Rational):
return return
elif denominator == 1: elif denominator == 1:
if sys.hexversion < 0x2070000 and type(value) == float: if sys.hexversion < 0x2070000 and isinstance(value, float):
# python 2.6 is different. # python 2.6 is different.
self._val = Fraction.from_float(value) self._val = Fraction.from_float(value)
else: else:

View File

@ -17,7 +17,7 @@ def check_module(feature):
module = modules[feature] module = modules[feature]
method_to_call = None method_to_call = None
if type(module) is tuple: if isinstance(module, tuple):
module, method_to_call = module module, method_to_call = module
try: try:

View File

@ -13,8 +13,8 @@ class TestFeatures(PillowTestCase):
self.assertTrue(features.check_codec(feature) in [True, False]) self.assertTrue(features.check_codec(feature) in [True, False])
def test_supported_features(self): def test_supported_features(self):
self.assertTrue(type(features.get_supported_modules()) is list) self.assertIsInstance(features.get_supported_modules(), list)
self.assertTrue(type(features.get_supported_codecs()) is list) self.assertIsInstance(features.get_supported_codecs(), list)
def test_unsupported_codec(self): def test_unsupported_codec(self):
# Arrange # Arrange

View File

@ -392,7 +392,7 @@ class TestFilePng(PillowTestCase):
info = PngImagePlugin.PngInfo() info = PngImagePlugin.PngInfo()
info.add_text("Text", "Ascii") info.add_text("Text", "Ascii")
im = roundtrip(im, pnginfo=info) im = roundtrip(im, pnginfo=info)
self.assertEqual(type(im.info["Text"]), str) self.assertIsInstance(im.info["Text"], str)
def test_unicode_text(self): def test_unicode_text(self):
# Check preservation of non-ASCII characters on Python3 # Check preservation of non-ASCII characters on Python3

View File

@ -123,10 +123,9 @@ class TestFileTiffMetadata(PillowTestCase):
reloaded = loaded.tag_v2.named() reloaded = loaded.tag_v2.named()
for k, v in original.items(): for k, v in original.items():
if type(v) == IFDRational: if isinstance(v, IFDRational):
original[k] = IFDRational(*_limit_rational(v, 2**31)) original[k] = IFDRational(*_limit_rational(v, 2**31))
if type(v) == tuple and \ if isinstance(v, tuple) and isinstance(v[0], IFDRational):
type(v[0]) == IFDRational:
original[k] = tuple([IFDRational( original[k] = tuple([IFDRational(
*_limit_rational(elt, 2**31)) for elt in v]) *_limit_rational(elt, 2**31)) for elt in v])
@ -136,8 +135,8 @@ class TestFileTiffMetadata(PillowTestCase):
for tag, value in reloaded.items(): for tag, value in reloaded.items():
if tag in ignored: if tag in ignored:
continue continue
if (type(original[tag]) == tuple if (isinstance(original[tag], tuple)
and type(original[tag][0]) == IFDRational): and isinstance(original[tag][0], IFDRational)):
# Need to compare element by element in the tuple, # Need to compare element by element in the tuple,
# not comparing tuples of object references # not comparing tuples of object references
self.assert_deep_equal(original[tag], self.assert_deep_equal(original[tag],
@ -175,7 +174,7 @@ class TestFileTiffMetadata(PillowTestCase):
im.save(out) im.save(out)
reloaded = Image.open(out) reloaded = Image.open(out)
self.assert_(type(im.info['icc_profile']) is not tuple) self.assertNotIsInstance(im.info['icc_profile'], tuple)
self.assertEqual(im.info['icc_profile'], reloaded.info['icc_profile']) self.assertEqual(im.info['icc_profile'], reloaded.info['icc_profile'])
def test_iccprofile_binary(self): def test_iccprofile_binary(self):

View File

@ -249,7 +249,7 @@ class TestImage(PillowTestCase):
self.assertTrue(Image.new('RGB', (1,1))) self.assertTrue(Image.new('RGB', (1,1)))
# Should pass lists too # Should pass lists too
i = Image.new('RGB', [1,1]) i = Image.new('RGB', [1,1])
self.assertEqual(type(i.size), tuple) self.assertIsInstance(i.size, tuple)
def test_storage_neg(self): def test_storage_neg(self):
# Storage.c accepted negative values for xsize, ysize. Was # Storage.c accepted negative values for xsize, ysize. Was

View File

@ -9,7 +9,7 @@ def pixel(im):
if hasattr(im, "im"): if hasattr(im, "im"):
return "%s %r" % (im.mode, im.getpixel((0, 0))) return "%s %r" % (im.mode, im.getpixel((0, 0)))
else: else:
if isinstance(im, type(0)): if isinstance(im, int):
return int(im) # hack to deal with booleans return int(im) # hack to deal with booleans
print(im) print(im)

View File

@ -106,9 +106,8 @@ class TestOleFileIo(PillowTestCase):
mtime = root_entry.getmtime() mtime = root_entry.getmtime()
# Assert # Assert
self.assertIsInstance(ctime, type(None)) self.assertIsNone(ctime)
self.assertIsInstance(mtime, datetime.datetime) self.assertIsInstance(mtime, datetime.datetime)
self.assertEqual(ctime, None)
self.assertEqual(mtime.year, 2014) self.assertEqual(mtime.year, 2014)
ole.close() ole.close()

View File

@ -189,7 +189,7 @@ class pil_build_ext(build_ext):
for root in (JPEG_ROOT, JPEG2K_ROOT, TIFF_ROOT, ZLIB_ROOT, for root in (JPEG_ROOT, JPEG2K_ROOT, TIFF_ROOT, ZLIB_ROOT,
FREETYPE_ROOT, LCMS_ROOT, IMAGEQUANT_ROOT): FREETYPE_ROOT, LCMS_ROOT, IMAGEQUANT_ROOT):
if isinstance(root, type(())): if isinstance(root, tuple):
lib_root, include_root = root lib_root, include_root = root
else: else:
lib_root = include_root = root lib_root = include_root = root