Skip to content

Commit 7e162d8

Browse files
jaracoclaude
andcommitted
Revert "Prefer the greater metadata directory when a path supplies several."
The ordering was lexicographic on the directory name and not on the parsed version, so it only approximated a preference for the newer version and did not resolve every instance of the reported condition. This reverts commit 6a8b861. Ref python/cpython#100666. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
1 parent 3be1416 commit 7e162d8

3 files changed

Lines changed: 7 additions & 79 deletions

File tree

‎importlib_metadata/__init__.py‎

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -922,22 +922,14 @@ def __init__(self, path: FastPath):
922922
From the children in the path, calculate early all of the
923923
children that appear to represent metadata (infos) or legacy
924924
metadata (eggs).
925-
926-
Children are considered in reverse lexicographic order, so when
927-
a path supplies more than one metadata directory for a
928-
distribution, as an interrupted or unclean upgrade may leave
929-
behind, the greater one is found first
930-
(python/cpython#100666). Because the order is lexicographic on
931-
the directory name and not on the parsed version, it only
932-
approximates a preference for the newer version.
933925
"""
934926

935927
base = os.path.basename(path.root).lower()
936928
base_is_egg = base.endswith(".egg")
937929
self.infos = FreezableDefaultDict(list)
938930
self.eggs = FreezableDefaultDict(list)
939931

940-
for child in sorted(path.children(), key=str.lower, reverse=True):
932+
for child in path.children():
941933
low = child.lower()
942934
if low.endswith((".dist-info", ".egg-info")):
943935
# rpartition is faster than splitext and suitable for this purpose.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
Reverted the preference for the greater metadata directory added in v9.1.0.
2+
The ordering was lexicographic on the directory name and not on the parsed
3+
version, so it only approximated a preference for the newer version. Where a
4+
path supplies more than one metadata directory for a distribution, discovery
5+
once again reports them in the order the path supplies
6+
(python/cpython#100666).

‎tests/test_main.py‎

Lines changed: 0 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -208,76 +208,6 @@ def test_missing_required_metadata(self):
208208
unversioned.version
209209

210210

211-
class MultipleVersionsTests(fixtures.OnSysPath, fixtures.SiteDir, unittest.TestCase):
212-
"""
213-
An unclean upgrade can leave more than one metadata directory for a
214-
distribution in one path. Prefer the greater one.
215-
216-
Ref python/cpython#100666.
217-
"""
218-
219-
# Deliberately not in version order; the preference follows the
220-
# lexicographic ordering of the directory names, which only
221-
# approximates the ordering of the versions they name.
222-
versions = '1.12.1', '1.13.1', '1.9.0', '2.0.0'
223-
expected = '2.0.0', '1.9.0', '1.13.1', '1.12.1'
224-
225-
@staticmethod
226-
def make_pkg(name, version, files=None):
227-
"""
228-
Create metadata for a dist-info package at the indicated version.
229-
"""
230-
return {
231-
f'{name}-{version}.dist-info': (
232-
{'METADATA': f'Name: {name}\nVersion: {version}\n'}
233-
if files is None
234-
else files
235-
),
236-
}
237-
238-
def build_versions(self, *versions):
239-
for ver in versions:
240-
fixtures.build_files(self.make_pkg('foo', ver), self.site_dir)
241-
242-
def test_greater_version_preferred(self):
243-
"""
244-
Retrieving by name resolves to the greater version.
245-
"""
246-
self.build_versions(*self.versions)
247-
assert version('foo') == self.expected[0]
248-
249-
def test_discovery_order(self):
250-
"""
251-
Preferring one version doesn't hide the others from discovery,
252-
which reports them greatest first.
253-
"""
254-
self.build_versions(*self.versions)
255-
found = tuple(dist.version for dist in Distribution.discover(name='foo'))
256-
assert found == self.expected
257-
258-
def test_greater_version_preferred_in_eggs(self):
259-
"""
260-
Legacy egg-info metadata gets the same preference.
261-
"""
262-
fixtures.build_files(
263-
{
264-
f'foo-{ver}.egg-info': {'PKG-INFO': f'Name: foo\nVersion: {ver}\n'}
265-
for ver in self.versions
266-
},
267-
self.site_dir,
268-
)
269-
assert version('foo') == self.expected[0]
270-
271-
def test_validity_preferred_to_version(self):
272-
"""
273-
A valid dist is still preferred to an invalid one of a greater
274-
version, the condition that prompted the report.
275-
"""
276-
self.build_versions('1.12.1')
277-
fixtures.build_files(self.make_pkg('foo', '2.0.0', files={}), self.site_dir)
278-
assert version('foo') == '1.12.1'
279-
280-
281211
class NonASCIITests(fixtures.OnSysPath, fixtures.SiteDir, unittest.TestCase):
282212
@staticmethod
283213
def pkg_with_non_ascii_description(site_dir):

0 commit comments

Comments
 (0)