Skip to content

Commit 006f2c4

Browse files
mwcraigdhomeier
authored andcommitted
Maintain 1.x behavior of in operator when key is wrong type
Closes astropy#5583
1 parent 358cff2 commit 006f2c4

2 files changed

Lines changed: 16 additions & 1 deletion

File tree

‎astropy/io/fits/hdu/hdulist.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,7 @@ def __contains__(self, item):
293293

294294
try:
295295
self._try_while_unread_hdus(self.index_of, item)
296-
except KeyError:
296+
except (KeyError, TypeError):
297297
return False
298298

299299
return True

‎astropy/io/fits/tests/test_hdulist.py‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -793,3 +793,18 @@ def test_overwrite_vs_clobber(self):
793793
assert (str(warning_lines[0].message) == '"clobber" was '
794794
'deprecated in version 1.3 and will be removed in a '
795795
'future version. Use argument "overwrite" instead.')
796+
797+
def test_invalid_hdu_key_in_contains(self):
798+
"""
799+
Make sure invalid keys in the 'in' operator return False.
800+
Regression test for https://github.com/astropy/astropy/issues/5583
801+
"""
802+
hdulist = fits.HDUList(fits.PrimaryHDU())
803+
hdulist.append(fits.ImageHDU())
804+
hdulist.append(fits.ImageHDU())
805+
806+
# A more or less random assortment of things which are not valid keys.
807+
bad_keys = [None, 3.5, {}]
808+
809+
for key in bad_keys:
810+
assert not (key in hdulist)

0 commit comments

Comments
 (0)