diff --git a/tests/test_lazy_imports.py b/tests/test_lazy_imports.py new file mode 100644 index 0000000000..a6fdf20718 --- /dev/null +++ b/tests/test_lazy_imports.py @@ -0,0 +1,28 @@ +import subprocess +import sys +from textwrap import dedent + +import pytest + + +@pytest.mark.skipif(sys.version_info < (3, 15), reason="PEP 810 requires Python 3.15") +def test_public_command_class_is_loaded_on_first_use(): + subprocess.run( + [ + sys.executable, + "-X", + "lazy_imports=normal", + "-c", + dedent( + """\ + import sys + import typer + + assert "typer.main" not in sys.modules + app = typer.Typer() + assert type(app) is sys.modules["typer.main"].Typer + """ + ), + ], + check=True, + ) diff --git a/typer/__init__.py b/typer/__init__.py index c706f83149..9ce396153f 100644 --- a/typer/__init__.py +++ b/typer/__init__.py @@ -1,5 +1,17 @@ """Typer, build great CLIs. Easy to code. Based on Python type hints.""" +__lazy_modules__ = { + "shutil", + "typer", + "typer._click.exceptions", + "typer._click.termui", + "typer._click.utils", + "typer.exceptions", + "typer.main", + "typer.models", + "typer.params", +} + __version__ = "0.29.3" from shutil import get_terminal_size as get_terminal_size diff --git a/typer/_adapters.py b/typer/_adapters.py index 448c54afac..9dbd80c494 100644 --- a/typer/_adapters.py +++ b/typer/_adapters.py @@ -1,3 +1,18 @@ +__lazy_modules__ = { + "collections.abc", + "datetime", + "enum", + "os", + "pathlib", + "pydantic", + "pydantic.errors", + "sys", + "typer._click", + "typer._param_types", + "typer._typing", + "typer.models", +} + import os import sys from collections.abc import Generator, Sequence diff --git a/typer/_click/__init__.py b/typer/_click/__init__.py index 48c9edcddd..6cec74478c 100644 --- a/typer/_click/__init__.py +++ b/typer/_click/__init__.py @@ -2,6 +2,14 @@ Code taken and adapted from Click: https://github.com/pallets/click/releases/tag/8.3.1 """ +__lazy_modules__ = { + "typer._click.core", + "typer._click.exceptions", + "typer._click.formatting", + "typer._click.termui", + "typer._click.utils", +} + from .core import Command as Command from .core import Context as Context from .core import Parameter as Parameter diff --git a/typer/_click/_compat.py b/typer/_click/_compat.py index 7b4ae5a43d..ef1564706f 100644 --- a/typer/_click/_compat.py +++ b/typer/_click/_compat.py @@ -1,3 +1,13 @@ +__lazy_modules__ = { + "codecs", + "collections.abc", + "os", + "typer._click._winconsole", + "types", + "typing", + "weakref", +} + import codecs import io import os diff --git a/typer/_click/_termui_impl.py b/typer/_click/_termui_impl.py index 20aabd5e97..a9fb2cc8d7 100644 --- a/typer/_click/_termui_impl.py +++ b/typer/_click/_termui_impl.py @@ -3,6 +3,20 @@ is placed here and only imported as needed. """ +__lazy_modules__ = { + "collections.abc", + "contextlib", + "io", + "math", + "msvcrt", + "termios", + "time", + "tty", + "typer._click._compat", + "typer._click.utils", + "types", +} + import contextlib import math import os diff --git a/typer/_click/_textwrap.py b/typer/_click/_textwrap.py index e240956f82..b035084be0 100644 --- a/typer/_click/_textwrap.py +++ b/typer/_click/_textwrap.py @@ -1,3 +1,7 @@ +__lazy_modules__ = { + "collections.abc", +} + import textwrap from collections.abc import Generator from contextlib import contextmanager diff --git a/typer/_click/_winconsole.py b/typer/_click/_winconsole.py index f6dedbfd6e..44f2418e18 100644 --- a/typer/_click/_winconsole.py +++ b/typer/_click/_winconsole.py @@ -6,6 +6,13 @@ # compared to the original patches as we do not need to patch # the entire interpreter but just work in our little world of # echo and prompt. +__lazy_modules__ = { + "collections.abc", + "msvcrt", + "time", + "typer._click._compat", +} + import io import sys import time diff --git a/typer/_click/core.py b/typer/_click/core.py index a405a27660..910300bcb6 100644 --- a/typer/_click/core.py +++ b/typer/_click/core.py @@ -1,3 +1,16 @@ +__lazy_modules__ = { + "collections.abc", + "inspect", + "os", + "typer._click.exceptions", + "typer._click.globals", + "typer._click.parser", + "typer._click.termui", + "typer._click.utils", + "typer.exceptions", + "types", +} + import enum import inspect import os diff --git a/typer/_click/decorators.py b/typer/_click/decorators.py index 1f5bc43b3f..37e1250b73 100644 --- a/typer/_click/decorators.py +++ b/typer/_click/decorators.py @@ -1,3 +1,7 @@ +__lazy_modules__ = { + "typer._click.utils", +} + from collections.abc import Callable from typing import TYPE_CHECKING, Any, ParamSpec, TypeVar diff --git a/typer/_click/exceptions.py b/typer/_click/exceptions.py index b546ffa6f4..72ceba0aa7 100644 --- a/typer/_click/exceptions.py +++ b/typer/_click/exceptions.py @@ -1,3 +1,10 @@ +__lazy_modules__ = { + "collections.abc", + "typer._click._compat", + "typer._click.globals", + "typer._click.utils", +} + from collections.abc import Sequence from typing import IO, TYPE_CHECKING, Any, Union diff --git a/typer/_click/formatting.py b/typer/_click/formatting.py index 28a53741bf..397591dd04 100644 --- a/typer/_click/formatting.py +++ b/typer/_click/formatting.py @@ -1,3 +1,9 @@ +__lazy_modules__ = { + "collections.abc", + "typer._click._compat", + "typer._click.parser", +} + from collections.abc import Generator, Iterable, Iterator, Sequence from contextlib import contextmanager diff --git a/typer/_click/parser.py b/typer/_click/parser.py index 3eb5acd632..468178ae20 100644 --- a/typer/_click/parser.py +++ b/typer/_click/parser.py @@ -22,6 +22,12 @@ # maintained by the Python Software Foundation. # Copyright 2001-2006 Gregory P. Ward # Copyright 2002-2006 Python Software Foundation +__lazy_modules__ = { + "collections", + "collections.abc", + "typer._click.exceptions", +} + from collections import deque from collections.abc import Sequence from typing import ( diff --git a/typer/_click/shell_completion.py b/typer/_click/shell_completion.py index 29155b7444..ee55de47d0 100644 --- a/typer/_click/shell_completion.py +++ b/typer/_click/shell_completion.py @@ -1,3 +1,9 @@ +__lazy_modules__ = { + "collections.abc", + "re", + "typer._click.core", +} + import re from abc import ABC, abstractmethod from collections.abc import MutableMapping diff --git a/typer/_click/termui.py b/typer/_click/termui.py index 784ae56212..8511a16ce5 100644 --- a/typer/_click/termui.py +++ b/typer/_click/termui.py @@ -1,3 +1,13 @@ +__lazy_modules__ = { + "collections.abc", + "contextlib", + "io", + "typer._click.exceptions", + "typer._click.globals", + "typer._click.utils", + "typer.exceptions", +} + import io from collections.abc import Callable, Iterable from contextlib import AbstractContextManager diff --git a/typer/_click/utils.py b/typer/_click/utils.py index 2d0dc90cb0..8d7ed4687d 100644 --- a/typer/_click/utils.py +++ b/typer/_click/utils.py @@ -1,3 +1,14 @@ +__lazy_modules__ = { + "collections.abc", + "functools", + "os", + "re", + "sys", + "typer._click._compat", + "typer._click.globals", + "types", +} + import os import re import sys diff --git a/typer/_coercion.py b/typer/_coercion.py index 52ded22943..1a32cef949 100644 --- a/typer/_coercion.py +++ b/typer/_coercion.py @@ -1,3 +1,15 @@ +__lazy_modules__ = { + "collections.abc", + "datetime", + "os", + "typer._click", + "typer._click.exceptions", + "typer._display", + "typer._param_types", + "typer._typing", + "typer.models", +} + import os from abc import ABC, abstractmethod from collections.abc import Callable diff --git a/typer/_completion_classes.py b/typer/_completion_classes.py index cfae02c9bf..df025563ec 100644 --- a/typer/_completion_classes.py +++ b/typer/_completion_classes.py @@ -1,3 +1,12 @@ +__lazy_modules__ = { + "importlib.util", + "os", + "re", + "sys", + "typer", + "typing", +} + import importlib.util import os import re diff --git a/typer/_completion_shared.py b/typer/_completion_shared.py index 96c144c307..43d8e6d9f5 100644 --- a/typer/_completion_shared.py +++ b/typer/_completion_shared.py @@ -1,3 +1,13 @@ +__lazy_modules__ = { + "os", + "pathlib", + "shellingham", + "subprocess", + "typer", + "typer._click.globals", + "typer.exceptions", +} + import os import re import subprocess diff --git a/typer/_display.py b/typer/_display.py index d64b2b0006..cfadfc0cf9 100644 --- a/typer/_display.py +++ b/typer/_display.py @@ -1,3 +1,8 @@ +__lazy_modules__ = { + "typer._click.utils", + "typing", +} + from typing import Any from ._click.utils import _escape_control_characters diff --git a/typer/_param_types.py b/typer/_param_types.py index 356e0c7528..a5e212f464 100644 --- a/typer/_param_types.py +++ b/typer/_param_types.py @@ -1,3 +1,19 @@ +__lazy_modules__ = { + "collections.abc", + "datetime", + "enum", + "os", + "pathlib", + "stat", + "typer._click", + "typer._click._compat", + "typer._click.exceptions", + "typer._click.utils", + "typer._display", + "typer._typing", + "uuid", +} + import os import stat from collections.abc import Sequence diff --git a/typer/_typing.py b/typer/_typing.py index b45965af90..0a8c00546e 100644 --- a/typer/_typing.py +++ b/typer/_typing.py @@ -1,6 +1,12 @@ # Adapted from pydantic 1.9.2 # mypy: ignore-errors +__lazy_modules__ = { + "collections.abc", + "numbers", + "types", +} + import numbers import types from collections.abc import Callable diff --git a/typer/cli.py b/typer/cli.py index 98f9f4ec77..b98131c493 100644 --- a/typer/cli.py +++ b/typer/cli.py @@ -1,3 +1,12 @@ +__lazy_modules__ = { + "importlib.util", + "pathlib", + "re", + "sys", + "typer._click", + "typing", +} + import importlib.util import re import sys diff --git a/typer/completion.py b/typer/completion.py index c6b92ad00d..9eb21dfbeb 100644 --- a/typer/completion.py +++ b/typer/completion.py @@ -1,3 +1,16 @@ +__lazy_modules__ = { + "collections.abc", + "os", + "sys", + "typer", + "typer._click", + "typer._completion_classes", + "typer._completion_shared", + "typer.models", + "typer.utils", + "typing", +} + import os import sys from collections.abc import MutableMapping diff --git a/typer/core.py b/typer/core.py index 5bdbb2fe75..270f23a0c5 100644 --- a/typer/core.py +++ b/typer/core.py @@ -1,3 +1,20 @@ +__lazy_modules__ = { + "collections.abc", + "difflib", + "enum", + "errno", + "gettext", + "inspect", + "sys", + "typer._click.parser", + "typer._click.shell_completion", + "typer._coercion", + "typer._display", + "typer._param_types", + "typer.exceptions", + "typing", +} + import errno import inspect import os diff --git a/typer/main.py b/typer/main.py index d0be102132..852fd097e8 100644 --- a/typer/main.py +++ b/typer/main.py @@ -1,3 +1,24 @@ +__lazy_modules__ = { + "annotated_doc", + "collections.abc", + "functools", + "inspect", + "os", + "platform", + "shutil", + "subprocess", + "traceback", + "typer", + "typer._click.globals", + "typer._coercion", + "typer._param_types", + "typer._typing", + "typer.completion", + "typer.utils", + "types", + "typing", +} + import inspect import os import platform diff --git a/typer/models.py b/typer/models.py index 2924bca979..aded589800 100644 --- a/typer/models.py +++ b/typer/models.py @@ -1,3 +1,7 @@ +__lazy_modules__ = { + "typer._click.shell_completion", +} + import inspect import io from collections.abc import Callable, Sequence diff --git a/typer/params.py b/typer/params.py index ecd8559951..e6b6089965 100644 --- a/typer/params.py +++ b/typer/params.py @@ -1,3 +1,11 @@ +__lazy_modules__ = { + "annotated_doc", + "collections.abc", + "typer", + "typer._click.shell_completion", + "typer.models", +} + from collections.abc import Callable from typing import TYPE_CHECKING, Annotated, Any diff --git a/typer/rich_utils.py b/typer/rich_utils.py index c724035f4b..67314e24db 100644 --- a/typer/rich_utils.py +++ b/typer/rich_utils.py @@ -1,5 +1,27 @@ # Extracted and modified from https://github.com/ewels/rich-click +__lazy_modules__ = { + "collections", + "collections.abc", + "inspect", + "io", + "rich", + "rich.align", + "rich.columns", + "rich.emoji", + "rich.markdown", + "rich.markup", + "rich.padding", + "rich.panel", + "rich.table", + "rich.text", + "rich.theme", + "rich.traceback", + "typer", + "typer.core", + "typer.models", +} + import inspect import io from collections import defaultdict diff --git a/typer/testing.py b/typer/testing.py index b038878dc6..b35cfa124f 100644 --- a/typer/testing.py +++ b/typer/testing.py @@ -1,3 +1,14 @@ +__lazy_modules__ = { + "collections.abc", + "os", + "shlex", + "sys", + "typer", + "typer._click", + "typer.main", + "types", +} + import contextlib import io import os diff --git a/typer/utils.py b/typer/utils.py index addf9334d4..a6b7b5c366 100644 --- a/typer/utils.py +++ b/typer/utils.py @@ -1,3 +1,12 @@ +__lazy_modules__ = { + "collections.abc", + "copy", + "inspect", + "typer._typing", + "typer.models", + "typing", +} + import inspect from collections.abc import Callable from copy import copy