Skip to content

Commit 198537e

Browse files
committed
Apply changes from importlib_metadata 6.5.0
1 parent 89561ce commit 198537e

File tree

3 files changed

+38
-6
lines changed

3 files changed

+38
-6
lines changed

Lib/importlib/metadata/__init__.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,26 @@ def __repr__(self):
343343
return f'<FileHash mode: {self.mode} value: {self.value}>'
344344

345345

346-
class Distribution(metaclass=abc.ABCMeta):
346+
class DeprecatedNonAbstract:
347+
def __new__(cls, *args, **kwargs):
348+
all_names = {
349+
name for subclass in inspect.getmro(cls) for name in vars(subclass)
350+
}
351+
abstract = {
352+
name
353+
for name in all_names
354+
if getattr(getattr(cls, name), '__isabstractmethod__', False)
355+
}
356+
if abstract:
357+
warnings.warn(
358+
f"Unimplemented abstract methods {abstract}",
359+
DeprecationWarning,
360+
stacklevel=2,
361+
)
362+
return super().__new__(cls)
363+
364+
365+
class Distribution(DeprecatedNonAbstract):
347366
"""A Python distribution package."""
348367

349368
@abc.abstractmethod

Lib/test/test_importlib/test_main.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import re
22
import pickle
33
import unittest
4+
import warnings
45
import importlib.metadata
6+
import contextlib
57
import itertools
68

79
try:
@@ -10,6 +12,7 @@
1012
from .stubs import fake_filesystem_unittest as ffs
1113

1214
from . import fixtures
15+
from ._context import suppress
1316
from importlib.metadata import (
1417
Distribution,
1518
EntryPoint,
@@ -23,6 +26,13 @@
2326
)
2427

2528

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

@@ -47,6 +57,9 @@ def test_package_not_found_mentions_metadata(self):
4757

4858
assert "metadata" in str(ctx.exception)
4959

60+
# expected to fail until ABC is enforced
61+
@suppress(AssertionError)
62+
@suppress_known_deprecation()
5063
def test_abc_enforced(self):
5164
with self.assertRaises(TypeError):
5265
type('DistributionSubclass', (Distribution,), {})()
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
Updated ``importlib.metadata`` with changes from ``importlib_metadata`` 5.2
2-
through 6.4.1, including: Support ``installed-files.txt`` for
2+
through 6.5.0, including: Support ``installed-files.txt`` for
33
``Distribution.files`` when present. ``PackageMetadata`` now stipulates an
44
additional ``get`` method allowing for easy querying of metadata keys that
55
may not be present. ``packages_distributions`` now honors packages and
66
modules with Python modules that not ``.py`` sources (e.g. ``.pyc``,
77
``.so``). Expand protocol for ``PackageMetadata.get_all`` to match the
88
upstream implementation of ``email.message.Message.get_all`` in
9-
python/typeshed#9620. Declared ``Distribution`` as an abstract class,
10-
enforcing definition of abstract methods in instantiated subclasses.
11-
Deprecated expectation that ``PackageMetadata.__getitem__`` will return
12-
``None`` for missing keys. In the future, it will raise a ``KeyError``.
9+
python/typeshed#9620. Deprecated use of ``Distribution`` without defining
10+
abstract methods. Deprecated expectation that
11+
``PackageMetadata.__getitem__`` will return ``None`` for missing keys. In
12+
the future, it will raise a ``KeyError``.

0 commit comments

Comments
 (0)