From 0ffdf0298a576e7a97d737de71ffe72e2b8c50ba Mon Sep 17 00:00:00 2001 From: odow Date: Thu, 2 Nov 2023 11:58:48 +1300 Subject: [PATCH 1/3] Fix variable_ref_type for unsupported types and GenericNonlinearExpr --- src/nlp_expr.jl | 2 +- src/variables.jl | 8 ++++++++ test/test_nlp_expr.jl | 7 +++++++ test/test_variable.jl | 14 ++++++++++++++ 4 files changed, 30 insertions(+), 1 deletion(-) diff --git a/src/nlp_expr.jl b/src/nlp_expr.jl index e77e9cd09da..ac4fc79aea8 100644 --- a/src/nlp_expr.jl +++ b/src/nlp_expr.jl @@ -133,7 +133,7 @@ Alias for `GenericNonlinearExpr{VariableRef}`, the specific """ const NonlinearExpr = GenericNonlinearExpr{VariableRef} -variable_ref_type(::GenericNonlinearExpr{V}) where {V} = V +variable_ref_type(::Type{GenericNonlinearExpr{V}}) where {V} = V const _PREFIX_OPERATORS = (:+, :-, :*, :/, :^, :||, :&&, :>, :<, :(<=), :(>=), :(==)) diff --git a/src/variables.jl b/src/variables.jl index efdea16ddee..e815f24b6c2 100644 --- a/src/variables.jl +++ b/src/variables.jl @@ -224,6 +224,14 @@ variable type associated with the model or expression type `F`. """ variable_ref_type(::F) where {F} = variable_ref_type(F) +function variable_ref_type(::Type{F}) where {F} + return error( + "Unable to compute the `variable_ref_type` of the type `$F`. If you " * + "are developing a JuMP extension, defined a new method for " * + "`JuMP.variable_ref_type(::Type{$F})`", + ) +end + variable_ref_type(::Type{V}) where {V<:AbstractVariableRef} = V value_type(::Type{<:AbstractVariableRef}) = Float64 diff --git a/test/test_nlp_expr.jl b/test/test_nlp_expr.jl index e07dc1ed036..def51c63ff1 100644 --- a/test/test_nlp_expr.jl +++ b/test/test_nlp_expr.jl @@ -971,4 +971,11 @@ function test_operator_max() return end +function test_variable_ref_type() + for V in (GenericVariableRef{Int}, VariableRef) + @test variable_ref_type(GenericNonlinearExpr{V}) == V + end + return +end + end # module diff --git a/test/test_variable.jl b/test/test_variable.jl index 1029c10a20f..5d1763582e0 100644 --- a/test/test_variable.jl +++ b/test/test_variable.jl @@ -1555,4 +1555,18 @@ function test_parameter_arrays() return end +function test_variable_ref_type_unsupported() + for F in (Vector{VariableRef}, Vector{Int}) + @test_throws( + ErrorException( + "Unable to compute the `variable_ref_type` of the type $F`. If you " * + "are developing a JuMP extension, defined a new method for " * + "`JuMP.variable_ref_type(::Type{$F}`", + ), + variable_ref_type(F), + ) + end + return +end + end # module TestVariable From f36a48ae9d0b74af7ef9bbd661fe33ad147dd226 Mon Sep 17 00:00:00 2001 From: Oscar Dowson Date: Thu, 2 Nov 2023 13:27:04 +1300 Subject: [PATCH 2/3] Update test/test_variable.jl --- test/test_variable.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/test_variable.jl b/test/test_variable.jl index 5d1763582e0..2a439c7ed7a 100644 --- a/test/test_variable.jl +++ b/test/test_variable.jl @@ -1559,7 +1559,7 @@ function test_variable_ref_type_unsupported() for F in (Vector{VariableRef}, Vector{Int}) @test_throws( ErrorException( - "Unable to compute the `variable_ref_type` of the type $F`. If you " * + "Unable to compute the `variable_ref_type` of the type `$F`. If you " * "are developing a JuMP extension, defined a new method for " * "`JuMP.variable_ref_type(::Type{$F}`", ), From ae29edd28e65283e44feb0021bc8623ef32e39de Mon Sep 17 00:00:00 2001 From: Oscar Dowson Date: Thu, 2 Nov 2023 15:13:27 +1300 Subject: [PATCH 3/3] Apply suggestions from code review --- src/variables.jl | 2 +- test/test_variable.jl | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/variables.jl b/src/variables.jl index e815f24b6c2..df70cf9e9a3 100644 --- a/src/variables.jl +++ b/src/variables.jl @@ -227,7 +227,7 @@ variable_ref_type(::F) where {F} = variable_ref_type(F) function variable_ref_type(::Type{F}) where {F} return error( "Unable to compute the `variable_ref_type` of the type `$F`. If you " * - "are developing a JuMP extension, defined a new method for " * + "are developing a JuMP extension, define a new method for " * "`JuMP.variable_ref_type(::Type{$F})`", ) end diff --git a/test/test_variable.jl b/test/test_variable.jl index 2a439c7ed7a..4e4a2221f91 100644 --- a/test/test_variable.jl +++ b/test/test_variable.jl @@ -1560,8 +1560,8 @@ function test_variable_ref_type_unsupported() @test_throws( ErrorException( "Unable to compute the `variable_ref_type` of the type `$F`. If you " * - "are developing a JuMP extension, defined a new method for " * - "`JuMP.variable_ref_type(::Type{$F}`", + "are developing a JuMP extension, define a new method for " * + "`JuMP.variable_ref_type(::Type{$F})`", ), variable_ref_type(F), )