Skip to content

dimension fetches fist item of AbstractArray even if empty #224

Open

Description

@BambOoxX

While wrapping QuantityArrays with KeyedArray from AxisKeys.jl, I met an edge case of dimension with empty arrays.

] activate --temp
] add AxisKeys
] add DynamicQuantities

A = KeyedArray(QuantityArray(Matrix{Float64}(undef,0,6),us"s"); time = Float64[], channel = ["$i"  for i in 1:6])
dimension(A)

This throws

julia> dimension(A)
ERROR: BoundsError: attempt to access 0×6 NamedDimsArray(QuantityArray(::Matrix{Float64}, ::Quantity{Float64, SymbolicDimensions{FRInt32}}), (:time, :channel)) at index [1]
Stacktrace:
 [1] throw_boundserror(A::NamedDimsArray{(:time, :channel), Quantity{…}, 2, QuantityArray{…}}, I::Tuple{Int64})
   @ Base .\essentials.jl:15
 [2] checkbounds
   @ .\abstractarray.jl:699 [inlined]
 [3] getindex
   @ D:\bamboo\.julia\packages\AxisKeys\a6h2b\src\struct.jl:76 [inlined]
 [4] first
   @ .\abstractarray.jl:452 [inlined]
 [5] dimension(aq::KeyedArray{Quantity{…}, 2, NamedDimsArray{…}, Tuple{…}})
   @ DynamicQuantities D:\bamboo\.julia\packages\DynamicQuantities\wqpWc\src\utils.jl:442
 [6] top-level scope
   @ REPL[26]:1
Some type information was truncated. Use `show(err)` to see complete types.

at

dimension(aq::AbstractArray{<:UnionAbstractQuantity}) = allequal(dimension.(aq)) ? dimension(first(aq)) : throw(DimensionError(aq[begin], aq[begin+1:end]))

I am not sure how this should be circumvented. If the array is empty, it is obvious that all elements should be of the same type. It may require an additional dispatch.

Activity

  1. MilesCranmer commented on Jun 8, 2026

    @MilesCranmer
    Member

    Thanks! Yeah, I guess that dimension(::QuantityArray) should definitely work, even when the array is empty. I am surprised it is getting forwarded to the AbstractArray call... strange. @MilesCranmerBot can you please make a PR fixing this?

    I am not sure how to handle the case of an empty AbstractArray. Maybe just promote the quantity type and return a dimensionless 1.0? But maybe that is a second issue.

  2. MilesCranmerBot commented on Jun 8, 2026

    @MilesCranmerBot
    Contributor

    Opened PR: #226

    Commit: 372fae1

    Verified locally:

    • Targeted empty QuantityArray/parent-wrapper check passed
    • Pkg.test(; test_args=["Assorted unittests"]) passed (10364 pass, 6 broken)
    • Direct AxisKeys reproduction from the issue passed in a temporary environment

    Current PR checks are partly green with Julia 1 and docs still pending.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions