Registry Mirror
Skip to content

Conversation

@headtr1ck

@headtr1ck headtr1ck commented Oct 8, 2026 •

Copy link
Copy Markdown
Collaborator

Description

Follow-up of #11677: Variable is now generic in its shape and dtype as well, like NamedArray: Variable[ShapeType, DType, DimType].

  • The constructor infers the dtype from duck arrays, other Variables and Python scalars and lists, e.g. Variable("x", [1, 2]) has an integer dtype (also in NamedArray's from_array); other data gives Any
  • np.asarray(variable) keeps the dtype
  • Methods that can change the shape or dtype return a Variable with the respective types instead of Self, e.g. comparisons have a boolean dtype; the generated arithmetic mixin is generic too
  • Operators also return a plain Variable for subclasses, as they do at runtime; in-place operators keep Self

Checklist

  • Tests added
  • User visible changes (including notable bug fixes) are documented in whats-new.rst

AI Disclosure

  • This PR contains AI-generated content.
    • I have tested any AI-generated content in my PR.
    • I take responsibility for any AI-generated content in my PR.
      Tools: Claude Code

[This is Claude Code on behalf of Michael Niklas]

🤖 Generated with Claude Code

headtr1ck and others added 2 commits October 8, 2026 14:50
Variable now takes the same type parameters as NamedArray,
Variable[ShapeType, DType, DimType]. The constructor infers the dtype from duck
arrays; the shape type is tuple[int, ...], as it cannot be inferred and Any
would make mypy drop all type arguments in overloaded functions.

Methods that returned Self although they can change the shape or dtype now
return a Variable with the respective types: indexing, transpose, reductions,
astype, real/imag, ufuncs and the arithmetic operators, whose generated
mixin is now generic too. Comparisons have a boolean dtype, argsort an intp
dtype. Binary and unary operators also return a Variable for subclasses, like
at runtime; in-place operators keep returning Self.

Co-authored-by: Claude <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: Claude <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@github-actions github-actions Bot added the topic-NamedArray Lightweight version of Variable label Oct 8, 2026
headtr1ck and others added 4 commits October 8, 2026 16:05
Add overloads to Variable, IndexVariable and NamedArray's from_array for
scalars and lists (nested up to three levels) of bool, int, float, complex
and str, which numpy converts to arrays of a known dtype. They are tried in
order, so that e.g. a list literal of ints is not taken for a list of floats;
an empty list becomes float64. Tuples are not included, as they become 0-d
object arrays.

Co-authored-by: Claude <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: Claude <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: Claude <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
AbstractArray.__array__ cannot type the dtype, as AbstractArray is not generic
in it. Add typing-only overloads to Variable, so that e.g. np.asarray(variable)
keeps the dtype.

Co-authored-by: Claude <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@headtr1ck headtr1ck added topic-typing plan to merge Final call for comments and removed topic-NamedArray Lightweight version of Variable labels Oct 8, 2026
@headtr1ck
headtr1ck enabled auto-merge (squash) October 12, 2026 10:34
Co-authored-by: Claude <noreply@anthropic.com>
@github-actions github-actions Bot added the topic-NamedArray Lightweight version of Variable label Oct 12, 2026
@headtr1ck
headtr1ck merged commit 466ca4f into pydata:main Oct 12, 2026
44 of 46 checks passed
@headtr1ck
headtr1ck deleted the variable-generic-shape-dtype branch October 12, 2026 11:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

plan to merge Final call for comments topic-NamedArray Lightweight version of Variable topic-typing

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant