File tree Expand file tree Collapse file tree 3 files changed +13
-28
lines changed
Expand file tree Collapse file tree 3 files changed +13
-28
lines changed Original file line number Diff line number Diff line change 1- import functools
2- import warnings
31import re
42import textwrap
53import email .message
64
75from ._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
208class 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 ):
Original file line number Diff line number Diff line change 1+ Message.__getitem__ now raises a KeyError on missing keys.
Original file line number Diff line number Diff line change 11import re
22import textwrap
33import unittest
4- import warnings
54import importlib
6- import contextlib
75
86from . import fixtures
97from importlib_metadata import (
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-
2819class 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 """
You can’t perform that action at this time.
0 commit comments