Skip to content

Commit

Permalink
Fix return type when getting attribute of empty vector
Browse files Browse the repository at this point in the history
  • Loading branch information
odow committed May 12, 2024
1 parent d5954b4 commit 37fcc40
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/attributes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,9 @@ end
# get(::ModelLike, ::AbstractVariableAttribute, ::Vector{VariableIndex})
# would not allow us to define get(::SomeModel, ::AnyAttribute, ::Vector).
function get(model::ModelLike, attr::AnyAttribute, idxs::Vector)
if isempty(idxs)
return Vector{attribute_value_type(attr)}()

Check warning on line 400 in src/attributes.jl

View check run for this annotation

Codecov / codecov/patch

src/attributes.jl#L400

Added line #L400 was not covered by tests
end
return get.(model, attr, idxs)
end

Expand Down
17 changes: 17 additions & 0 deletions test/attributes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,23 @@ function test_attributes_AutomaticDifferentiationBackend()
return
end

function test_empty_vector_attribute()
model = MOI.Utilities.Model{Float64}()
x = MOI.get(model, MOI.ListOfVariableIndices())
@test typeof(x) == Vector{MOI.VariableIndex}
ret = MOI.get(model, MOI.VariablePrimalStart(), x)
@test typeof(ret) == Vector{Any}
ret = MOI.get(model, MOI.VariableName(), x)
@test typeof(ret) == Vector{String}
F, S = MOI.ScalarAffineFunction{Float64}, MOI.EqualTo{Float64}
c = MOI.get(model, MOI.ListOfConstraintIndices{F,S}())
ret = MOI.get(model, MOI.ConstraintPrimalStart(), c)
@test typeof(ret) == Vector{Any}
ret = MOI.get(model, MOI.ConstraintName(), c)
@test typeof(ret) == Vector{String}
return
end

function runtests()
for name in names(@__MODULE__; all = true)
if startswith("$name", "test_")
Expand Down

0 comments on commit 37fcc40

Please sign in to comment.