Skip to content

Commit

Permalink
Fix and add test
Browse files Browse the repository at this point in the history
  • Loading branch information
hoodmane committed Dec 20, 2024
1 parent 2442f72 commit b5bcd16
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
9 changes: 6 additions & 3 deletions spacy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,14 @@ def blank(
config = util.dot_to_dict(config)
return LangClass.from_config(config, vocab=vocab, meta=meta)


def __getattr__(name):
if name == "cli":
import importlib

return importlib.import_module("." + name, __name__)
if name == "info":
from .cli.info import info

return info
if name == "cli":
from . import cli
return cli
raise AttributeError(f"module {__name__!r} has no attribute {name!r}")
24 changes: 24 additions & 0 deletions spacy/tests/test_misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -495,3 +495,27 @@ def test_find_available_port():
with pytest.warns(UserWarning, match="already in use"):
found_port = find_available_port(port, host, auto_select=True)
assert found_port == port + 1, "Didn't find next port"


def test_lazy_load_cli():
import subprocess
import sys
from textwrap import dedent

subprocess.run(
[
sys.executable,
"-c",
dedent(
"""\
import sys
import spacy
assert "spacy" in sys.modules
assert "spacy.cli" not in sys.modules
spacy.cli
assert "spacy.cli" in sys.modules
"""
),
],
check=True,
)

0 comments on commit b5bcd16

Please sign in to comment.