Skip to content

Very unintuitive error message when the syntax is invalid #260

@ForceBru

Description

@ForceBru

Code:

begin
	import Pkg
	Pkg.activate(temp=true)
	Pkg.add(name="Turing", version="0.16.0", io=devnull)
	Pkg.add(name="Distributions", io=devnull)
	Pkg.status()
end


using Turing, Distributions

@model function(data::AbstractVector)	
	p ~ Dirichlet(2, 1.0)
	μ1 ~ Normal(-4e-4, 2.1e-4)
	μ2 ~ Normal( 4e-4, 2.1e-4)
	
	Σ ~ filldist(Uniform(0, 2.1e-4), 2)
	
	data ~ filldist(
		UnivariateGMM([μ1, μ2], Σ, p),
		length(data)
	)
end

Error:

~/test $ julia turing_bug2.jl
  Activating new environment at `/var/folders/ys/3h0gnqns4b98zb66_vl_m35m0000gn/T/jl_nVue8d/Project.toml`
      Status `/private/var/folders/ys/3h0gnqns4b98zb66_vl_m35m0000gn/T/jl_nVue8d/Project.toml`
  [31c24e10] Distributions v0.25.2
  [fce5fe82] Turing v0.16.0
ERROR: LoadError: LoadError: KeyError: key :name not found
Stacktrace:
 [1] getindex(h::Dict{Symbol, Any}, key::Symbol)
   @ Base ./dict.jl:482
 [2] build_output(modelinfo::Dict{Symbol, Any}, linenumbernode::LineNumberNode)
   @ DynamicPPL ~/.julia/packages/DynamicPPL/emAbx/src/compiler.jl:385
 [3] model(mod::Module, linenumbernode::LineNumberNode, expr::Expr, warn::Bool)
   @ DynamicPPL ~/.julia/packages/DynamicPPL/emAbx/src/compiler.jl:91
 [4] var"@model"(__source__::LineNumberNode, __module__::Module, expr::Any, warn::Any) (repeats 2 times)
   @ DynamicPPL ~/.julia/packages/DynamicPPL/emAbx/src/compiler.jl:82
in expression starting at /Users/forcebru/test/turing_bug2.jl:12
in expression starting at /Users/forcebru/test/turing_bug2.jl:12
~/test [1] $

I literally spent 15 minutes trying to understand what was wrong. Turns out, I was using too much R lately, and @model function(data::AbstractVector) should've been @model function model_name(data::AbstractVector).

However:

  1. The error message KeyError: key :name not found looks like an internal error that doesn't tell me what the problem is.
  2. function(arg) arg + 1 end is actually valid Julia syntax, so one can write my_func = function(arg) arg + 1 end, much like in R. I guess Turing's models aren't really functions (even though they look exactly like functions), so it's probably fine that some syntax isn't supported, but the error message could use some improvement, IMO

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions