Skip to content
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

Fix return type when getting attribute of empty vector #2501

Merged
merged 1 commit into from
May 13, 2024
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
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 @@
# 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
Loading