Skip to content

Commit 1e64c1a

Browse files
committed
unconditional import during type checking
1 parent 2f6ce1a commit 1e64c1a

File tree

1 file changed

+16
-4
lines changed

1 file changed

+16
-4
lines changed

pytorch_lightning/__init__.py

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import os
55
import sys
66
import time
7+
import typing
78

89
_this_year = time.strftime("%Y")
910
__version__ = '1.3.0dev'
@@ -50,6 +51,7 @@
5051
_PACKAGE_ROOT = os.path.dirname(__file__)
5152
_PROJECT_ROOT = os.path.dirname(_PACKAGE_ROOT)
5253

54+
5355
try:
5456
# This variable is injected in the __builtins__ by the build
5557
# process. It used to enable importing subpackages of skimage when
@@ -58,10 +60,16 @@
5860
except NameError:
5961
__LIGHTNING_SETUP__: bool = False
6062

61-
if __LIGHTNING_SETUP__: # pragma: no-cover
62-
sys.stdout.write(f'Partial import of `{__name__}` during the build process.\n') # pragma: no-cover
63-
# We are not importing the rest of the lightning during the build process, as it may not be compiled yet
64-
else:
63+
64+
if typing.TYPE_CHECKING:
65+
# Simplify the imports for the static type checker.
66+
from pytorch_lightning import metrics
67+
from pytorch_lightning.callbacks import Callback
68+
from pytorch_lightning.core import LightningDataModule, LightningModule
69+
from pytorch_lightning.trainer import Trainer
70+
from pytorch_lightning.utilities.seed import seed_everything
71+
72+
elif not __LIGHTNING_SETUP__: # pragma: no-cover
6573
from pytorch_lightning import metrics
6674
from pytorch_lightning.callbacks import Callback
6775
from pytorch_lightning.core import LightningDataModule, LightningModule
@@ -76,6 +84,10 @@
7684
'seed_everything',
7785
'metrics',
7886
]
87+
else:
88+
sys.stdout.write(f'Partial import of `{__name__}` during the build process.\n') # pragma: no-cover
89+
# We are not importing the rest of the lightning during the build process, as it may not be compiled yet
90+
7991

8092
# for compatibility with namespace packages
8193
__import__('pkg_resources').declare_namespace(__name__)

0 commit comments

Comments
 (0)