Skip to content

Commit f21ae3f

Browse files
sararobcopybara-github
authored andcommitted
fix: Remove lazy loading of vertexai.types module to enable from vertexai.types import TypeName
PiperOrigin-RevId: 834811652
1 parent 7d3bcdd commit f21ae3f

File tree

1 file changed

+19
-6
lines changed

1 file changed

+19
-6
lines changed

vertexai/__init__.py

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
import importlib
1818
import sys
1919

20+
from types import ModuleType
21+
2022
from google.cloud.aiplatform import version as aiplatform_version
2123

2224
__version__ = aiplatform_version.__version__
@@ -27,6 +29,22 @@
2729
_genai_types = None
2830

2931

32+
class _LazyTypesLoader(ModuleType):
33+
"""A module that lazily loads the actual types module when an attribute is accessed."""
34+
35+
def __init__(self, *args, **kwargs):
36+
super().__init__(*args, **kwargs)
37+
self._module = None
38+
39+
def __getattr__(self, name):
40+
if self._module is None:
41+
self._module = importlib.import_module("._genai.types", __package__)
42+
return getattr(self._module, name)
43+
44+
sys.modules[__name__ + ".types"] = _LazyTypesLoader(__name__ + ".types")
45+
46+
47+
3048
def __getattr__(name): # type: ignore[no-untyped-def]
3149
# Lazy importing the preview submodule
3250
# See https://peps.python.org/pep-0562/
@@ -45,12 +63,7 @@ def __getattr__(name): # type: ignore[no-untyped-def]
4563
return getattr(_genai_client, name)
4664

4765
if name == "types":
48-
global _genai_types
49-
if _genai_types is None:
50-
_genai_types = importlib.import_module("._genai.types", __name__)
51-
if "vertexai.types" not in sys.modules:
52-
sys.modules["vertexai.types"] = _genai_types
53-
return _genai_types
66+
return sys.modules[__name__ + ".types"]
5467

5568
raise AttributeError(f"module '{__name__}' has no attribute '{name}'")
5669

0 commit comments

Comments
 (0)