Skip to content

fix: allow PipelineVariable keys in HyperparameterTuner hyperparameter_ranges annotation (v2) - #6327

Merged
mohamedzeidan2021 merged 1 commit into
aws:master-v2from
mohamedzeidan2021:fix/issue-5243-tuner-hyperparameter-ranges-annotation-v2
Sep 28, 2026
Merged

mohamedzeidan2021 merged 1 commit into
aws:master-v2from
mohamedzeidan2021:fix/issue-5243-tuner-hyperparameter-ranges-annotation-v2

Conversation

@mohamedzeidan2021

Copy link
Copy Markdown
Collaborator

Issue

Fixes #5243 (v2 backport — companion to #6314 which fixes the same annotation in v3/sagemaker-train).

The HyperparameterTuner constructor annotates hyperparameter_ranges as Dict[str, ParameterRange]. However, 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. Because the annotation only allows str keys, mypy reports a false positive on valid code:

error: Dict entry 0 has incompatible type "ParameterString": "CategoricalParameter";
       expected "str": "ParameterRange"  [dict-item]

Fix

Broaden the key type to Union[str, PipelineVariable]:

hyperparameter_ranges: Dict[Union[str, PipelineVariable], ParameterRange],

Union and PipelineVariable are already imported in the module. The __init__ and create() docstrings are updated to note that keys may be a str or a PipelineVariable. This is an annotation/docstring-only change — runtime behavior is unchanged (annotations are not enforced, and _validate_parameter_ranges already accepts any key), and ParameterString subclasses PipelineVariable, so the union is exactly right.

Testing

tests/unit/test_tuner.py:

  • New test_hyperparameter_ranges_annotation_allows_pipeline_variable_keys inspects the resolved __init__ annotation and asserts the key union contains both str and PipelineVariable. This is the regression guard — it fails if the type is narrowed back to Dict[str, ParameterRange].
  • New test_init_with_pipeline_variable_hyperparameter_key constructs a tuner with a ParameterString key as a runtime sanity check (documented as not being the regression guard, since annotations aren't runtime-enforced).

All 107 tests in test_tuner.py pass. Reproduced the mypy dict-item error against the unpatched annotation and confirmed it is cleared by the fix. black (line-length 100) and flake8 (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.

…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
@mohamedzeidan2021
mohamedzeidan2021 merged commit f2c42b4 into aws:master-v2 Sep 28, 2026
9 of 11 checks passed

This branch was successfully deployed

1 active deployment
auto-approve — 37a3a761 Deployed Sep 25, 2026 by mohamedzeidan2021 via wait-for-approval #231
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants