Skip to content

Commit

Permalink
When requesting an item from metadata, if it's not present, raise a K…
Browse files Browse the repository at this point in the history
…eyError. Fixes #371.
  • Loading branch information
jaraco committed Dec 18, 2022
1 parent 51c0c85 commit a7eb4ba
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
14 changes: 14 additions & 0 deletions importlib_metadata/_adapters.py
Expand Up @@ -39,6 +39,20 @@ def __init__(self, *args, **kwargs):
def __iter__(self):
return super().__iter__()

def __getitem__(self, item):
"""
Prefer dict-like behavior for __getitem__ when keys are missing.
>>> msg = Message(email.message.Message())
>>> msg['thing']
Traceback (most recent call last):
...
KeyError: 'thing'
"""
res = super().__getitem__(item)
if res is None:
raise KeyError(item)
return res

def _repair_headers(self):
def redent(value):
"Correct for RFC822 indentation"
Expand Down
1 change: 0 additions & 1 deletion tests/test_api.py
Expand Up @@ -141,7 +141,6 @@ def test_importlib_metadata_version(self):
resolved = version('importlib-metadata')
assert re.match(self.version_pattern, resolved)

@__import__('pytest').mark.xfail(reason="not implemented #371")
def test_missing_key(self):
"""
Attempting to request missing metadata raises KeyError.
Expand Down

0 comments on commit a7eb4ba

Please sign in to comment.