Skip to content

Commit 28a0a07

Browse files
Dmitri BagaevDmitri Bagaev
authored andcommitted
fix(): better error message for datavar creation
1 parent caeec7a commit 28a0a07

File tree

6 files changed

+26
-12
lines changed

6 files changed

+26
-12
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "GraphPPL"
22
uuid = "b3f8163a-e979-4e85-b43e-1f63d8c8b42c"
33
authors = ["Dmitry Bagaev <[email protected]>"]
4-
version = "2.0.0"
4+
version = "2.0.1"
55

66
[deps]
77
MacroTools = "1914dd2f-81c6-5fcd-8719-6d5c9610ff09"

docs/src/transformation-steps.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ y = datavar(args...) where { options... }
3737
is translated to
3838

3939
```
40-
y = datavar(var"#model", options, :y, ensure_type(args[1]), args[2:end]...)
40+
ensure_type(args[1]) || error(...)
41+
y = datavar(var"#model", options, :y, args[1], args[2:end]...)
4142
```
4243

4344
where `var"#model"` references to an hidden model variable, `ensure_type` function ensures that the first argument is a valid type object, rest of the arguments are left untouched.

src/backends/reactivemp.jl

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,9 @@ function write_randomvar_expression(::ReactiveMPBackend, model, varexp, options,
8787
end
8888

8989
function write_datavar_expression(::ReactiveMPBackend, model, varexpr, options, type, arguments)
90-
return :($varexpr = ReactiveMP.datavar($model, $options, $(GraphPPL.fquote(varexpr)), ReactiveMP.PointMass{ GraphPPL.ensure_type($(type)) }, $(arguments...)))
90+
errstr = "The expression `$varexpr = datavar($(type))` is incorrect. datavar(::Type, [ dims... ]) requires `Type` as a first argument, but `$(type)` is not a `Type`."
91+
checktype = :(GraphPPL.ensure_type($(type)) || error($errstr))
92+
return :($checktype; $varexpr = ReactiveMP.datavar($model, $options, $(GraphPPL.fquote(varexpr)), ReactiveMP.PointMass{ $type }, $(arguments...)))
9193
end
9294

9395
function write_constvar_expression(::ReactiveMPBackend, model, varexpr, arguments)
@@ -326,7 +328,7 @@ function write_datavar_options(::ReactiveMPBackend, variable, type, options)
326328
end
327329
end
328330

329-
return :(ReactiveMP.DataVariableCreationOptions(ReactiveMP.PointMass{ GraphPPL.ensure_type($type) }, $subject_option, $allow_missing_option))
331+
return :(ReactiveMP.DataVariableCreationOptions(ReactiveMP.PointMass{ $type }, $subject_option, $allow_missing_option))
330332
end
331333

332334
function write_default_model_constraints(::ReactiveMPBackend)

src/model.jl

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,6 @@ fquote(expr::Symbol) = Expr(:quote, expr)
1515
fquote(expr::Int) = expr
1616
fquote(expr::Expr) = expr
1717

18-
"""
19-
ensure_type
20-
"""
21-
ensure_type(x::Type) = x
22-
ensure_type(x) = error("Valid type object was expected but '$x' has been found")
23-
2418
is_kwargs_expression(x) = false
2519
is_kwargs_expression(x::Expr) = x.head === :parameters
2620

@@ -293,7 +287,7 @@ function generate_model_expression(backend, model_options, model_specification)
293287
# Step 2.1 Convert datavar calls
294288
if @capture(expression, varexpr_ = datavar(arguments__; options__))
295289
@assert varexpr varids "Invalid model specification: '$varexpr' id is duplicated"
296-
@assert length(arguments) >= 1 "Invalid datavar() creation. datavar(::Type{T}, [ dims... ]) requires type specification as a first argument, but the expression `$(expression)` has no type argument."
290+
@assert length(arguments) >= 1 "The expression `$expression` is incorrect. datavar(::Type, [ dims... ]) requires `Type` as a first argument."
297291

298292
push!(varids, varexpr)
299293

src/utils.jl

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,12 @@ Shorthand for `ishead(expr, :ref)`.
3939
4040
See also: [`ishead`](@ref)
4141
"""
42-
isref(expr) = ishead(expr, :ref)
42+
isref(expr) = ishead(expr, :ref)
43+
44+
"""
45+
ensure_type(x)
46+
47+
Checks if `x` is of type `Type`
48+
"""
49+
ensure_type(x::Type) = true
50+
ensure_type(x) = false

test/utils.jl

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,4 +54,13 @@ end
5454

5555
end
5656

57+
@testset "ensure_type tests" begin
58+
import GraphPPL: ensure_type
59+
60+
@test ensure_type(Int) === true
61+
@test ensure_type(1) === false
62+
@test ensure_type(Float64) === true
63+
@test ensure_type(1.0) === false
64+
end
65+
5766
end

0 commit comments

Comments
 (0)