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

Improve error message when = is used instead of == #3892

Merged
merged 1 commit into from
Nov 26, 2024
Merged

Improve error message when = is used instead of == #3892

merged 1 commit into from
Nov 26, 2024

Conversation

odow
Copy link
Member

@odow odow commented Nov 26, 2024

Closes #3891

Fixes https://discourse.julialang.org/t/problem-setting-constraint-in-simple-lp-using-jump/123059

julia> function quantile_regression_linear()
           # retrieve views of the relevant data 
           y = rand(1000)
           x = rand(1000)
           N = length(y)
           # initialize the optimization model and solve it 
           model = Model()
           set_silent(model)
           @variable(model, u_plus[1:N]  0.0)
           @variable(model, u_minus[1:N]  0.0)
           @variable(model, θ[1:2])
           @constraint(model, [i in 1:N], y[i] - θ[1] - θ[2]*x[i] = u_plus[i] + u_minus[i])
       end
ERROR: LoadError: At REPL[11]:12: `@constraint(model, [i in 1:N], (y[i] - θ[1]) - θ[2] * x[i] = begin
        #= REPL[11]:12 =#
        u_plus[i] + u_minus[i]
    end)`: Invalid keyword argument detected. If you are trying to construct an equality constraint, use `==` instead of `=`.
Stacktrace:
 [1] error(::String, ::String)
   @ Base ./error.jl:44
 [2] (::JuMP.Containers.var"#error_fn#98"{String})(str::String)
   @ JuMP.Containers ~/.julia/dev/JuMP/src/Containers/macro.jl:331
 [3] parse_macro_arguments(error_fn::JuMP.Containers.var"#error_fn#98"{}, args::Tuple{…}; valid_kwargs::Nothing, num_positional_args::Nothing)
   @ JuMP.Containers ~/.julia/dev/JuMP/src/Containers/macro.jl:58
 [4] parse_macro_arguments(error_fn::Function, args::Tuple{Symbol, Expr, Expr})
   @ JuMP.Containers ~/.julia/dev/JuMP/src/Containers/macro.jl:41
 [5] var"@constraint"(__source__::LineNumberNode, __module__::Module, input_args::Vararg{Any})
   @ JuMP ~/.julia/dev/JuMP/src/macros/@constraint.jl:103
in expression starting at REPL[11]:12
Some type information was truncated. Use `show(err)` to see complete types.

Copy link

codecov bot commented Nov 26, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 99.58%. Comparing base (1230ea6) to head (a89a34a).
Report is 1 commits behind head on master.

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #3892   +/-   ##
=======================================
  Coverage   99.58%   99.58%           
=======================================
  Files          43       43           
  Lines        6041     6043    +2     
=======================================
+ Hits         6016     6018    +2     
  Misses         25       25           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@odow odow merged commit 488a0db into master Nov 26, 2024
11 checks passed
@odow odow deleted the od/kwarg branch November 26, 2024 02:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

Improve error message when confusing = with == in @constraint?
1 participant