Skip to content

Commit a970a49

Browse files
committed
Message.__getitem__ now raises a KeyError on missing keys.
Ref #371
1 parent 32c14aa commit a970a49

File tree

3 files changed

+13
-28
lines changed

3 files changed

+13
-28
lines changed

importlib_metadata/_adapters.py

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,8 @@
1-
import functools
2-
import warnings
31
import re
42
import textwrap
53
import email.message
64

75
from ._text import FoldedCase
8-
from ._compat import pypy_partial
9-
10-
11-
# Do not remove prior to 2024-01-01 or Python 3.14
12-
_warn = functools.partial(
13-
warnings.warn,
14-
"Implicit None on return values is deprecated and will raise KeyErrors.",
15-
DeprecationWarning,
16-
stacklevel=pypy_partial(2),
17-
)
186

197

208
class Message(email.message.Message):
@@ -53,12 +41,17 @@ def __iter__(self):
5341

5442
def __getitem__(self, item):
5543
"""
56-
Warn users that a ``KeyError`` can be expected when a
57-
missing key is supplied. Ref python/importlib_metadata#371.
44+
Override parent behavior to typical dict behavior.
45+
46+
``email.message.Message`` will emit None values for missing
47+
keys. Typical mappings, including this ``Message``, will raise
48+
a key error for missing keys.
49+
50+
Ref python/importlib_metadata#371.
5851
"""
5952
res = super().__getitem__(item)
6053
if res is None:
61-
_warn()
54+
raise KeyError(item)
6255
return res
6356

6457
def _repair_headers(self):

newsfragments/371.removal.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Message.__getitem__ now raises a KeyError on missing keys.

tests/test_api.py

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
import re
22
import textwrap
33
import unittest
4-
import warnings
54
import importlib
6-
import contextlib
75

86
from . import fixtures
97
from importlib_metadata import (
@@ -18,13 +16,6 @@
1816
)
1917

2018

21-
@contextlib.contextmanager
22-
def suppress_known_deprecation():
23-
with warnings.catch_warnings(record=True) as ctx:
24-
warnings.simplefilter('default', category=DeprecationWarning)
25-
yield ctx
26-
27-
2819
class APITests(
2920
fixtures.EggInfoPkg,
3021
fixtures.EggInfoPkgPipInstalledNoToplevel,
@@ -157,13 +148,13 @@ def test_importlib_metadata_version(self):
157148
resolved = version('importlib-metadata')
158149
assert re.match(self.version_pattern, resolved)
159150

160-
def test_missing_key_legacy(self):
151+
def test_missing_key(self):
161152
"""
162-
Requesting a missing key will still return None, but warn.
153+
Requesting a missing key raises KeyError.
163154
"""
164155
md = metadata('distinfo-pkg')
165-
with suppress_known_deprecation():
166-
assert md['does-not-exist'] is None
156+
with self.assertRaises(KeyError):
157+
md['does-not-exist']
167158

168159
def test_get_key(self):
169160
"""

0 commit comments

Comments
 (0)