Skip to content

Commit b642f20

Browse files
committed
feat: actionable guidance for removed v2 interfaces
In v3 the v2 top-level modules (sagemaker.estimator, sagemaker.model, ...) were removed, so importing them fails with a bare "ModuleNotFoundError: No module named 'sagemaker.estimator'" that gives the caller no path forward. This adds an actionable migration experience. - raise_removed_in_v3() helper in sagemaker.core.deprecations: emits a DeprecationWarning and raises a ModuleNotFoundError naming the v3 replacement (and the exact import where the migration guide publishes one), plus the migration-guide link. - 12 curated "tombstone" modules under sagemaker/ for the high-traffic removed interfaces (estimator, model, predictor, base_predictor, predictor_async, transformer, tuner, processing, pipeline, multidatamodel, automl, algorithm). These ship with sagemaker-core. - A fallback meta-path finder (_RemovedV2ModuleFinder) registered from sagemaker.core.__init__ as a last resort: any other removed sagemaker.<name> gets a generic "not available in v3, see migration guide" message instead of a bare error. Appended to sys.meta_path so it never shadows real v3 packages or the tombstones, with a _KNOWN_V3_TOPLEVEL guard protecting real packages (core/train/serve/mlops/lineage/ai_registry). Adds tests/unit/test_removed_v2_modules.py (36 tests) covering tombstone messages and the fallback finder (registration, ordering, non-shadowing of real packages, pass-through of non-sagemaker and nested names).
1 parent b27221b commit b642f20

15 files changed

Lines changed: 570 additions & 0 deletions
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License"). You
4+
# may not use this file except in compliance with the License. A copy of
5+
# the License is located at
6+
#
7+
# http://aws.amazon.com/apache2.0/
8+
#
9+
# or in the "license" file accompanying this file. This file is
10+
# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
11+
# ANY KIND, either express or implied. See the License for the specific
12+
# language governing permissions and limitations under the License.
13+
"""Removed in v3. This module stood in the v2 SageMaker Python SDK.
14+
15+
Importing ``sagemaker.algorithm`` raises an actionable ``ModuleNotFoundError`` that
16+
points to the v3 replacement. See the migration guide for details.
17+
"""
18+
from __future__ import absolute_import
19+
20+
from sagemaker.core.deprecations import raise_removed_in_v3
21+
22+
raise_removed_in_v3(
23+
module="sagemaker.algorithm",
24+
replacement="`ModelTrainer` in the sagemaker-train package",
25+
v3_import="from sagemaker.train import ModelTrainer",
26+
)
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License"). You
4+
# may not use this file except in compliance with the License. A copy of
5+
# the License is located at
6+
#
7+
# http://aws.amazon.com/apache2.0/
8+
#
9+
# or in the "license" file accompanying this file. This file is
10+
# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
11+
# ANY KIND, either express or implied. See the License for the specific
12+
# language governing permissions and limitations under the License.
13+
"""Removed in v3. This module stood in the v2 SageMaker Python SDK.
14+
15+
Importing ``sagemaker.automl`` raises an actionable ``ModuleNotFoundError`` that
16+
points to the v3 replacement. See the migration guide for details.
17+
"""
18+
from __future__ import absolute_import
19+
20+
from sagemaker.core.deprecations import raise_removed_in_v3
21+
22+
raise_removed_in_v3(
23+
module="sagemaker.automl",
24+
replacement="the AutoML resources under sagemaker.core",
25+
)
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License"). You
4+
# may not use this file except in compliance with the License. A copy of
5+
# the License is located at
6+
#
7+
# http://aws.amazon.com/apache2.0/
8+
#
9+
# or in the "license" file accompanying this file. This file is
10+
# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
11+
# ANY KIND, either express or implied. See the License for the specific
12+
# language governing permissions and limitations under the License.
13+
"""Removed in v3. This module stood in the v2 SageMaker Python SDK.
14+
15+
Importing ``sagemaker.base_predictor`` raises an actionable ``ModuleNotFoundError`` that
16+
points to the v3 replacement. See the migration guide for details.
17+
"""
18+
from __future__ import absolute_import
19+
20+
from sagemaker.core.deprecations import raise_removed_in_v3
21+
22+
raise_removed_in_v3(
23+
module="sagemaker.base_predictor",
24+
replacement="the Endpoint resource in the sagemaker-core package",
25+
)

‎sagemaker-core/src/sagemaker/core/__init__.py‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,17 @@
11
"""SageMaker Core package for low-level resource management and SDK foundations."""
2+
23
from sagemaker.core.utils.utils import enable_textual_rich_console_and_traceback
4+
from sagemaker.core.deprecations import register_removed_module_finder
35

46

57
enable_textual_rich_console_and_traceback()
68

9+
# Install the fallback finder that gives actionable migration guidance for v2
10+
# modules removed in v3 but not individually tombstoned. sagemaker-core is the
11+
# universal dependency of every v3 package, so registering here guarantees the
12+
# finder is active whenever the SDK is used.
13+
register_removed_module_finder()
14+
715
# Job management
816
from sagemaker.core.job import _Job # noqa: F401
917
from sagemaker.core.processing import ( # noqa: F401

‎sagemaker-core/src/sagemaker/core/deprecations.py‎

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,107 @@
1313
"""Module for deprecation abstractions."""
1414
from __future__ import absolute_import
1515

16+
import importlib.abc
1617
import logging
18+
import sys
1719
import warnings
1820

1921
logger = logging.getLogger(__name__)
2022

2123
V2_URL = "https://sagemaker.readthedocs.io/en/stable/v2.html"
2224

25+
# Migration guide for users moving from the v2 SDK to v3.
26+
V3_MIGRATION_URL = "https://github.com/aws/sagemaker-python-sdk/blob/master/migration.md"
27+
28+
# Real top-level ``sagemaker.*`` names that ship in v3. The fallback finder must
29+
# never intercept these -- even when a package (e.g. sagemaker-train) is simply
30+
# not installed, its absence should surface the normal error, not a bogus
31+
# "removed in v3" message.
32+
_KNOWN_V3_TOPLEVEL = frozenset({"core", "train", "serve", "mlops", "lineage", "ai_registry"})
33+
34+
35+
def raise_removed_in_v3(module, replacement=None, v3_import=None):
36+
"""Warn and then raise an actionable error for a v2 module removed in v3.
37+
38+
The v2 SDK exposed top-level modules (e.g. ``sagemaker.estimator``) that no
39+
longer exist in v3. Importing one would otherwise fail with a bare
40+
``ModuleNotFoundError: No module named 'sagemaker.estimator'`` that gives the
41+
caller no path forward. This helper is called from lightweight "tombstone"
42+
modules that stand in for those removed names: it emits a
43+
``DeprecationWarning`` and then raises a ``ModuleNotFoundError`` whose message
44+
names the v3 replacement and links the migration guide.
45+
46+
Args:
47+
module (str): The removed v2 module path, e.g. ``"sagemaker.estimator"``.
48+
replacement (str): Human readable v3 replacement, e.g.
49+
``"ModelTrainer in the sagemaker-train package"``. Optional.
50+
v3_import (str): The exact v3 import statement, e.g.
51+
``"from sagemaker.train import ModelTrainer"``. Quoted verbatim so the
52+
caller can copy-paste it. Optional.
53+
54+
Raises:
55+
ModuleNotFoundError: always, after emitting the deprecation warning.
56+
"""
57+
msg = f"`{module}` was removed in the SageMaker Python SDK v3."
58+
if replacement:
59+
msg += f" Use {replacement}."
60+
if v3_import:
61+
msg += f" ({v3_import})"
62+
msg += f"\nSee {V3_MIGRATION_URL} for the migration guide."
63+
64+
warnings.warn(msg, DeprecationWarning, stacklevel=2)
65+
logger.warning(msg)
66+
raise ModuleNotFoundError(msg, name=module)
67+
68+
69+
class _RemovedV2ModuleFinder(importlib.abc.MetaPathFinder):
70+
"""Fallback finder that gives actionable guidance for removed v2 modules.
71+
72+
Curated removals ship as explicit "tombstone" modules (e.g.
73+
``sagemaker/estimator.py``) that raise a precise, per-module message. This
74+
finder is the *catch-all* for every other ``sagemaker.<name>`` that existed
75+
in v2 but was not individually tombstoned: instead of a bare
76+
``ModuleNotFoundError: No module named 'sagemaker.foo'``, the caller gets a
77+
message that says the module is not available in v3 and points to the
78+
migration guide.
79+
80+
It is registered by appending to ``sys.meta_path``, so it only runs *after*
81+
the normal import machinery has failed to locate the module. That ordering
82+
guarantees it never shadows:
83+
- real v3 packages (``sagemaker.core``, ``sagemaker.train``, ...), and
84+
- the curated tombstone modules, which resolve as ordinary files first.
85+
"""
86+
87+
def find_spec(self, fullname, path=None, target=None):
88+
"""Intercept only genuinely-missing top-level ``sagemaker.<name>`` imports."""
89+
if not fullname.startswith("sagemaker."):
90+
return None
91+
leaf = fullname[len("sagemaker.") :]
92+
# Only guard top-level names; never touch real v3 subpackages.
93+
if "." in leaf or leaf in _KNOWN_V3_TOPLEVEL:
94+
return None
95+
96+
msg = (
97+
f"`{fullname}` is not available in the SageMaker Python SDK v3. "
98+
"It may have been removed or moved to a new location."
99+
f"\nSee {V3_MIGRATION_URL} for the migration guide."
100+
)
101+
warnings.warn(msg, DeprecationWarning, stacklevel=2)
102+
logger.warning(msg)
103+
raise ModuleNotFoundError(msg, name=fullname)
104+
105+
106+
def register_removed_module_finder():
107+
"""Install the fallback finder for removed v2 modules (idempotent).
108+
109+
Appends a single ``_RemovedV2ModuleFinder`` to ``sys.meta_path`` so it acts
110+
as a last resort. Safe to call multiple times -- it installs at most one
111+
instance per process.
112+
"""
113+
if any(isinstance(f, _RemovedV2ModuleFinder) for f in sys.meta_path):
114+
return
115+
sys.meta_path.append(_RemovedV2ModuleFinder())
116+
23117

24118
def _warn(msg, sdk_version=None):
25119
"""Generic warning raiser referencing V2
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License"). You
4+
# may not use this file except in compliance with the License. A copy of
5+
# the License is located at
6+
#
7+
# http://aws.amazon.com/apache2.0/
8+
#
9+
# or in the "license" file accompanying this file. This file is
10+
# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
11+
# ANY KIND, either express or implied. See the License for the specific
12+
# language governing permissions and limitations under the License.
13+
"""Removed in v3. This module stood in the v2 SageMaker Python SDK.
14+
15+
Importing ``sagemaker.estimator`` raises an actionable ``ModuleNotFoundError`` that
16+
points to the v3 replacement. See the migration guide for details.
17+
"""
18+
from __future__ import absolute_import
19+
20+
from sagemaker.core.deprecations import raise_removed_in_v3
21+
22+
raise_removed_in_v3(
23+
module="sagemaker.estimator",
24+
replacement="`ModelTrainer` in the sagemaker-train package",
25+
v3_import="from sagemaker.train import ModelTrainer",
26+
)
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License"). You
4+
# may not use this file except in compliance with the License. A copy of
5+
# the License is located at
6+
#
7+
# http://aws.amazon.com/apache2.0/
8+
#
9+
# or in the "license" file accompanying this file. This file is
10+
# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
11+
# ANY KIND, either express or implied. See the License for the specific
12+
# language governing permissions and limitations under the License.
13+
"""Removed in v3. This module stood in the v2 SageMaker Python SDK.
14+
15+
Importing ``sagemaker.model`` raises an actionable ``ModuleNotFoundError`` that
16+
points to the v3 replacement. See the migration guide for details.
17+
"""
18+
from __future__ import absolute_import
19+
20+
from sagemaker.core.deprecations import raise_removed_in_v3
21+
22+
raise_removed_in_v3(
23+
module="sagemaker.model",
24+
replacement="`ModelBuilder` in the sagemaker-serve package",
25+
v3_import="from sagemaker.serve import ModelBuilder",
26+
)
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License"). You
4+
# may not use this file except in compliance with the License. A copy of
5+
# the License is located at
6+
#
7+
# http://aws.amazon.com/apache2.0/
8+
#
9+
# or in the "license" file accompanying this file. This file is
10+
# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
11+
# ANY KIND, either express or implied. See the License for the specific
12+
# language governing permissions and limitations under the License.
13+
"""Removed in v3. This module stood in the v2 SageMaker Python SDK.
14+
15+
Importing ``sagemaker.multidatamodel`` raises an actionable ``ModuleNotFoundError`` that
16+
points to the v3 replacement. See the migration guide for details.
17+
"""
18+
from __future__ import absolute_import
19+
20+
from sagemaker.core.deprecations import raise_removed_in_v3
21+
22+
raise_removed_in_v3(
23+
module="sagemaker.multidatamodel",
24+
replacement="`ModelBuilder` in the sagemaker-serve package",
25+
v3_import="from sagemaker.serve import ModelBuilder",
26+
)
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License"). You
4+
# may not use this file except in compliance with the License. A copy of
5+
# the License is located at
6+
#
7+
# http://aws.amazon.com/apache2.0/
8+
#
9+
# or in the "license" file accompanying this file. This file is
10+
# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
11+
# ANY KIND, either express or implied. See the License for the specific
12+
# language governing permissions and limitations under the License.
13+
"""Removed in v3. This module stood in the v2 SageMaker Python SDK.
14+
15+
Importing ``sagemaker.pipeline`` raises an actionable ``ModuleNotFoundError`` that
16+
points to the v3 replacement. See the migration guide for details.
17+
"""
18+
from __future__ import absolute_import
19+
20+
from sagemaker.core.deprecations import raise_removed_in_v3
21+
22+
raise_removed_in_v3(
23+
module="sagemaker.pipeline",
24+
replacement="`Pipeline` in the sagemaker-mlops package",
25+
v3_import="from sagemaker.mlops.pipeline import Pipeline",
26+
)
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License"). You
4+
# may not use this file except in compliance with the License. A copy of
5+
# the License is located at
6+
#
7+
# http://aws.amazon.com/apache2.0/
8+
#
9+
# or in the "license" file accompanying this file. This file is
10+
# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
11+
# ANY KIND, either express or implied. See the License for the specific
12+
# language governing permissions and limitations under the License.
13+
"""Removed in v3. This module stood in the v2 SageMaker Python SDK.
14+
15+
Importing ``sagemaker.predictor`` raises an actionable ``ModuleNotFoundError`` that
16+
points to the v3 replacement. See the migration guide for details.
17+
"""
18+
from __future__ import absolute_import
19+
20+
from sagemaker.core.deprecations import raise_removed_in_v3
21+
22+
raise_removed_in_v3(
23+
module="sagemaker.predictor",
24+
replacement="the Endpoint resource in the sagemaker-core package",
25+
)

0 commit comments

Comments
 (0)