Skip to content

Commit

Permalink
Apply changes from importlib_metadata 6.5.0
Browse files Browse the repository at this point in the history
  • Loading branch information
jaraco committed Apr 18, 2023
1 parent 89561ce commit d9eafb7
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 6 deletions.
21 changes: 20 additions & 1 deletion Lib/importlib/metadata/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,26 @@ def __repr__(self):
return f'<FileHash mode: {self.mode} value: {self.value}>'


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
Expand Down
13 changes: 13 additions & 0 deletions Lib/test/test_importlib/_context.py
Original file line number Diff line number Diff line change
@@ -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()
"""
13 changes: 13 additions & 0 deletions Lib/test/test_importlib/test_main.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import re
import pickle
import unittest
import warnings
import importlib.metadata
import contextlib
import itertools

try:
Expand All @@ -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,
Expand All @@ -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)?'

Expand All @@ -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,), {})()
Expand Down
Original file line number Diff line number Diff line change
@@ -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``.

0 comments on commit d9eafb7

Please sign in to comment.