Skip to content

Commit 27e0e7d

Browse files
authored
Fix return type when getting attribute of empty vector (#2501)
1 parent d5954b4 commit 27e0e7d

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

src/attributes.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -396,6 +396,9 @@ end
396396
# get(::ModelLike, ::AbstractVariableAttribute, ::Vector{VariableIndex})
397397
# would not allow us to define get(::SomeModel, ::AnyAttribute, ::Vector).
398398
function get(model::ModelLike, attr::AnyAttribute, idxs::Vector)
399+
if isempty(idxs)
400+
return Vector{attribute_value_type(attr)}()
401+
end
399402
return get.(model, attr, idxs)
400403
end
401404

test/attributes.jl

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,23 @@ function test_attributes_AutomaticDifferentiationBackend()
316316
return
317317
end
318318

319+
function test_empty_vector_attribute()
320+
model = MOI.Utilities.Model{Float64}()
321+
x = MOI.get(model, MOI.ListOfVariableIndices())
322+
@test typeof(x) == Vector{MOI.VariableIndex}
323+
ret = MOI.get(model, MOI.VariablePrimalStart(), x)
324+
@test typeof(ret) == Vector{Any}
325+
ret = MOI.get(model, MOI.VariableName(), x)
326+
@test typeof(ret) == Vector{String}
327+
F, S = MOI.ScalarAffineFunction{Float64}, MOI.EqualTo{Float64}
328+
c = MOI.get(model, MOI.ListOfConstraintIndices{F,S}())
329+
ret = MOI.get(model, MOI.ConstraintPrimalStart(), c)
330+
@test typeof(ret) == Vector{Any}
331+
ret = MOI.get(model, MOI.ConstraintName(), c)
332+
@test typeof(ret) == Vector{String}
333+
return
334+
end
335+
319336
function runtests()
320337
for name in names(@__MODULE__; all = true)
321338
if startswith("$name", "test_")

0 commit comments

Comments
 (0)