From 26bf1937c72239cc3db07e68da3e397463f919fe Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Sun, 3 Apr 2016 23:41:28 +1000 Subject: [PATCH] Added warning for deprecated as_dict method --- PIL/TiffImagePlugin.py | 5 ++--- Tests/test_file_tiff.py | 37 +++++++++++++++++++++++-------------- 2 files changed, 25 insertions(+), 17 deletions(-) diff --git a/PIL/TiffImagePlugin.py b/PIL/TiffImagePlugin.py index f899676e8..035d6f8d4 100644 --- a/PIL/TiffImagePlugin.py +++ b/PIL/TiffImagePlugin.py @@ -448,11 +448,10 @@ class ImageFileDirectory_v2(collections.MutableMapping): def as_dict(self): """Return a dictionary of the image's tags. - use `dict(ifd)` instead. - .. deprecated:: 3.0.0 """ - # FIXME Deprecate: use dict(self) + warnings.warn("as_dict() is deprecated. " + + "Please use dict(ifd) instead.", DeprecationWarning) return dict(self) def named(self): diff --git a/Tests/test_file_tiff.py b/Tests/test_file_tiff.py index 43240216e..5af7b55aa 100644 --- a/Tests/test_file_tiff.py +++ b/Tests/test_file_tiff.py @@ -244,31 +244,40 @@ class TestFileTiff(PillowTestCase): # Assert self.assertIsInstance(ret, str) - def test_as_dict(self): + def test_as_dict_deprecation(self): # Arrange filename = "Tests/images/pil136.tiff" im = Image.open(filename) + + self.assert_warning(DeprecationWarning, lambda: im.tag_v2.as_dict()) + self.assert_warning(DeprecationWarning, lambda: im.tag.as_dict()) + + def test_dict(self): + # Arrange + filename = "Tests/images/pil136.tiff" + im = Image.open(filename) + # v2 interface - self.assertEqual( - im.tag_v2.as_dict(), - {256: 55, 257: 43, 258: (8, 8, 8, 8), 259: 1, - 262: 2, 296: 2, 273: (8,), 338: (1,), 277: 4, - 279: (9460,), 282: 72.0, 283: 72.0, 284: 1}) + v2_tags = {256: 55, 257: 43, 258: (8, 8, 8, 8), 259: 1, + 262: 2, 296: 2, 273: (8,), 338: (1,), 277: 4, + 279: (9460,), 282: 72.0, 283: 72.0, 284: 1} + self.assertEqual(dict(im.tag_v2), v2_tags) + self.assertEqual(im.tag_v2.as_dict(), v2_tags) # legacy interface - self.assertEqual( - im.tag.as_dict(), - {256: (55,), 257: (43,), 258: (8, 8, 8, 8), 259: (1,), - 262: (2,), 296: (2,), 273: (8,), 338: (1,), 277: (4,), - 279: (9460,), 282: ((720000, 10000),), - 283: ((720000, 10000),), 284: (1,)}) + legacy_tags = {256: (55,), 257: (43,), 258: (8, 8, 8, 8), 259: (1,), + 262: (2,), 296: (2,), 273: (8,), 338: (1,), 277: (4,), + 279: (9460,), 282: ((720000, 10000),), + 283: ((720000, 10000),), 284: (1,)} + self.assertEqual(dict(im.tag), legacy_tags) + self.assertEqual(im.tag.as_dict(), legacy_tags) def test__delitem__(self): filename = "Tests/images/pil136.tiff" im = Image.open(filename) - len_before = len(im.ifd.as_dict()) + len_before = len(dict(im.ifd)) del im.ifd[256] - len_after = len(im.ifd.as_dict()) + len_after = len(dict(im.ifd)) self.assertEqual(len_before, len_after + 1) def test_load_byte(self):