Skip to content

Broadcast lhs .~ f(args...; kwargs...) crashes with MethodError when mixing positional and keyword args #309

Description

@docxology

Summary

The broadcast form of the model DSL raises MethodError (no method matching tuple)
at model-construction time when the RHS supplies both positional and keyword arguments,
e.g. z .~ f(μ, σ = σ). Keyword-only and positional-only broadcast work; only the mixed
case is broken.

Root cause

src/model_macro.jl:617-628 combine_broadcast_args(args::Vector, kwargs::Vector) builds the
keyword NamedTuple from the entire broadcast-argument tuple args instead of just the
keyword values:

GraphPPL.MixedArguments((μ,), NamedTuple{(:σ,)}(args))   # args = (μ, σ) → MethodError

Repro

using GraphPPL, Distributions
import GraphPPL: @model, create_model

@model function bc_mixed(x, y, out)
    out ~ Normal(x, y)
end
@model function bc_main2()
    local μ, σ
    for i in 1:5
        μ[i] ~ Normal(0, 1); σ[i] ~ Gamma(1, 1)
    end
    z .~ bc_mixed(μ, σ = σ)     # positional μ + keyword σ → MethodError
    out ~ Normal(z[5], 1)
end
create_model(bc_main2())        # ERROR: MethodError no method matching tuple(...)

Suggested fix

Select only the keyword slice of the broadcast args for the NamedTuple in the else branch
of combine_broadcast_args (keep positional values in MixedArguments.args). Add a
regression test for the mixed broadcast case.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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