Skip to content

Commit c3bae1e

Browse files
authored
Merge pull request #491 from python/debt/remove-legacy
Remove deprecated constructs
2 parents b76931d + a970a49 commit c3bae1e

File tree

7 files changed

+15
-65
lines changed

7 files changed

+15
-65
lines changed

docs/conf.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,4 @@
6969
('py:class', 'importlib_metadata._meta._T'),
7070
# Workaround for #435
7171
('py:class', '_T'),
72-
# Other workarounds
73-
('py:class', 'importlib_metadata.DeprecatedNonAbstract'),
7472
]

importlib_metadata/__init__.py

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
import pathlib
1313
import operator
1414
import textwrap
15-
import warnings
1615
import functools
1716
import itertools
1817
import posixpath
@@ -334,27 +333,7 @@ def __repr__(self) -> str:
334333
return f'<FileHash mode: {self.mode} value: {self.value}>'
335334

336335

337-
class DeprecatedNonAbstract:
338-
# Required until Python 3.14
339-
def __new__(cls, *args, **kwargs):
340-
all_names = {
341-
name for subclass in inspect.getmro(cls) for name in vars(subclass)
342-
}
343-
abstract = {
344-
name
345-
for name in all_names
346-
if getattr(getattr(cls, name), '__isabstractmethod__', False)
347-
}
348-
if abstract:
349-
warnings.warn(
350-
f"Unimplemented abstract methods {abstract}",
351-
DeprecationWarning,
352-
stacklevel=2,
353-
)
354-
return super().__new__(cls)
355-
356-
357-
class Distribution(DeprecatedNonAbstract):
336+
class Distribution(metaclass=abc.ABCMeta):
358337
"""
359338
An abstract Python distribution package.
360339

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/+8256a9d7.removal.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Removed deprecated support for Distribution subclasses not implementing abstract methods.

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
"""

tests/test_main.py

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,13 @@
11
import re
22
import pickle
33
import unittest
4-
import warnings
54
import importlib
65
import importlib_metadata
7-
import contextlib
86
from .compat.py39 import os_helper
97

108
import pyfakefs.fake_filesystem_unittest as ffs
119

1210
from . import fixtures
13-
from ._context import suppress
1411
from ._path import Symlink
1512
from importlib_metadata import (
1613
Distribution,
@@ -25,13 +22,6 @@
2522
)
2623

2724

28-
@contextlib.contextmanager
29-
def suppress_known_deprecation():
30-
with warnings.catch_warnings(record=True) as ctx:
31-
warnings.simplefilter('default', category=DeprecationWarning)
32-
yield ctx
33-
34-
3525
class BasicTests(fixtures.DistInfoPkg, unittest.TestCase):
3626
version_pattern = r'\d+\.\d+(\.\d)?'
3727

@@ -56,9 +46,6 @@ def test_package_not_found_mentions_metadata(self):
5646

5747
assert "metadata" in str(ctx.exception)
5848

59-
# expected to fail until ABC is enforced
60-
@suppress(AssertionError)
61-
@suppress_known_deprecation()
6249
def test_abc_enforced(self):
6350
with self.assertRaises(TypeError):
6451
type('DistributionSubclass', (Distribution,), {})()

0 commit comments

Comments
 (0)