Skip to content

Commit 146d796

Browse files
committed
Import some objects only when type checking.
1 parent 2a43fa9 commit 146d796

File tree

1 file changed

+19
-12
lines changed

1 file changed

+19
-12
lines changed

tox_recreate_hook/__init__.py

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -31,34 +31,40 @@
3131
import sys
3232
from contextlib import contextmanager
3333
from importlib import import_module
34-
from types import CodeType
34+
from typing import TYPE_CHECKING
3535

3636
# 3rd party
3737
import pluggy # type: ignore
3838
from domdf_python_tools.paths import in_directory
39-
from domdf_python_tools.typing import PathLike
4039
from first import first
4140
from tox import reporter # type: ignore
42-
from tox.action import Action # type: ignore
43-
from tox.config import Config, TestenvConfig # type: ignore
44-
from tox.venv import VirtualEnv # type: ignore
4541

4642
# this package
4743
import tox_recreate_hook.hooks
4844

49-
__all__ = ["append_to_sys_path", "tox_testenv_create"]
45+
if TYPE_CHECKING:
46+
# stdlib
47+
from types import CodeType
48+
49+
# 3rd party
50+
from domdf_python_tools.typing import PathLike
51+
from tox.action import Action # type: ignore
52+
from tox.config import Config, TestenvConfig # type: ignore
53+
from tox.venv import VirtualEnv # type: ignore
5054

5155
__author__: str = "Dominic Davis-Foster"
5256
__copyright__: str = "2021 Dominic Davis-Foster"
5357
__license__: str = "MIT License"
5458
__version__: str = "0.1.0"
5559
__email__: str = "[email protected]"
5660

61+
__all__ = ["append_to_sys_path", "tox_testenv_create"]
62+
5763
hookimpl = pluggy.HookimplMarker("tox")
5864

5965

6066
@contextmanager
61-
def append_to_sys_path(path: PathLike):
67+
def append_to_sys_path(path: "PathLike"):
6268
"""
6369
Append ``path`` to :py:obj:`sys.path` for the scope of the :keyword:`with` block.
6470
@@ -67,7 +73,7 @@ def append_to_sys_path(path: PathLike):
6773

6874
path = os.fspath(path)
6975

70-
if path in sys.path:
76+
if path in sys.path: # pragma: no cover
7177
yield
7278
return
7379

@@ -81,10 +87,8 @@ def append_to_sys_path(path: PathLike):
8187

8288

8389
@hookimpl
84-
def tox_testenv_create(venv: VirtualEnv, action: Action) -> None: # noqa: D103
85-
envconfig: TestenvConfig = venv.envconfig
86-
config: Config = envconfig.config
87-
toxinidir = config.toxinidir
90+
def tox_testenv_create(venv: "VirtualEnv", action: "Action") -> None: # noqa: D103
91+
envconfig: "TestenvConfig" = venv.envconfig
8892

8993
if not envconfig.recreate:
9094
return
@@ -94,6 +98,9 @@ def tox_testenv_create(venv: VirtualEnv, action: Action) -> None: # noqa: D103
9498
if not recreate_hook.strip():
9599
return None
96100

101+
config: "Config" = envconfig.config
102+
toxinidir = config.toxinidir
103+
97104
# The whole process should take place within the toxinidir
98105
with in_directory(toxinidir):
99106
print(f"output = {recreate_hook}")

0 commit comments

Comments
 (0)