Conversation
…nd column annotations check_instance only walks the arguments of dicts, lists, sets and tuples. For any other parameterized generic -- Callable[[int], int], type[int], frozenset[str], Iterator[int], or Hamilton's own column[pd.Series, float] -- it matched the origin and then fell through to isinstance(obj, type_), which raises TypeError for a parameterized generic. With the type-checker adapter on, any node annotated this way crashed the run even for correct values. Once the origin has matched, accept the value for these generics. Generated-by: Claude Code (Claude Opus 5.5) Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This branch has not been 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.
Closes #1738.
With
lifecycle.FunctionInputOutputTypeCheckerenabled, a node annotated withCallable[[int], int],type[int],frozenset[str],Iterator[int]or Hamilton's owncolumn[pd.Series, float]crashes the run, even when the value is correct.Why:
htypes.check_instancefirst checksisinstance(obj, origin), then walks the type arguments only for dicts, lists, sets and tuples. Any other parameterized generic falls out of that block to the finalreturn isinstance(obj, type_), withtype_still parameterized, andisinstanceraises. This is the same crash as #1736, which was for PEP 604 unions, but it happens in a different branch.Why the existing tests missed it: the
check_instancetests only uselist[...],dict[...], unions andLiteral. Each of those returns before the finalisinstance.Changes
hamilton/htypes.py,check_instance: when a parameterized generic has already matched its origin and isn't one of the element-checked containers, returnTrueinstead of callingisinstanceon the parameterized type. A value whose type doesn't match the origin is still rejected by the existingnot isinstance(obj, origin)check.tests/test_type_utils.py:test_check_instance_with_other_parameterized_genericscovers matching values fortyping.Callable[[int], int],type[int],frozenset[str],typing.Iterator[int],typing.Sequence[int](arange) andhtypes.column[pd.Series, float]. It also covers origin mismatches that must still returnFalse.tests/lifecycle/test_default.py:Callable[[int], int],frozenset[str]andcolumn[pd.Series, float]listreturned forfrozenset[str]is still rejected with the adapter's ownTypeErrorHow I tested this
main(f15267da, Python 3.12):test_check_instance_with_other_parameterized_genericsfails withTypeError: Subscripted generics cannot be used with class and instance checks.test_function_input_output_type_checker_handles_other_parameterized_genericsfails with the same error, raised fromrun_after_node_execution.mainand the fix.pytest tests/test_type_utils.py tests/lifecycle tests/test_hamilton_driver.py tests/test_base.py tests/function_modifiers tests/test_function_modifiers.py tests/test_end_to_end.py: 791 passed, 1 skipped.ruff checkandruff format --check(0.16.9, the pre-commit pin) are clean, andgit diff --checkis clean.Notes
frozenset[str]andIterator[int]get an origin-only check: their elements aren't validated. That is the mostcheck_instancecan do without consuming an iterator, and it matches how the adapter treatsCallable. I kept the element-checked set (dict, list, set, tuple) unchanged so this PR stays one change.check_instancemoved tohtypes(10a67af, 2024-03-11).Checklist
AI disclosure (per the ASF Generative Tooling guidance): an AI coding tool (Claude Code, Claude Opus 5.5) wrote this change through the
breken-aiaccount. The tool found the bug and wrote the fix, the tests and this description. The commit carries aGenerated-by:trailer. The diff is a small original change to existing Hamilton code and includes no third-party material. The red/green runs above are real and can be re-run from the diff. If you would rather not take AI-assisted contributions here, say so and I will close this.🤖 Generated with Claude Code