Skip to content

Commit 46285bf

Browse files
sararobcopybara-github
authored andcommitted
fix: Enable from vertexai.types import TypeName without needing to run from vertexai import types first
PiperOrigin-RevId: 838839559
1 parent f4a6cbe commit 46285bf

File tree

2 files changed

+7
-32
lines changed

2 files changed

+7
-32
lines changed

tests/unit/architecture/test_vertexai_import.py

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -81,17 +81,7 @@ def test_vertexai_import():
8181
new_modules_after_vertexai = modules_after_vertexai - modules_before_vertexai
8282

8383
vertexai_module_name = vertexai.__name__ # == "vertexai"
84-
assert sorted(new_modules_after_vertexai) == sorted(
85-
[
86-
vertexai_module_name,
87-
f"{vertexai_module_name}.types",
88-
]
89-
)
90-
91-
placeholder_vertexai_types_module = sys.modules[f"{vertexai_module_name}.types"]
92-
assert isinstance(placeholder_vertexai_types_module, vertexai._LazyTypesLoader)
93-
94-
assert f"{vertexai_module_name}._genai.types" not in sys.modules
84+
assert sorted(new_modules_after_vertexai) == [vertexai_module_name]
9585

9686
assert vertexai_import_timedelta.total_seconds() < 0.005
9787
assert aip_import_timedelta.total_seconds() < 40

vertexai/__init__.py

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

20-
from types import ModuleType
21-
from typing import Any
22-
2320
from google.cloud.aiplatform import version as aiplatform_version
2421

2522
__version__ = aiplatform_version.__version__
@@ -30,23 +27,6 @@
3027
_genai_types = None
3128

3229

33-
class _LazyTypesLoader(ModuleType):
34-
"""A module that lazily loads the types module when an attribute is accessed via from `vertexai.types import TypeName`."""
35-
36-
def __init__(self, *args: Any, **kwargs: Any):
37-
super().__init__(*args, **kwargs)
38-
self._module = None
39-
40-
def __getattr__(self, name: str):
41-
if self._module is None:
42-
self._module = importlib.import_module("._genai.types", __package__)
43-
return getattr(self._module, name)
44-
45-
46-
# Register a placeholder _LazyTypesLoader instance for vertexai.types until it is accessed.
47-
sys.modules[__name__ + ".types"] = _LazyTypesLoader(__name__ + ".types")
48-
49-
5030
def __getattr__(name): # type: ignore[no-untyped-def]
5131
# Lazy importing the preview submodule
5232
# See https://peps.python.org/pep-0562/
@@ -65,7 +45,12 @@ def __getattr__(name): # type: ignore[no-untyped-def]
6545
return getattr(_genai_client, name)
6646

6747
if name == "types":
68-
return sys.modules[__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
6954

7055
raise AttributeError(f"module '{__name__}' has no attribute '{name}'")
7156

0 commit comments

Comments
 (0)