Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

@contract between @doc and def inhibits ExDoc, ExUnit.DocTest #80

Open
garthk opened this issue May 20, 2020 · 3 comments
Open

@contract between @doc and def inhibits ExDoc, ExUnit.DocTest #80

garthk opened this issue May 20, 2020 · 3 comments

Comments

@garthk
Copy link
Contributor

garthk commented May 20, 2020

a_contract_between_doc_and_def/0 below won't get tested or documented, without any warning to that effect:

defmodule NormContractVsDoctest do
  @moduledoc """
  Demonstrates interaction between `@contract` and `@doc`
  """

  use Norm

  @doc """
  Return `:a`.

      iex> a_bare()
      :a
  """
  def a_bare, do: :a

  @doc """
  Return `:a`.

      iex> a_contract_between_doc_and_def()
      :a
  """
  @contract a_contract_between_doc_and_def() :: :a
  def a_contract_between_doc_and_def, do: :a

  @contract a_contract_before_doc() :: :a
  @doc """
  Return `:a`.

      iex> a_contract_before_doc()
      :a
  """
  def a_contract_before_doc, do: :a
end

Strikes me the suppression might not be fixable without help from both, but you might well be in a position to see the @doc above @contract and give warning. Failing that, let me know and I'll raise a PR to document the problem so people don't have to find it in the issues.

@keathley
Copy link
Member

Hmm...I've been wondering if we should look at using the @on_definition hook for contracts. I think that would solve both of these problems. We'd be able to check to see if we have a contract attribute (or attributes) set, and we'd still be able to include all of the relevant docs and whatnot. Its probably more work to do it that way but it might result in less surprises for the user. @wojtekmach would probably no more than me since he's spent more time with that code.

@wojtekmach
Copy link
Contributor

wojtekmach commented May 24, 2020 via email

garthk added a commit to garthk/norm that referenced this issue May 24, 2020
@Qqwy
Copy link
Contributor

Qqwy commented Jun 21, 2020

There are two problems with using on_definition:

  1. on_definition is called once per function clause rather than per function as a whole.
  2. The result of the callback that on_definition calls is not used in any way. As such it cannot be used to e.g. insert code into a function (clause), at least not directly.

An alternative might be to override def/ defp directly instead, which has the disadvantage of making it (near?-)impossible to use Norm in combination with another library that overrides these in the same module.
Another alternative is overriding defmodule, which has the unfortunate (backwards-incompatible) consequence that Norm needs to be referenced outside of the modules in which it will be used. The advantage of this alternative is however that all complexity related to overriding @ and depending on defoverridable can be removed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants