generated from amazon-archives/__template_Apache-2.0
-
Notifications
You must be signed in to change notification settings - Fork 28
Add a config property descriptor along with a custom resolver and validators #642
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
ubaskota
merged 7 commits into
smithy-lang:config_resolution_main
from
ubaskota:config_property
Feb 28, 2026
+706
−0
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
bdccfa3
Add a config property descriptor along with a custom resolver and val…
ubaskota 4eb1b76
Remove a decorator that handles None value and update the tests
ubaskota 09f2546
Address reviews and comments
ubaskota 3550baf
Address reviews
ubaskota b44493a
Address reviews
ubaskota d346728
Fix and add tests
ubaskota 1a21a17
Address reviews
ubaskota File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
50 changes: 50 additions & 0 deletions
50
packages/smithy-aws-core/src/smithy_aws_core/config/custom_resolvers.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| # Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| from smithy_core.config.resolver import ConfigResolver | ||
| from smithy_core.retries import RetryStrategyOptions | ||
|
|
||
| from smithy_aws_core.config.validators import validate_max_attempts, validate_retry_mode | ||
|
|
||
|
|
||
| def resolve_retry_strategy( | ||
| resolver: ConfigResolver, | ||
| ) -> tuple[RetryStrategyOptions | None, str | None]: | ||
| """Resolve retry strategy from multiple config keys. | ||
|
|
||
| Resolves both retry_mode and max_attempts from sources and constructs | ||
| a RetryStrategyOptions object. This allows the retry strategy to be | ||
| configured from multiple sources. Example: retry_mode from config file and | ||
| max_attempts from environment variables. | ||
|
|
||
| :param resolver: The config resolver to use for resolution | ||
|
|
||
| :returns: Tuple of (RetryStrategyOptions, source_name) if both retry_mode and max_attempts | ||
| are resolved. Returns (None, None) if both values are missing. | ||
|
|
||
| For mixed sources, the source name includes both component sources: | ||
| "retry_mode=environment, max_attempts=config_file" | ||
| """ | ||
|
|
||
| retry_mode, mode_source = resolver.get("retry_mode") | ||
|
|
||
| max_attempts, attempts_source = resolver.get("max_attempts") | ||
|
|
||
| if retry_mode is None and max_attempts is None: | ||
| return None, None | ||
|
|
||
| if retry_mode is not None: | ||
| retry_mode = validate_retry_mode(retry_mode, mode_source) | ||
|
|
||
| if max_attempts is not None: | ||
| max_attempts = validate_max_attempts(max_attempts, attempts_source) | ||
|
|
||
| options = RetryStrategyOptions( | ||
| retry_mode=retry_mode, # type: ignore | ||
| max_attempts=max_attempts, # Can be None because strategy will use its default | ||
| ) | ||
|
|
||
| # Construct mixed source string showing where each component came from | ||
| source = f"retry_mode={mode_source or 'default'}, max_attempts={attempts_source or 'default'}" | ||
|
|
||
| return (options, source) | ||
126 changes: 126 additions & 0 deletions
126
packages/smithy-aws-core/src/smithy_aws_core/config/validators.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,126 @@ | ||
| # Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| import re | ||
| from typing import Any, get_args | ||
|
|
||
| from smithy_core.interfaces.retries import RetryStrategy | ||
| from smithy_core.retries import RetryStrategyOptions, RetryStrategyType | ||
|
|
||
|
|
||
| class ConfigValidationError(ValueError): | ||
| """Raised when a configuration value fails validation.""" | ||
|
|
||
| def __init__(self, key: str, value: Any, reason: str, source: str | None = None): | ||
| self.key = key | ||
| self.value = value | ||
| self.reason = reason | ||
| self.source = source | ||
|
|
||
| msg = f"Invalid value for '{key}': {value!r}. {reason}" | ||
| if source: | ||
| msg += f" (from source: {source})" | ||
| super().__init__(msg) | ||
|
|
||
|
|
||
| def validate_region(region: str, source: str | None = None) -> str: | ||
| """Validate region name format. | ||
|
|
||
| :param region: The value to validate | ||
| :param source: The config source that provided this value | ||
|
|
||
| :returns: The validated value | ||
|
|
||
| :raises ConfigValidationError: If the value format is invalid | ||
| """ | ||
| pattern = r"^(?![0-9]+$)(?!-)[a-zA-Z0-9-]{1,63}(?<!-)$" | ||
|
|
||
| if not re.match(pattern, region): | ||
| raise ConfigValidationError( | ||
| "region", | ||
| region, | ||
| "region doesn't match the pattern.", | ||
| source, | ||
| ) | ||
| return region | ||
|
|
||
|
|
||
| def validate_retry_mode(retry_mode: str, source: str | None = None) -> str: | ||
| """Validate retry mode. | ||
|
|
||
| Valid values: 'standard', 'simple' | ||
|
|
||
| :param retry_mode: The retry mode value to validate | ||
| :param source: The source that provided this value | ||
|
|
||
| :returns: The validated retry mode string | ||
|
|
||
| :raises: ConfigValidationError: If the retry mode is invalid | ||
| """ | ||
|
|
||
| valid_modes = get_args(RetryStrategyType) | ||
|
|
||
| if retry_mode not in valid_modes: | ||
| raise ConfigValidationError( | ||
| "retry_mode", | ||
| retry_mode, | ||
| f"retry_mode must be one of {valid_modes}, got {retry_mode}", | ||
| source, | ||
| ) | ||
|
|
||
| return retry_mode | ||
|
|
||
|
|
||
| def validate_max_attempts(max_attempts: str | int, source: str | None = None) -> int: | ||
| """Validate and convert max_attempts to integer. | ||
|
|
||
| :param max_attempts: The max attempts value (string or int) | ||
| :param source: The source that provided this value | ||
|
|
||
| :returns: The validated max_attempts as an integer | ||
|
|
||
| :raises ConfigValidationError: If the value is less than 1 or cannot be converted to an integer | ||
| """ | ||
| try: | ||
| max_attempts = int(max_attempts) | ||
| except (ValueError, TypeError): | ||
| raise ConfigValidationError( | ||
| "max_attempts", | ||
| max_attempts, | ||
| f"max_attempts must be a number, got {type(max_attempts).__name__}", | ||
| source, | ||
| ) | ||
|
|
||
| if max_attempts < 1: | ||
| raise ConfigValidationError( | ||
| "max_attempts", | ||
| max_attempts, | ||
| f"max_attempts must be a positive integer, got {max_attempts}", | ||
| source, | ||
| ) | ||
|
|
||
| return max_attempts | ||
|
|
||
|
|
||
| def validate_retry_strategy( | ||
| value: Any, source: str | None = None | ||
| ) -> RetryStrategy | RetryStrategyOptions: | ||
| """Validate retry strategy configuration. | ||
|
|
||
| :param value: The retry strategy value to validate | ||
| :param source: The source that provided this value | ||
|
|
||
| :returns: The validated retry strategy (RetryStrategy or RetryStrategyOptions) | ||
|
|
||
| :raises: ConfigValidationError: If the value is not a valid retry strategy type | ||
| """ | ||
|
|
||
| if isinstance(value, RetryStrategy | RetryStrategyOptions): | ||
| return value | ||
|
|
||
| raise ConfigValidationError( | ||
| "retry_strategy", | ||
| value, | ||
| f"retry_strategy must be RetryStrategy or RetryStrategyOptions, got {type(value).__name__}", | ||
| source, | ||
| ) |
96 changes: 96 additions & 0 deletions
96
packages/smithy-aws-core/tests/unit/config/test_custom_resolver.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,96 @@ | ||
| # Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| from typing import Any | ||
|
|
||
| from smithy_aws_core.config.custom_resolvers import resolve_retry_strategy | ||
| from smithy_core.config.resolver import ConfigResolver | ||
| from smithy_core.retries import RetryStrategyOptions | ||
|
|
||
|
|
||
| class StubSource: | ||
| """A simple ConfigSource implementation for testing.""" | ||
|
|
||
| def __init__(self, source_name: str, data: dict[str, Any] | None = None) -> None: | ||
| self._name = source_name | ||
| self._data = data or {} | ||
|
|
||
| @property | ||
| def name(self) -> str: | ||
| return self._name | ||
|
|
||
| def get(self, key: str) -> Any | None: | ||
| return self._data.get(key) | ||
|
|
||
|
|
||
| class TestResolveCustomResolverRetryStrategy: | ||
| """Test suite for complex configuration resolution""" | ||
|
|
||
| def test_resolves_from_both_values(self) -> None: | ||
| # When both retry mode and max attempts are set | ||
| # It should use source names for both values | ||
| source = StubSource( | ||
| "environment", {"retry_mode": "standard", "max_attempts": "3"} | ||
| ) | ||
| resolver = ConfigResolver(sources=[source]) | ||
|
|
||
| result, source_name = resolve_retry_strategy(resolver) | ||
|
|
||
| assert isinstance(result, RetryStrategyOptions) | ||
| assert result.retry_mode == "standard" | ||
| assert result.max_attempts == 3 | ||
| assert source_name == "retry_mode=environment, max_attempts=environment" | ||
|
|
||
| def test_tracks_different_sources_for_each_component(self) -> None: | ||
| source1 = StubSource("environment", {"retry_mode": "standard"}) | ||
| source2 = StubSource("config_file", {"max_attempts": "5"}) | ||
| resolver = ConfigResolver(sources=[source1, source2]) | ||
|
|
||
| result, source_name = resolve_retry_strategy(resolver) | ||
|
|
||
| assert isinstance(result, RetryStrategyOptions) | ||
| assert result.retry_mode == "standard" | ||
| assert result.max_attempts == 5 | ||
| assert source_name == "retry_mode=environment, max_attempts=config_file" | ||
|
|
||
| def test_converts_max_attempts_string_to_int(self) -> None: | ||
| source = StubSource( | ||
| "environment", {"max_attempts": "10", "retry_mode": "standard"} | ||
| ) | ||
| resolver = ConfigResolver(sources=[source]) | ||
|
|
||
| result, _ = resolve_retry_strategy(resolver) | ||
|
|
||
| assert isinstance(result, RetryStrategyOptions) | ||
| assert result.max_attempts == 10 | ||
| assert isinstance(result.max_attempts, int) | ||
|
|
||
| def test_returns_strategy_when_only_retry_mode_set(self) -> None: | ||
| source = StubSource("environment", {"retry_mode": "standard"}) | ||
| resolver = ConfigResolver(sources=[source]) | ||
|
|
||
| result, source_name = resolve_retry_strategy(resolver) | ||
|
|
||
| assert isinstance(result, RetryStrategyOptions) | ||
| assert result.retry_mode == "standard" | ||
| assert result.max_attempts is None | ||
| assert source_name == "retry_mode=environment, max_attempts=default" | ||
|
|
||
| def test_returns_strategy_when_only_max_attempts_set(self) -> None: | ||
| source = StubSource("environment", {"max_attempts": "5"}) | ||
| resolver = ConfigResolver(sources=[source]) | ||
|
|
||
| result, source_name = resolve_retry_strategy(resolver) | ||
|
|
||
| assert isinstance(result, RetryStrategyOptions) | ||
| assert result.max_attempts == 5 | ||
| assert source_name == "retry_mode=default, max_attempts=environment" | ||
|
|
||
| def test_returns_none_when_both_values_missing(self) -> None: | ||
| source = StubSource("environment", {}) | ||
| resolver = ConfigResolver(sources=[source]) | ||
|
|
||
| result, source_name = resolve_retry_strategy(resolver) | ||
|
|
||
| assert result is None | ||
| assert source_name is None |
51 changes: 51 additions & 0 deletions
51
packages/smithy-aws-core/tests/unit/config/test_validators.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| # Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
| from typing import Any | ||
|
|
||
| import pytest | ||
| from smithy_aws_core.config.validators import ( | ||
| ConfigValidationError, | ||
| validate_max_attempts, | ||
| validate_region, | ||
| validate_retry_mode, | ||
| ) | ||
|
|
||
|
|
||
| class TestValidators: | ||
| @pytest.mark.parametrize("region", ["us-east-1", "eu-west-1", "ap-south-1"]) | ||
| def test_validate_region_accepts_valid_values(self, region: str) -> None: | ||
| assert validate_region(region) == region | ||
|
|
||
| @pytest.mark.parametrize("invalid", ["-invalid", "-east", "12345", ""]) | ||
| def test_validate_region_rejects_invalid_values(self, invalid: str) -> None: | ||
| with pytest.raises(ConfigValidationError): | ||
| validate_region(invalid) | ||
|
|
||
| @pytest.mark.parametrize("mode", ["standard", "simple"]) | ||
| def test_validate_retry_mode_accepts_valid_values(self, mode: str) -> None: | ||
| assert validate_retry_mode(mode) == mode | ||
|
|
||
| @pytest.mark.parametrize("invalid_mode", ["some_retry", "some_retry_one", ""]) | ||
| def test_validate_retry_mode_rejects_invalid_values( | ||
| self, invalid_mode: str | ||
| ) -> None: | ||
| with pytest.raises(ConfigValidationError): | ||
| validate_retry_mode(invalid_mode) | ||
|
|
||
| @pytest.mark.parametrize("invalid_max_attempts", ["abcd", 0, -1]) | ||
| def test_validate_invalid_max_attempts_raises_error( | ||
| self, invalid_max_attempts: Any | ||
| ) -> None: | ||
| with pytest.raises( | ||
| ConfigValidationError, | ||
| match=r"(max_attempts must be a number|max_attempts must be a positive integer)", | ||
| ): | ||
| validate_max_attempts(invalid_max_attempts) | ||
|
|
||
| def test_invalid_retry_mode_error_message(self) -> None: | ||
| with pytest.raises(ConfigValidationError) as exc_info: | ||
| validate_retry_mode("random_mode") | ||
| assert ( | ||
| "Invalid value for 'retry_mode': 'random_mode'. retry_mode must be one " | ||
| "of ('simple', 'standard'), got random_mode" in str(exc_info.value) | ||
| ) |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.