|
| 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 | +"""Tests for safe_serialize with PipelineVariable support. |
| 14 | +
|
| 15 | +Verifies that safe_serialize in sagemaker.train.utils correctly handles |
| 16 | +PipelineVariable objects (e.g., ParameterInteger, ParameterString) by |
| 17 | +returning them as-is rather than attempting str() conversion which would |
| 18 | +raise TypeError. |
| 19 | +
|
| 20 | +See: https://github.com/aws/sagemaker-python-sdk/issues/5504 |
| 21 | +""" |
| 22 | +from __future__ import absolute_import |
| 23 | + |
| 24 | +import pytest |
| 25 | + |
| 26 | +from sagemaker.train.utils import safe_serialize |
| 27 | +from sagemaker.core.helper.pipeline_variable import PipelineVariable |
| 28 | +from sagemaker.core.workflow.parameters import ParameterInteger, ParameterString |
| 29 | + |
| 30 | + |
| 31 | +class TestSafeSerializeWithPipelineVariables: |
| 32 | + """Test safe_serialize handles PipelineVariable objects correctly.""" |
| 33 | + |
| 34 | + def test_safe_serialize_with_parameter_integer(self): |
| 35 | + """ParameterInteger should be returned as-is (identity preserved).""" |
| 36 | + param = ParameterInteger(name="MaxDepth", default_value=5) |
| 37 | + result = safe_serialize(param) |
| 38 | + assert result is param |
| 39 | + assert isinstance(result, PipelineVariable) |
| 40 | + |
| 41 | + def test_safe_serialize_with_parameter_string(self): |
| 42 | + """ParameterString should be returned as-is (identity preserved).""" |
| 43 | + param = ParameterString(name="Algorithm", default_value="xgboost") |
| 44 | + result = safe_serialize(param) |
| 45 | + assert result is param |
| 46 | + assert isinstance(result, PipelineVariable) |
| 47 | + |
| 48 | + def test_safe_serialize_does_not_call_str_on_pipeline_variable(self): |
| 49 | + """Verify that PipelineVariable.__str__ is never invoked (would raise TypeError).""" |
| 50 | + param = ParameterInteger(name="TestParam", default_value=10) |
| 51 | + # This should NOT raise TypeError |
| 52 | + result = safe_serialize(param) |
| 53 | + assert result is param |
| 54 | + |
| 55 | + |
| 56 | +class TestSafeSerializeBasicTypes: |
| 57 | + """Regression tests: verify basic types still work after PipelineVariable support.""" |
| 58 | + |
| 59 | + def test_safe_serialize_with_string(self): |
| 60 | + """Strings should be returned as-is without JSON wrapping.""" |
| 61 | + assert safe_serialize("hello") == "hello" |
| 62 | + assert safe_serialize("12345") == "12345" |
| 63 | + |
| 64 | + def test_safe_serialize_with_int(self): |
| 65 | + """Integers should be JSON-serialized to string.""" |
| 66 | + assert safe_serialize(42) == "42" |
| 67 | + |
| 68 | + def test_safe_serialize_with_float(self): |
| 69 | + """Floats should be JSON-serialized to string.""" |
| 70 | + assert safe_serialize(3.14) == "3.14" |
| 71 | + |
| 72 | + def test_safe_serialize_with_dict(self): |
| 73 | + """Dicts should be JSON-serialized.""" |
| 74 | + result = safe_serialize({"key": "val"}) |
| 75 | + assert result == '{"key": "val"}' |
| 76 | + |
| 77 | + def test_safe_serialize_with_bool(self): |
| 78 | + """Booleans should be JSON-serialized.""" |
| 79 | + assert safe_serialize(True) == "true" |
| 80 | + assert safe_serialize(False) == "false" |
| 81 | + |
| 82 | + def test_safe_serialize_with_none(self): |
| 83 | + """None should be JSON-serialized to 'null'.""" |
| 84 | + assert safe_serialize(None) == "null" |
| 85 | + |
| 86 | + def test_safe_serialize_with_list(self): |
| 87 | + """Lists should be JSON-serialized.""" |
| 88 | + assert safe_serialize([1, 2, 3]) == "[1, 2, 3]" |
| 89 | + |
| 90 | + def test_safe_serialize_with_custom_object(self): |
| 91 | + """Custom objects should fall back to str().""" |
| 92 | + |
| 93 | + class CustomObj: |
| 94 | + def __str__(self): |
| 95 | + return "custom" |
| 96 | + |
| 97 | + assert safe_serialize(CustomObj()) == "custom" |
0 commit comments