Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
odow committed Oct 7, 2024
1 parent 157f7ae commit a0d67eb
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 8 deletions.
32 changes: 25 additions & 7 deletions src/optimizer_interface.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1251,9 +1251,25 @@ function get_attribute(
return get_attribute(model, String(name))
end

_string_if_abstract_string(value) = value
_string_if_abstract_string(value::String) = value
_string_if_abstract_string(value::AbstractString) = String(value)
# Some MOI attributes have a strict value type, like ::String or ::Float64, but
# users may pass other generic subtypes, like SubString instead of String.
# Rather than throw obtuse errors, we can catch and fix some common cases. We
# shouldn't fix every case (e.g., AbstractString -> String) because users might

Check failure on line 1257 in src/optimizer_interface.jl

View workflow job for this annotation

GitHub Actions / build

[vale] reported by reviewdog 🐶 [Google.Latin] Use 'for example' instead of 'e.g.,'. Raw Output: {"message": "[Google.Latin] Use 'for example' instead of 'e.g.,'.", "location": {"path": "src/optimizer_interface.jl", "range": {"start": {"line": 1257, "column": 29}}}, "severity": "ERROR"}
# intentionally want the other subtype.
#
# The default case is to do nothing:
_to_concrete_value_type_if_needed(::MOI.AnyAttribute, value) = value

# A common case is passing an AbstractString instead of a String.
function _to_concrete_value_type_if_needed(
attr::MOI.AnyAttribute,
value::AbstractString,
)
if !(value isa String) && MOI.attribute_value_type(attr) === String
return String(value)
end
return value
end

"""
set_attribute(model::GenericModel, attr::MOI.AbstractModelAttribute, value)
Expand Down Expand Up @@ -1289,7 +1305,7 @@ function set_attribute(
attr::MOI.AbstractModelAttribute,
value,
)
MOI.set(model, attr, _string_if_abstract_string(value))
MOI.set(model, attr, _to_concrete_value_type_if_needed(attr, value))
return
end

Expand All @@ -1298,7 +1314,8 @@ function set_attribute(
attr::MOI.AbstractVariableAttribute,
value,
)
MOI.set(owner_model(x), attr, x, _string_if_abstract_string(value))
model = owner_model(x)
MOI.set(model, attr, x, _to_concrete_value_type_if_needed(attr, value))
return
end

Expand All @@ -1307,7 +1324,8 @@ function set_attribute(
attr::MOI.AbstractConstraintAttribute,
value,
)
MOI.set(owner_model(cr), attr, cr, _string_if_abstract_string(value))
model = owner_model(cr)
MOI.set(model, attr, cr, _to_concrete_value_type_if_needed(attr, value))
return
end

Expand Down Expand Up @@ -1348,7 +1366,7 @@ function set_attribute(
attr::MOI.AbstractOptimizerAttribute,
value,
)
MOI.set(model, attr, _string_if_abstract_string(value))
MOI.set(model, attr, _to_concrete_value_type_if_needed(attr, value))
return
end

Expand Down
4 changes: 3 additions & 1 deletion test/test_model.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1332,7 +1332,9 @@ function test_set_abstract_string()
@test ret isa String && ret == "foo"
set_attribute(model, abstract_string, abstract_string)
ret = get_attribute(model, abstract_string)
@test ret isa String && ret == "foo"
# This `ret` is NOT a `::String` because we don't know the value type of the
# attribute.
@test ret isa typeof(abstract_string) && ret == "foo"
return
end

Expand Down

0 comments on commit a0d67eb

Please sign in to comment.