Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions tests/test_lazy_imports.py
Original file line number Diff line number Diff line change
@@ -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,
)
12 changes: 12 additions & 0 deletions typer/__init__.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down
15 changes: 15 additions & 0 deletions typer/_adapters.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down
8 changes: 8 additions & 0 deletions typer/_click/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 10 additions & 0 deletions typer/_click/_compat.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
__lazy_modules__ = {
"codecs",
"collections.abc",
"os",
"typer._click._winconsole",
"types",
"typing",
"weakref",
}

import codecs
import io
import os
Expand Down
14 changes: 14 additions & 0 deletions typer/_click/_termui_impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions typer/_click/_textwrap.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
__lazy_modules__ = {
"collections.abc",
}

import textwrap
from collections.abc import Generator
from contextlib import contextmanager
Expand Down
7 changes: 7 additions & 0 deletions typer/_click/_winconsole.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 13 additions & 0 deletions typer/_click/core.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down
4 changes: 4 additions & 0 deletions typer/_click/decorators.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
__lazy_modules__ = {
"typer._click.utils",
}

from collections.abc import Callable
from typing import TYPE_CHECKING, Any, ParamSpec, TypeVar

Expand Down
7 changes: 7 additions & 0 deletions typer/_click/exceptions.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down
6 changes: 6 additions & 0 deletions typer/_click/formatting.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down
6 changes: 6 additions & 0 deletions typer/_click/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down
6 changes: 6 additions & 0 deletions typer/_click/shell_completion.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
__lazy_modules__ = {
"collections.abc",
"re",
"typer._click.core",
}

import re
from abc import ABC, abstractmethod
from collections.abc import MutableMapping
Expand Down
10 changes: 10 additions & 0 deletions typer/_click/termui.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down
11 changes: 11 additions & 0 deletions typer/_click/utils.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down
12 changes: 12 additions & 0 deletions typer/_coercion.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down
9 changes: 9 additions & 0 deletions typer/_completion_classes.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
__lazy_modules__ = {
"importlib.util",
"os",
"re",
"sys",
"typer",
"typing",
}

import importlib.util
import os
import re
Expand Down
10 changes: 10 additions & 0 deletions typer/_completion_shared.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
__lazy_modules__ = {
"os",
"pathlib",
"shellingham",
"subprocess",
"typer",
"typer._click.globals",
"typer.exceptions",
}

import os
import re
import subprocess
Expand Down
5 changes: 5 additions & 0 deletions typer/_display.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
__lazy_modules__ = {
"typer._click.utils",
"typing",
}

from typing import Any

from ._click.utils import _escape_control_characters
Expand Down
16 changes: 16 additions & 0 deletions typer/_param_types.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down
6 changes: 6 additions & 0 deletions typer/_typing.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down
9 changes: 9 additions & 0 deletions typer/cli.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
__lazy_modules__ = {
"importlib.util",
"pathlib",
"re",
"sys",
"typer._click",
"typing",
}

import importlib.util
import re
import sys
Expand Down
13 changes: 13 additions & 0 deletions typer/completion.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down
17 changes: 17 additions & 0 deletions typer/core.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down
21 changes: 21 additions & 0 deletions typer/main.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Loading
Loading