fix: allow PipelineVariable keys in HyperparameterTuner hyperparameter_ranges annotation (v2) - #6327
Merged
mohamedzeidan2021 merged 1 commit intoSep 28, 2026
Conversation
…r_ranges annotation The HyperparameterTuner constructor annotated hyperparameter_ranges as Dict[str, ParameterRange], but the tuner accepts a PipelineVariable (e.g. a pipeline ParameterString) as a dict key -- the hyperparameter name -- which is a documented, working pattern when building a tuning step inside a pipeline. The narrow annotation makes mypy report a false positive (dict-item) on valid code. Broaden the key type to Union[str, PipelineVariable] (Union and PipelineVariable are already imported) and update the __init__ and create() docstrings. Annotation/docstring-only: runtime behavior is unchanged and ParameterString subclasses PipelineVariable, so the union is exact. Adds an annotation regression guard plus a runtime sanity test. Fixes aws#5243
jam-jee
approved these changes
Sep 28, 2026
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 (v2 backport — companion to #6314 which fixes the same annotation in v3/
sagemaker-train).The
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
tests/unit/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].test_init_with_pipeline_variable_hyperparameter_keyconstructs a tuner with aParameterStringkey as a runtime sanity check (documented as not being the regression guard, since annotations aren't runtime-enforced).All 107 tests in
test_tuner.pypass. Reproduced themypydict-itemerror against the unpatched annotation and confirmed it is cleared by the fix.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.