From d9eafb746f4a8b4383a93acc29beada1e343e694 Mon Sep 17 00:00:00 2001 From: "Jason R. Coombs" Date: Tue, 18 Apr 2023 08:56:29 -0400 Subject: [PATCH] Apply changes from importlib_metadata 6.5.0 --- Lib/importlib/metadata/__init__.py | 21 ++++++++++++++++++- Lib/test/test_importlib/_context.py | 13 ++++++++++++ Lib/test/test_importlib/test_main.py | 13 ++++++++++++ ...-04-16-19-48-21.gh-issue-103584.3mBTuM.rst | 10 ++++----- 4 files changed, 51 insertions(+), 6 deletions(-) create mode 100644 Lib/test/test_importlib/_context.py diff --git a/Lib/importlib/metadata/__init__.py b/Lib/importlib/metadata/__init__.py index 54194a69a1a6ac..b8eb19d05dccae 100644 --- a/Lib/importlib/metadata/__init__.py +++ b/Lib/importlib/metadata/__init__.py @@ -343,7 +343,26 @@ def __repr__(self): return f'' -class Distribution(metaclass=abc.ABCMeta): +class DeprecatedNonAbstract: + def __new__(cls, *args, **kwargs): + all_names = { + name for subclass in inspect.getmro(cls) for name in vars(subclass) + } + abstract = { + name + for name in all_names + if getattr(getattr(cls, name), '__isabstractmethod__', False) + } + if abstract: + warnings.warn( + f"Unimplemented abstract methods {abstract}", + DeprecationWarning, + stacklevel=2, + ) + return super().__new__(cls) + + +class Distribution(DeprecatedNonAbstract): """A Python distribution package.""" @abc.abstractmethod diff --git a/Lib/test/test_importlib/_context.py b/Lib/test/test_importlib/_context.py new file mode 100644 index 00000000000000..8a53eb55d1503b --- /dev/null +++ b/Lib/test/test_importlib/_context.py @@ -0,0 +1,13 @@ +import contextlib + + +# from jaraco.context 4.3 +class suppress(contextlib.suppress, contextlib.ContextDecorator): + """ + A version of contextlib.suppress with decorator support. + + >>> @suppress(KeyError) + ... def key_error(): + ... {}[''] + >>> key_error() + """ diff --git a/Lib/test/test_importlib/test_main.py b/Lib/test/test_importlib/test_main.py index 5f6a0d08c16f33..46cd2b696d4cc8 100644 --- a/Lib/test/test_importlib/test_main.py +++ b/Lib/test/test_importlib/test_main.py @@ -1,7 +1,9 @@ import re import pickle import unittest +import warnings import importlib.metadata +import contextlib import itertools try: @@ -10,6 +12,7 @@ from .stubs import fake_filesystem_unittest as ffs from . import fixtures +from ._context import suppress from importlib.metadata import ( Distribution, EntryPoint, @@ -23,6 +26,13 @@ ) +@contextlib.contextmanager +def suppress_known_deprecation(): + with warnings.catch_warnings(record=True) as ctx: + warnings.simplefilter('default', category=DeprecationWarning) + yield ctx + + class BasicTests(fixtures.DistInfoPkg, unittest.TestCase): version_pattern = r'\d+\.\d+(\.\d)?' @@ -47,6 +57,9 @@ def test_package_not_found_mentions_metadata(self): assert "metadata" in str(ctx.exception) + # expected to fail until ABC is enforced + @suppress(AssertionError) + @suppress_known_deprecation() def test_abc_enforced(self): with self.assertRaises(TypeError): type('DistributionSubclass', (Distribution,), {})() diff --git a/Misc/NEWS.d/next/Library/2023-04-16-19-48-21.gh-issue-103584.3mBTuM.rst b/Misc/NEWS.d/next/Library/2023-04-16-19-48-21.gh-issue-103584.3mBTuM.rst index d0acb7931359bc..6d7c93ade9cd94 100644 --- a/Misc/NEWS.d/next/Library/2023-04-16-19-48-21.gh-issue-103584.3mBTuM.rst +++ b/Misc/NEWS.d/next/Library/2023-04-16-19-48-21.gh-issue-103584.3mBTuM.rst @@ -1,12 +1,12 @@ Updated ``importlib.metadata`` with changes from ``importlib_metadata`` 5.2 -through 6.4.1, including: Support ``installed-files.txt`` for +through 6.5.0, including: Support ``installed-files.txt`` for ``Distribution.files`` when present. ``PackageMetadata`` now stipulates an additional ``get`` method allowing for easy querying of metadata keys that may not be present. ``packages_distributions`` now honors packages and modules with Python modules that not ``.py`` sources (e.g. ``.pyc``, ``.so``). Expand protocol for ``PackageMetadata.get_all`` to match the upstream implementation of ``email.message.Message.get_all`` in -python/typeshed#9620. Declared ``Distribution`` as an abstract class, -enforcing definition of abstract methods in instantiated subclasses. -Deprecated expectation that ``PackageMetadata.__getitem__`` will return -``None`` for missing keys. In the future, it will raise a ``KeyError``. +python/typeshed#9620. Deprecated use of ``Distribution`` without defining +abstract methods. Deprecated expectation that +``PackageMetadata.__getitem__`` will return ``None`` for missing keys. In +the future, it will raise a ``KeyError``.