Skip to content

Commit 43c451e

Browse files
jaracoclaude
andcommitted
Keep only the lazy import test that is about this package.
Asserting that attribute access on a lazy name reaches the module exercises the import machinery and not anything here, so it goes; the import time it stood for is what the perf exercises already watch. The localize regression moves to tests/compat, beside the module it covers and out of the suite that syncs to the stdlib, which has no _compat to localize for. What stays in the shared suite is that the lazy modules go unloaded, now named for the declaration it reads. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
1 parent 61af6b3 commit 43c451e

2 files changed

Lines changed: 38 additions & 42 deletions

File tree

‎tests/compat/test_localize.py‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
import importlib.metadata as stdlib
77
import pathlib
8+
import subprocess
89
import sys
910
import unittest
1011
import warnings
@@ -177,3 +178,38 @@ def metadata(self):
177178
warnings.simplefilter('always')
178179
assert localize.dist(hostile) is hostile
179180
assert not [str(warning.message) for warning in caught]
181+
182+
183+
class LazyAdaptersTests(unittest.TestCase):
184+
"""
185+
Adapting a message withstands the adapters being imported lazily.
186+
"""
187+
188+
def test_adapted_message_passes_through(self):
189+
"""
190+
An already adapted message survives localization unchanged.
191+
192+
``localize.message`` tests the message with ``isinstance``,
193+
reaching the class through a lazily imported module so that the
194+
name resolves first. Were the class the lazy name itself, before
195+
3.15 it would be a proxy, which answers as no match however the
196+
object relates to it, and the message would be adapted a second
197+
time. Run as a subprocess because reading a lazy name resolves it
198+
for good, which any other test would do first.
199+
200+
Ref python/importlib_metadata#554.
201+
"""
202+
script = (
203+
'import email.message, importlib; '
204+
'from importlib_metadata._compat import localize; '
205+
'adapters = importlib.import_module("importlib_metadata._adapters"); '
206+
'msg = adapters.Message(email.message.Message()); '
207+
'print(localize.message(msg) is msg)'
208+
)
209+
proc = subprocess.run(
210+
[sys.executable, '-c', script],
211+
capture_output=True,
212+
encoding='utf-8',
213+
check=True,
214+
)
215+
assert proc.stdout.strip() == 'True', 'the message was adapted twice'

‎tests/test_main.py‎

Lines changed: 2 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -115,9 +115,9 @@ class LazyImportTests(unittest.TestCase):
115115
The imports at module scope cost no more than the deferred ones did.
116116
"""
117117

118-
def test_declared_modules_stay_unloaded(self):
118+
def test_lazy_modules_stay_unloaded(self):
119119
"""
120-
Importing the package loads none of the modules it declares lazy.
120+
Importing the package loads none of its ``__lazy_modules__``.
121121
122122
``importlib.abc`` goes first because the package imports it
123123
eagerly and, on some Pythons, it reaches ``inspect`` through
@@ -139,46 +139,6 @@ def test_declared_modules_stay_unloaded(self):
139139
)
140140
assert not proc.stdout.split(), f'imported eagerly: {proc.stdout.strip()}'
141141

142-
def test_reading_through_a_lazy_name(self):
143-
"""
144-
Attribute access on a lazy name reaches the real module.
145-
146-
Note that the name itself need not be the module: before 3.15 the
147-
backport binds a proxy that passes attribute access through, so
148-
this holds while ``is`` comparison against the module does not.
149-
"""
150-
assert importlib_metadata.csv.reader is importlib.import_module('csv').reader
151-
assert isinstance(importlib_metadata.json.loads('{}'), dict)
152-
assert importlib_metadata.inspect.getmodulename('f.py') == 'f'
153-
154-
def test_adapted_message_passes_through(self):
155-
"""
156-
An already adapted message survives localization unchanged.
157-
158-
``localize.message`` tests it with ``isinstance``, reaching the
159-
class through a lazy module so that the name resolves first. A
160-
proxy named directly answers as no match however the object
161-
relates to it, and the message would be adapted a second time.
162-
Run as a subprocess because reading a lazy name resolves it for
163-
good, which every other test here would do first.
164-
165-
Ref python/importlib_metadata#554.
166-
"""
167-
script = (
168-
'import email.message, importlib; '
169-
'from importlib_metadata._compat import localize; '
170-
'adapters = importlib.import_module("importlib_metadata._adapters"); '
171-
'msg = adapters.Message(email.message.Message()); '
172-
'print(localize.message(msg) is msg)'
173-
)
174-
proc = subprocess.run(
175-
[sys.executable, '-c', script],
176-
capture_output=True,
177-
encoding='utf-8',
178-
check=True,
179-
)
180-
assert proc.stdout.strip() == 'True', 'the message was adapted twice'
181-
182142

183143
class NameNormalizationTests(fixtures.OnSysPath, fixtures.SiteDir, unittest.TestCase):
184144
@staticmethod

0 commit comments

Comments
 (0)