Skip to content

Commit 7622bd0

Browse files
authored
Move import_attr util to _common/ (#51316)
Quarantining library dependencies out of `_private/` --------- Signed-off-by: Edward Oakes <ed.nmi.oakes@gmail.com>
1 parent 115ddcf commit 7622bd0

File tree

11 files changed

+44
-42
lines changed

11 files changed

+44
-42
lines changed

python/ray/_common/__init__.py

Whitespace-only changes.

python/ray/_common/utils.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import importlib
2+
3+
4+
def import_attr(full_path: str, *, reload_module: bool = False):
5+
"""Given a full import path to a module attr, return the imported attr.
6+
7+
If `reload_module` is set, the module will be reloaded using `importlib.reload`.
8+
9+
For example, the following are equivalent:
10+
MyClass = import_attr("module.submodule:MyClass")
11+
MyClass = import_attr("module.submodule.MyClass")
12+
from module.submodule import MyClass
13+
14+
Returns:
15+
Imported attr
16+
"""
17+
if full_path is None:
18+
raise TypeError("import path cannot be None")
19+
20+
if ":" in full_path:
21+
if full_path.count(":") > 1:
22+
raise ValueError(
23+
f'Got invalid import path "{full_path}". An '
24+
"import path may have at most one colon."
25+
)
26+
module_name, attr_name = full_path.split(":")
27+
else:
28+
last_period_idx = full_path.rfind(".")
29+
module_name = full_path[:last_period_idx]
30+
attr_name = full_path[last_period_idx + 1 :]
31+
32+
module = importlib.import_module(module_name)
33+
if reload_module:
34+
importlib.reload(module)
35+
return getattr(module, attr_name)

python/ray/_private/runtime_env/plugin.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from abc import ABC
55
from typing import List, Dict, Optional, Any, Type
66

7+
from ray._common.utils import import_attr
78
from ray._private.runtime_env.context import RuntimeEnvContext
89
from ray._private.runtime_env.uri_cache import URICache
910
from ray._private.runtime_env.constants import (
@@ -15,7 +16,6 @@
1516
RAY_RUNTIME_ENV_PLUGIN_MAX_PRIORITY,
1617
)
1718
from ray.util.annotations import DeveloperAPI
18-
from ray._private.utils import import_attr
1919

2020
default_logger = logging.getLogger(__name__)
2121

python/ray/_private/utils.py

Lines changed: 0 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1178,40 +1178,6 @@ def new_func(*args, **kwargs):
11781178
return deprecated_wrapper
11791179

11801180

1181-
def import_attr(full_path: str, *, reload_module: bool = False):
1182-
"""Given a full import path to a module attr, return the imported attr.
1183-
1184-
If `reload_module` is set, the module will be reloaded using `importlib.reload`.
1185-
1186-
For example, the following are equivalent:
1187-
MyClass = import_attr("module.submodule:MyClass")
1188-
MyClass = import_attr("module.submodule.MyClass")
1189-
from module.submodule import MyClass
1190-
1191-
Returns:
1192-
Imported attr
1193-
"""
1194-
if full_path is None:
1195-
raise TypeError("import path cannot be None")
1196-
1197-
if ":" in full_path:
1198-
if full_path.count(":") > 1:
1199-
raise ValueError(
1200-
f'Got invalid import path "{full_path}". An '
1201-
"import path may have at most one colon."
1202-
)
1203-
module_name, attr_name = full_path.split(":")
1204-
else:
1205-
last_period_idx = full_path.rfind(".")
1206-
module_name = full_path[:last_period_idx]
1207-
attr_name = full_path[last_period_idx + 1 :]
1208-
1209-
module = importlib.import_module(module_name)
1210-
if reload_module:
1211-
importlib.reload(module)
1212-
return getattr(module, attr_name)
1213-
1214-
12151181
def get_wheel_filename(
12161182
sys_platform: str = sys.platform,
12171183
ray_version: str = ray.__version__,

python/ray/llm/_internal/serve/deployments/llm/llm_server.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
# Third-party imports
77
from pydantic import ValidationError as PydanticValidationError
88
from ray import serve
9-
from ray._private.utils import import_attr
9+
from ray._common.utils import import_attr
1010

1111
# Local imports
1212
from ray.llm._internal.serve.configs.constants import (

python/ray/serve/_private/application_state.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
import ray
1212
from ray import cloudpickle
13-
from ray._private.utils import import_attr
13+
from ray._common.utils import import_attr
1414
from ray.exceptions import RuntimeEnvSetupError
1515
from ray.serve._private.build_app import BuiltApplication, build_app
1616
from ray.serve._private.common import (

python/ray/serve/_private/utils.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,9 @@
1717

1818
import ray
1919
import ray.util.serialization_addons
20+
from ray._common.utils import import_attr
2021
from ray._private.resource_spec import HEAD_NODE_RESOURCE_NAME
21-
from ray._private.utils import get_random_alphanumeric_string, import_attr
22+
from ray._private.utils import get_random_alphanumeric_string
2223
from ray._private.worker import LOCAL_MODE, SCRIPT_MODE
2324
from ray._raylet import MessagePackSerializer
2425
from ray.actor import ActorHandle

python/ray/serve/config.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
PrivateAttr,
1515
validator,
1616
)
17-
from ray._private.utils import import_attr
17+
from ray._common.utils import import_attr
1818
from ray.serve._private.constants import (
1919
DEFAULT_AUTOSCALING_POLICY,
2020
DEFAULT_GRPC_PORT,

python/ray/serve/scripts.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
import ray
1616
from ray import serve
17-
from ray._private.utils import import_attr
17+
from ray._common.utils import import_attr
1818
from ray.autoscaler._private.cli_logger import cli_logger
1919
from ray.dashboard.modules.dashboard_sdk import parse_runtime_env_args
2020
from ray.dashboard.modules.serve.sdk import ServeSubmissionClient

python/ray/serve/tests/test_standalone_2.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ def test_controller_deserialization_deployment_def(
205205
def run_graph():
206206
"""Deploys a Serve application to the controller's Ray cluster."""
207207
from ray import serve
208-
from ray._private.utils import import_attr
208+
from ray._common.utils import import_attr
209209

210210
# Import and build the graph
211211
graph = import_attr("test_config_files.pizza.serve_dag")

0 commit comments

Comments
 (0)