Skip to content

Fix ambiguities with BlockArrays.jl #39

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Feb 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "BlockSparseArrays"
uuid = "2c9a651f-6452-4ace-a6ac-809f4280fbb4"
authors = ["ITensor developers <[email protected]> and contributors"]
version = "0.2.12"
version = "0.2.13"

[deps]
Adapt = "79e6a3ab-5dfb-504d-930d-738a2a938a0e"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,15 @@
return similar_blocksparse(a, elt, axes)
end

# Fix ambiguity error with `BlockArrays.jl`.
function Base.similar(

Check warning on line 33 in ext/BlockSparseArraysGradedUnitRangesExt/BlockSparseArraysGradedUnitRangesExt.jl

View check run for this annotation

Codecov / codecov/patch

ext/BlockSparseArraysGradedUnitRangesExt/BlockSparseArraysGradedUnitRangesExt.jl#L33

Added line #L33 was not covered by tests
a::StridedArray,
elt::Type,
axes::Tuple{AbstractGradedUnitRange,Vararg{AbstractGradedUnitRange}},
)
return similar_blocksparse(a, elt, axes)

Check warning on line 38 in ext/BlockSparseArraysGradedUnitRangesExt/BlockSparseArraysGradedUnitRangesExt.jl

View check run for this annotation

Codecov / codecov/patch

ext/BlockSparseArraysGradedUnitRangesExt/BlockSparseArraysGradedUnitRangesExt.jl#L38

Added line #L38 was not covered by tests
end

# Fix ambiguity error with `BlockArrays.jl`.
function Base.similar(
a::StridedArray,
Expand Down Expand Up @@ -63,14 +72,21 @@
return getindex_blocksparse(a, I1, I_rest...)
end

# Fix ambiguity errors.
# Fix ambiguity error with Base.
function Base.getindex(a::Vector, I::AbstractGradedUnitRange)
return getindex_blocksparse(a, I)

Check warning on line 77 in ext/BlockSparseArraysGradedUnitRangesExt/BlockSparseArraysGradedUnitRangesExt.jl

View check run for this annotation

Codecov / codecov/patch

ext/BlockSparseArraysGradedUnitRangesExt/BlockSparseArraysGradedUnitRangesExt.jl#L76-L77

Added lines #L76 - L77 were not covered by tests
end

# Fix ambiguity error with BlockSparseArrays.jl.
function Base.getindex(
a::AnyAbstractBlockSparseArray,
I1::AbstractGradedUnitRange,
I_rest::AbstractGradedUnitRange...,
)
return getindex_blocksparse(a, I1, I_rest...)
end

# Fix ambiguity error with BlockSparseArrays.jl.
function Base.getindex(
a::AnyAbstractBlockSparseArray{<:Any,2},
I1::AbstractGradedUnitRange,
Expand Down
4 changes: 2 additions & 2 deletions test/test_aqua.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ using Aqua: Aqua
using Test: @testset

@testset "Code quality (Aqua.jl)" begin
# TODO: Fix Aqua issues and add this back.
# Aqua.test_all(BlockSparseArrays)
# TODO: Reenable ambiguity and piracy checks.
Aqua.test_all(BlockSparseArrays; ambiguities=false, piracies=false)
end
17 changes: 16 additions & 1 deletion test/test_gradedunitrangesext.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
using Test: @test, @testset
using BlockArrays:
AbstractBlockArray, Block, BlockedOneTo, blockedrange, blocklengths, blocksize
using BlockSparseArrays: BlockSparseArray, BlockSparseMatrix, blockstoredlength
using BlockSparseArrays:
BlockSparseArray, BlockSparseMatrix, BlockSparseVector, blockstoredlength
using GradedUnitRanges:
GradedUnitRanges,
GradedOneTo,
Expand Down Expand Up @@ -332,6 +333,20 @@ const elts = (Float32, Float64, Complex{Float32}, Complex{Float64})
@test iszero(b[Block(1, 2)])
@test b[Block(2, 2)] == a2
@test all(GradedUnitRanges.space_isequal.(axes(b), (r, dual(r))))

# Regression test for Vector, which caused
# an ambiguity error with Base.
r = gradedrange([U1(0) => 2, U1(1) => 3])
a1 = randn(elt, 2)
a2 = zeros(elt, 3)
a = vcat(a1, a2)
b = a[r]
@test eltype(b) === elt
@test b isa BlockSparseVector{elt}
@test blockstoredlength(b) == 1
@test b[Block(1)] == a1
@test iszero(b[Block(2)])
@test all(GradedUnitRanges.space_isequal.(axes(b), (r,)))
end
end
end