Skip to content

[Type] ndarray typing 2: Fix NDArray single-arg subscript crash#412

Draft
hughperkins wants to merge 2 commits intohp/typing-t4-1-eval-strfrom
hp/typing-t4-2-ndarray-subscript
Draft

[Type] ndarray typing 2: Fix NDArray single-arg subscript crash#412
hughperkins wants to merge 2 commits intohp/typing-t4-1-eval-strfrom
hp/typing-t4-2-ndarray-subscript

Conversation

@hughperkins
Copy link
Collaborator

NdarrayType.class_getitem crashed when called with a single arg (e.g. NdarrayType[dtype]) because it tried to unpack a non-tuple. Wrap single args in a tuple before passing to init.

Issue: #

Brief Summary

copilot:summary

Walkthrough

copilot:walkthrough

@hughperkins hughperkins force-pushed the hp/typing-t4-2-ndarray-subscript branch from 219619c to 5762be5 Compare March 12, 2026 03:49
@hughperkins
Copy link
Collaborator Author

Opus 4.6 review:

PR Review: hp/typing-t4-2-ndarray-subscript

Summary

This PR fixes NdarrayType.__class_getitem__ to correctly handle single-argument subscript notation (e.g. NdarrayType[qd.i32]). Python's subscript protocol passes a single value directly (not a tuple) when there is one item, so the change normalizes args to a tuple before unpacking. It also removes the unused **kwargs parameter.

Issues Found

No issues found. The change is correct and all 190 ndarray-related tests pass.

Suggestions for Improvement

  1. Add a test for single-argument subscript — The codebase uses NDArray[dtype, ndim] extensively but I found no tests for NDArray[dtype] (dtype-only). Consider adding a test in test_ndarray_typing.py:

    def test_ndarray_typing_single_arg():
        t = qd.types.NDArray[qd.i32]
        assert t.dtype == qd.i32
        assert t.ndim is None
  2. Optional: handle NdarrayType[] explicitly — NdarrayType[()] works (empty tuple → cls()), but NdarrayType[] is invalid Python syntax. No change needed; this is just for awareness.

  3. Style — The implementation is consistent with the codebase. The tuple normalization pattern is a common idiom for __class_getitem__ (e.g. similar to typing.Generic).

@hughperkins hughperkins force-pushed the hp/typing-t4-2-ndarray-subscript branch from 5762be5 to a402c99 Compare March 12, 2026 04:33
NdarrayType.__class_getitem__ crashed when called with a single arg
(e.g. NdarrayType[dtype]) because it tried to unpack a non-tuple.
Wrap single args in a tuple before passing to __init__.
Test that NDArray[dtype] (without ndim) works and produces an
NdarrayType with the correct dtype and ndim=None.
@hughperkins hughperkins force-pushed the hp/typing-t4-2-ndarray-subscript branch from f0a7816 to 050f3a7 Compare March 12, 2026 04:37
@hughperkins hughperkins marked this pull request as ready for review March 12, 2026 04:55
@hughperkins hughperkins marked this pull request as draft March 12, 2026 04:59
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.

1 participant