fix: allow PipelineVariable keys in HyperparameterTuner hyperparameter_ranges annotation - #6314
Open
mohamedzeidan2021 wants to merge 2 commits into
Conversation
…r_ranges annotation hyperparameter_ranges was annotated Dict[str, ParameterRange], but the tuner accepts a PipelineVariable (e.g. a pipeline ParameterString) as a dict key (hyperparameter name). mypy therefore reported a false dict-item error for valid code. Broaden the key type to Union[str, PipelineVariable] and align the docstrings. Runtime behavior is unchanged (annotations are not enforced). Fixes aws#5243
|
Claude finished @mohamedzeidan2021's task in 54s —— View job Review: PR #6314 — allow PipelineVariable keys in
|
|
Claude finished @mohamedzeidan2021's task in 45s —— View job Review: allow
|
This branch was successfully deployed
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Issue
Fixes #5243
The v3
HyperparameterTunerconstructor annotateshyperparameter_rangesasDict[str, ParameterRange]. However, the tuner accepts aPipelineVariable(e.g. a pipelineParameterString) as a dict key — the hyperparameter name — which is a documented, working pattern when building a tuning step inside a pipeline. Because the annotation only allowsstrkeys,mypyreports a false positive on valid code:Fix
Broaden the key type to
Union[str, PipelineVariable]:UnionandPipelineVariableare already imported in the module. The__init__andcreate()docstrings are updated to note that keys may be astror aPipelineVariable. This is an annotation/docstring-only change — runtime behavior is unchanged (annotations are not enforced, and_validate_parameter_rangesalready accepts any key), andParameterStringsubclassesPipelineVariable, so the union is exactly right.Testing
sagemaker-train/tests/unit/train/test_tuner.py:test_hyperparameter_ranges_annotation_allows_pipeline_variable_keysinspects the resolved__init__annotation and asserts the key union contains bothstrandPipelineVariable. This is the regression guard — it fails if the type is narrowed back toDict[str, ParameterRange](verified viagit stash).test_init_with_pipeline_variable_hyperparameter_keyconstructs a tuner with aParameterStringkey as a runtime sanity check (documented in the test as not being the regression guard, since annotations aren't runtime-enforced).All 45 tests in
test_tuner.pypass;black(line-length 100) andflake8(line-length 120) clean.Backwards compatibility
Widening a parameter's accepted type is backwards compatible — every existing
Dict[str, ParameterRange]argument still satisfies the broader union. No runtime effect. This is the only signature-level annotation of this parameter in the repo.