Fixing Dialyzer and ecto changeset uses

I have an umbrella app. I see the value of Dialyzer and I’m trying to get started with it. I’ve gotten pretty far but I have an issue around Ecto I cannot solve.

This is for a small App in the umbrella that handles Authentication. I can trim it all down the simplest example.

Using Elixir 1.4.2 and Dialyxir 0.4.0.

Code in Question

defmodule Auth.Account do
  use Ecto.Schema
  import Ecto.Changeset

  schema "auth_accounts" do
    field :email, :string
    field :password_hash, :string
    field :password, :string, virtual: true

    timestamps()
  end

  def build(params \\ :invalid) do
    changeset(%__MODULE__{}, params)
  end

  def changeset(account, params \\ %{}) do
    account
    |> cast(params, ~w(email password))
  end
end

Relevant Error Output

lib/auth/account.ex:13: Function build/0 has no local return
lib/auth/account.ex:13: Function build/1 has no local return
lib/auth/account.ex:14: The call 'Elixir.Auth.Account':changeset(#{'__meta__':=#{'__struct__':='Elixir.Ecto.Schema.Metadata', 'context':='nil', 'source':={'nil',<<_:104>>}, 'state':='built'}, '__struct__':='Elixir.Auth.Account', 'email':='nil', 'id':='nil', 'inserted_at':='nil', 'password':='nil', 'password_hash':='nil', 'updated_at':='nil'},params@1::any()) 
  will never return since it differs in the 1st argument from the success typing arguments: ({map(),map()} | #{'__struct__':=atom(), 'action'=>'delete' | 'insert' | 'nil' | 'replace' | 'update', 'changes'=>#{atom()=>_}, 'constraints'=>[#{'constraint':=binary(), 'field':=atom(), 'match':='exact' | 'suffix', 'message':={_,_}, 'type':='unique'}], 'data'=>'nil' | #{'__struct__':=atom()}, 'empty_values'=>_, 'errors'=>[{atom(),{_,_}}], 'filters'=>#{atom()=>_}, 'params'=>'nil' | #{binary()=>_}, 'prepare'=>[fun((map()) -> map())], 'repo'=>atom(), 'required'=>[atom()], 'types'=>'nil' | #{atom()=>atom() | {'array',_} | {'embed',map()} | {'in',_} | {'map',_}}, 'valid?'=>boolean(), 'validations'=>[{atom(),_}]},'invalid' | #{'__struct__'=>none(), atom() | binary()=>_})

After lots of playing, it appears the problem is around the build function’s use of %_MODULE_{}. See Stack Overflow Topic. However, I just can’t figure out a valid alternative.

Thoughts? Ideas? Help?

1 Like

I asked this question over on StackOverflow and the resolution to my problem was to upgrade to a new ecto version.

1 Like