Skip to content

Deprecate Distribution without concrete methods #451

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
v6.5.0
======

* #422: Removed ABC metaclass from ``Distribution`` and instead
deprecated construction of ``Distribution`` objects without
concrete methods.

v6.4.1
======

Expand Down
2 changes: 2 additions & 0 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,6 @@
('py:class', 'importlib_metadata._meta._T'),
# Workaround for #435
('py:class', '_T'),
# Other workarounds
('py:class', 'importlib_metadata.DeprecatedNonAbstract'),
]
21 changes: 20 additions & 1 deletion importlib_metadata/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,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 tests/_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 tests/test_main.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import re
import pickle
import unittest
import warnings
import importlib
import importlib_metadata
import contextlib
import itertools
import pyfakefs.fake_filesystem_unittest as ffs

from . import fixtures
from ._context import suppress
from importlib_metadata import (
Distribution,
EntryPoint,
Expand All @@ -20,6 +23,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 @@ -44,6 +54,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