Dialyzer Error when function returns Ecto.Query.t

Hi folks.

I am running dialyzer on my project and getting the following error:

employee.ex:77: Invalid type specification for function 'Elixir.Employee':employees_with_contacts_query/0. The success typing is () -> #{}

Here is a snippet of my code (what I believe to be the relevant parts only):

defmodule Employee do
  require Ecto.Query
  import Ecto.Query
  
  @doc ~S"""
  Query to select the employees with contact number.
  """
  @spec employees_with_contacts_query :: Ecto.Query.t
  def employees_with_contacts_query do
    from e in Employee,
      join: cb in ContactBook,
      where: cb.employee_id == e.id
  end
end

Does anyone see anything wrong here?

(I tried looking at the docs for Ecto.Query.from/2, to check if maybe it doesn’t actually return an Ecto.Query.t, and I noticed that none of the functions in Ecto.Query have a type annotation. Is this because they are not functions, but macros? What does that mean when I am trying to add type annotations to my own functions?)

1 Like

Ecto.Query.from is not a function, it is a macro. And you can’t properly @spec them.

Also the macro is to complex to dissect or expand it manually (at least it is for me). Perhaps someone more keen to macros or ecto is able to chime in here?

1 Like

Thanks @NobbZ. I replaced “Is this because they are functions?” in my original post with “Is this because they are not functions, but macros?”, which was my original intent.

At any rate, you answered my question - it’s because from is a macro, so I should not spec it.

Thank you!

1 Like