Skip to content

Commit a0a4a27

Browse files
authored
Merge pull request #15 from cmpnd-ai/fix/relative-lazy-exports
fix: resolve lazy login imports relative to the loaded package
2 parents 6aea565 + 9206167 commit a0a4a27

3 files changed

Lines changed: 47 additions & 8 deletions

File tree

‎lm15/__init__.py‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -293,12 +293,12 @@
293293
# nothing on the request path needs them, and lm15's import time is a
294294
# promise, so these names resolve lazily (PEP 562).
295295
_LAZY_LOGIN = {
296-
"Auth": ("lm15.login", "Auth"),
297-
"AsyncAuth": ("lm15.login", "AsyncAuth"),
298-
"BoundClient": ("lm15.login", "BoundClient"),
299-
"TerminalUI": ("lm15.login", "TerminalUI"),
300-
"providers": ("lm15.login", "providers"),
301-
"connect": ("lm15.interactive", "connect"),
296+
"Auth": (".login", "Auth"),
297+
"AsyncAuth": (".login", "AsyncAuth"),
298+
"BoundClient": (".login", "BoundClient"),
299+
"TerminalUI": (".login", "TerminalUI"),
300+
"providers": (".login", "providers"),
301+
"connect": (".interactive", "connect"),
302302
}
303303

304304

@@ -308,6 +308,6 @@ def __getattr__(name: str):
308308
raise AttributeError(f"module 'lm15' has no attribute {name!r}")
309309
import importlib
310310

311-
value = getattr(importlib.import_module(target[0]), target[1])
311+
value = getattr(importlib.import_module(target[0], __package__), target[1])
312312
globals()[name] = value
313313
return value

‎lm15/login/flows/__init__.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ def _account_flow(provider: str) -> ProviderFlow | None:
5353
return None
5454
import importlib
5555

56-
module = importlib.import_module(f"lm15.login.flows.{module_name}")
56+
module = importlib.import_module(f".{module_name}", __package__)
5757
for name in dir(module):
5858
candidate = getattr(module, name)
5959
if isinstance(candidate, type) and issubclass(candidate, ProviderFlow) and candidate is not ProviderFlow:

‎tests/test_api_papercuts.py‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,3 +98,42 @@ def test_factories_stay_in_types(self):
9898

9999
for name in self.FACTORY_NAMES:
100100
assert callable(getattr(types, name))
101+
102+
@pytest.mark.parametrize("surface", ["exports", "flows"])
103+
def test_lazy_imports_use_the_loaded_package(self, surface):
104+
import subprocess
105+
import sys
106+
from pathlib import Path
107+
108+
code = """
109+
import importlib.util
110+
import sys
111+
from pathlib import Path
112+
113+
root = Path(sys.argv[1])
114+
sys.modules['lm15'] = None # No unrelated top-level installation may be used.
115+
spec = importlib.util.spec_from_file_location(
116+
'vendored_lm15', root / '__init__.py', submodule_search_locations=[str(root)],
117+
)
118+
package = importlib.util.module_from_spec(spec)
119+
sys.modules[spec.name] = package
120+
spec.loader.exec_module(package)
121+
assert 'vendored_lm15.login' not in sys.modules
122+
if sys.argv[2] == 'exports':
123+
for name in package.__all__:
124+
getattr(package, name)
125+
from vendored_lm15.login import Auth
126+
assert package.Auth is Auth
127+
assert package.connect.__module__ == 'vendored_lm15.interactive'
128+
else:
129+
from vendored_lm15.login.flows import descriptor, flow, ProviderFlow
130+
assert descriptor('xai').id == 'xai'
131+
assert isinstance(flow('xai', 'device'), ProviderFlow)
132+
assert type(flow('xai', 'device')).__module__ == 'vendored_lm15.login.flows.xai'
133+
"""
134+
result = subprocess.run(
135+
[sys.executable, "-I", "-c", code,
136+
str(Path(__file__).resolve().parents[1] / "lm15"), surface],
137+
capture_output=True, text=True, encoding="utf-8", timeout=30,
138+
)
139+
assert result.returncode == 0, result.stderr

0 commit comments

Comments
 (0)