Skip to content

Commit 6ed01ae

Browse files
committed
Merge removal commits into feature/clean-entry-points
2 parents 9a6641b + dde2b9d commit 6ed01ae

File tree

3 files changed

+5
-267
lines changed

3 files changed

+5
-267
lines changed

importlib_metadata/__init__.py

+5-201
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
from importlib import import_module
3030
from importlib.abc import MetaPathFinder
3131
from itertools import starmap
32-
from typing import List, Mapping, Optional, Union
32+
from typing import List, Mapping, Optional
3333

3434

3535
__all__ = [
@@ -227,17 +227,6 @@ def _for(self, dist):
227227
vars(self).update(dist=dist)
228228
return self
229229

230-
def __iter__(self):
231-
"""
232-
Supply iter so one may construct dicts of EntryPoints by name.
233-
"""
234-
msg = (
235-
"Construction of dict of EntryPoints is deprecated in "
236-
"favor of EntryPoints."
237-
)
238-
warnings.warn(msg, DeprecationWarning)
239-
return iter((self.name, self))
240-
241230
def matches(self, **params):
242231
"""
243232
EntryPoint matches the given parameters.
@@ -283,77 +272,7 @@ def __hash__(self):
283272
return hash(self._key())
284273

285274

286-
class DeprecatedList(list):
287-
"""
288-
Allow an otherwise immutable object to implement mutability
289-
for compatibility.
290-
291-
>>> recwarn = getfixture('recwarn')
292-
>>> dl = DeprecatedList(range(3))
293-
>>> dl[0] = 1
294-
>>> dl.append(3)
295-
>>> del dl[3]
296-
>>> dl.reverse()
297-
>>> dl.sort()
298-
>>> dl.extend([4])
299-
>>> dl.pop(-1)
300-
4
301-
>>> dl.remove(1)
302-
>>> dl += [5]
303-
>>> dl + [6]
304-
[1, 2, 5, 6]
305-
>>> dl + (6,)
306-
[1, 2, 5, 6]
307-
>>> dl.insert(0, 0)
308-
>>> dl
309-
[0, 1, 2, 5]
310-
>>> dl == [0, 1, 2, 5]
311-
True
312-
>>> dl == (0, 1, 2, 5)
313-
True
314-
>>> len(recwarn)
315-
1
316-
"""
317-
318-
__slots__ = ()
319-
320-
_warn = functools.partial(
321-
warnings.warn,
322-
"EntryPoints list interface is deprecated. Cast to list if needed.",
323-
DeprecationWarning,
324-
stacklevel=pypy_partial(2),
325-
)
326-
327-
def _wrap_deprecated_method(method_name: str): # type: ignore
328-
def wrapped(self, *args, **kwargs):
329-
self._warn()
330-
return getattr(super(), method_name)(*args, **kwargs)
331-
332-
return method_name, wrapped
333-
334-
locals().update(
335-
map(
336-
_wrap_deprecated_method,
337-
'__setitem__ __delitem__ append reverse extend pop remove '
338-
'__iadd__ insert sort'.split(),
339-
)
340-
)
341-
342-
def __add__(self, other):
343-
if not isinstance(other, tuple):
344-
self._warn()
345-
other = tuple(other)
346-
return self.__class__(tuple(self) + other)
347-
348-
def __eq__(self, other):
349-
if not isinstance(other, tuple):
350-
self._warn()
351-
other = tuple(other)
352-
353-
return tuple(self).__eq__(other)
354-
355-
356-
class EntryPoints(DeprecatedList):
275+
class EntryPoints(tuple):
357276
"""
358277
An immutable collection of selectable EntryPoint objects.
359278
"""
@@ -364,14 +283,6 @@ def __getitem__(self, name): # -> EntryPoint:
364283
"""
365284
Get the EntryPoint in self matching name.
366285
"""
367-
if isinstance(name, int):
368-
warnings.warn(
369-
"Accessing entry points by index is deprecated. "
370-
"Cast to tuple if needed.",
371-
DeprecationWarning,
372-
stacklevel=2,
373-
)
374-
return super().__getitem__(name)
375286
try:
376287
return next(iter(self.select(name=name)))
377288
except StopIteration:
@@ -396,10 +307,6 @@ def names(self):
396307
def groups(self):
397308
"""
398309
Return the set of all groups of all entry points.
399-
400-
For coverage while SelectableGroups is present.
401-
>>> EntryPoints().groups
402-
set()
403310
"""
404311
return {ep.group for ep in self}
405312

@@ -415,101 +322,6 @@ def _from_text(text):
415322
)
416323

417324

418-
class Deprecated:
419-
"""
420-
Compatibility add-in for mapping to indicate that
421-
mapping behavior is deprecated.
422-
423-
>>> recwarn = getfixture('recwarn')
424-
>>> class DeprecatedDict(Deprecated, dict): pass
425-
>>> dd = DeprecatedDict(foo='bar')
426-
>>> dd.get('baz', None)
427-
>>> dd['foo']
428-
'bar'
429-
>>> list(dd)
430-
['foo']
431-
>>> list(dd.keys())
432-
['foo']
433-
>>> 'foo' in dd
434-
True
435-
>>> list(dd.values())
436-
['bar']
437-
>>> len(recwarn)
438-
1
439-
"""
440-
441-
_warn = functools.partial(
442-
warnings.warn,
443-
"SelectableGroups dict interface is deprecated. Use select.",
444-
DeprecationWarning,
445-
stacklevel=pypy_partial(2),
446-
)
447-
448-
def __getitem__(self, name):
449-
self._warn()
450-
return super().__getitem__(name)
451-
452-
def get(self, name, default=None):
453-
self._warn()
454-
return super().get(name, default)
455-
456-
def __iter__(self):
457-
self._warn()
458-
return super().__iter__()
459-
460-
def __contains__(self, *args):
461-
self._warn()
462-
return super().__contains__(*args)
463-
464-
def keys(self):
465-
self._warn()
466-
return super().keys()
467-
468-
def values(self):
469-
self._warn()
470-
return super().values()
471-
472-
473-
class SelectableGroups(Deprecated, dict):
474-
"""
475-
A backward- and forward-compatible result from
476-
entry_points that fully implements the dict interface.
477-
"""
478-
479-
@classmethod
480-
def load(cls, eps):
481-
by_group = operator.attrgetter('group')
482-
ordered = sorted(eps, key=by_group)
483-
grouped = itertools.groupby(ordered, by_group)
484-
return cls((group, EntryPoints(eps)) for group, eps in grouped)
485-
486-
@property
487-
def _all(self):
488-
"""
489-
Reconstruct a list of all entrypoints from the groups.
490-
"""
491-
groups = super(Deprecated, self).values()
492-
return EntryPoints(itertools.chain.from_iterable(groups))
493-
494-
@property
495-
def groups(self):
496-
return self._all.groups
497-
498-
@property
499-
def names(self):
500-
"""
501-
for coverage:
502-
>>> SelectableGroups().names
503-
set()
504-
"""
505-
return self._all.names
506-
507-
def select(self, **params):
508-
if not params:
509-
return self
510-
return self._all.select(**params)
511-
512-
513325
class PackagePath(pathlib.PurePosixPath):
514326
"""A reference to a path in a package"""
515327

@@ -1029,27 +841,19 @@ def version(distribution_name):
1029841
"""
1030842

1031843

1032-
def entry_points(**params) -> Union[EntryPoints, SelectableGroups]:
844+
def entry_points(**params) -> EntryPoints:
1033845
"""Return EntryPoint objects for all installed packages.
1034846
1035847
Pass selection parameters (group or name) to filter the
1036848
result to entry points matching those properties (see
1037849
EntryPoints.select()).
1038850
1039-
For compatibility, returns ``SelectableGroups`` object unless
1040-
selection parameters are supplied. In the future, this function
1041-
will return ``EntryPoints`` instead of ``SelectableGroups``
1042-
even when no selection parameters are supplied.
1043-
1044-
For maximum future compatibility, pass selection parameters
1045-
or invoke ``.select`` with parameters on the result.
1046-
1047-
:return: EntryPoints or SelectableGroups for all installed packages.
851+
:return: EntryPoints for all installed packages.
1048852
"""
1049853
eps = itertools.chain.from_iterable(
1050854
dist.entry_points for dist in _unique(distributions())
1051855
)
1052-
return SelectableGroups.load(eps).select(**params)
856+
return EntryPoints(eps).select(**params)
1053857

1054858

1055859
def files(distribution_name):

tests/test_api.py

-56
Original file line numberDiff line numberDiff line change
@@ -124,62 +124,6 @@ def test_entry_points_missing_name(self):
124124
def test_entry_points_missing_group(self):
125125
assert entry_points(group='missing') == ()
126126

127-
def test_entry_points_dict_construction(self):
128-
"""
129-
Prior versions of entry_points() returned simple lists and
130-
allowed casting those lists into maps by name using ``dict()``.
131-
Capture this now deprecated use-case.
132-
"""
133-
with suppress_known_deprecation() as caught:
134-
eps = dict(entry_points(group='entries'))
135-
136-
assert 'main' in eps
137-
assert eps['main'] == entry_points(group='entries')['main']
138-
139-
# check warning
140-
expected = next(iter(caught))
141-
assert expected.category is DeprecationWarning
142-
assert "Construction of dict of EntryPoints is deprecated" in str(expected)
143-
144-
def test_entry_points_by_index(self):
145-
"""
146-
Prior versions of Distribution.entry_points would return a
147-
tuple that allowed access by index.
148-
Capture this now deprecated use-case
149-
See python/importlib_metadata#300 and bpo-44246.
150-
"""
151-
eps = distribution('distinfo-pkg').entry_points
152-
with suppress_known_deprecation() as caught:
153-
eps[0]
154-
155-
# check warning
156-
expected = next(iter(caught))
157-
assert expected.category is DeprecationWarning
158-
assert "Accessing entry points by index is deprecated" in str(expected)
159-
160-
def test_entry_points_groups_getitem(self):
161-
"""
162-
Prior versions of entry_points() returned a dict. Ensure
163-
that callers using '.__getitem__()' are supported but warned to
164-
migrate.
165-
"""
166-
with suppress_known_deprecation():
167-
entry_points()['entries'] == entry_points(group='entries')
168-
169-
with self.assertRaises(KeyError):
170-
entry_points()['missing']
171-
172-
def test_entry_points_groups_get(self):
173-
"""
174-
Prior versions of entry_points() returned a dict. Ensure
175-
that callers using '.get()' are supported but warned to
176-
migrate.
177-
"""
178-
with suppress_known_deprecation():
179-
entry_points().get('missing', 'default') == 'default'
180-
entry_points().get('entries', 'default') == entry_points()['entries']
181-
entry_points().get('missing', ()) == ()
182-
183127
def test_entry_points_allows_no_attributes(self):
184128
ep = entry_points().select(group='entries', name='main')
185129
with self.assertRaises(AttributeError):

tests/test_main.py

-10
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
import re
2-
import json
32
import pickle
43
import unittest
5-
import warnings
64
import importlib
75
import importlib_metadata
86
import pyfakefs.fake_filesystem_unittest as ffs
@@ -259,14 +257,6 @@ def test_hashable(self):
259257
"""EntryPoints should be hashable"""
260258
hash(self.ep)
261259

262-
def test_json_dump(self):
263-
"""
264-
json should not expect to be able to dump an EntryPoint
265-
"""
266-
with self.assertRaises(Exception):
267-
with warnings.catch_warnings(record=True):
268-
json.dumps(self.ep)
269-
270260
def test_module(self):
271261
assert self.ep.module == 'value'
272262

0 commit comments

Comments
 (0)