Skip to content

Commit

Permalink
fix: canonicalize all positional args in @named
Browse files Browse the repository at this point in the history
- So far, only the 1st positional-arg was canonicalized.
- Add relevant tests.
  • Loading branch information
ven-k committed Dec 7, 2023
1 parent 97b6524 commit 65440fe
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/systems/abstractsystem.jl
Original file line number Diff line number Diff line change
Expand Up @@ -964,7 +964,7 @@ function _named(name, call, runtime = false)
if length(call.args) >= 2 && call.args[2] isa Expr
# canonicalize to use `:parameters`
if call.args[2].head === :kw
call.args[2] = Expr(:parameters, Expr(:kw, call.args[2].args...))
call = Expr(call.head, call.args[1], Expr(:parameters, call.args[2:end]...))

Check warning on line 967 in src/systems/abstractsystem.jl

View check run for this annotation

Codecov / codecov/patch

src/systems/abstractsystem.jl#L967

Added line #L967 was not covered by tests
has_kw = true
elseif call.args[2].head === :parameters
has_kw = true
Expand Down
23 changes: 23 additions & 0 deletions test/direct.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ using ModelingToolkit, StaticArrays, LinearAlgebra, SparseArrays
using DiffEqBase
using Test

using ModelingToolkit: getdefault, getmetadata, SymScope

canonequal(a, b) = isequal(simplify(a), simplify(b))

# Calculus
Expand Down Expand Up @@ -271,3 +273,24 @@ end
@parameters x [misc = "wow"]
@test SymbolicUtils.getmetadata(Symbolics.unwrap(x), ModelingToolkit.VariableMisc,
nothing) == "wow"

# Scope of defaults in the systems generated by @named
@mtkmodel MoreThanOneArg begin
@variables begin
x(t)
y(t)
z(t)
end
end

@parameters begin
l
m
n
end

@named model = MoreThanOneArg(x = l, y = m, z = n)

@test getmetadata(getdefault(model.x), SymScope) == ParentScope(LocalScope())
@test getmetadata(getdefault(model.y), SymScope) == ParentScope(LocalScope())
@test getmetadata(getdefault(model.z), SymScope) == ParentScope(LocalScope())

0 comments on commit 65440fe

Please sign in to comment.