Overriding Worker.new results in a compilation warning
Closed this issue · 2 comments
arjan commented
- Oban Version: any
- PostgreSQL Version: n/a
- Elixir & Erlang/OTP Versions: OTP 26, Elixir 1.15, 1.16
Current Behavior
When creating a custom new/2 function on a worker, a compilation warning is raised. Easily reproducible with this script:
Mix.install([:oban])
defmodule MyWorker do
use Oban.Worker
@impl Oban.Worker
def new(args, opts \\ []) do
Map.put(args, "foo", 1)
super(args, opts)
end
@impl Oban.Worker
def perform(_), do: :ok
end
The warning that is thrown is this one:
warning: this clause for new/1 cannot match because a previous clause at line 4 always matches
│
7 │
│
│
└─ /home/arjan/.cache/mix/installs/elixir-1.16.1-erts-14.0.2/a89644f639a5a591d0aea0a967f470fd/deps/oban/lib/oban/worker.ex:7
I think it has to do with the defaults on the second argument.
The warning goes away if I add a @callback
declaration for the new/1 in Oban.Worker
, next to the existing callback:
@callback new(args :: Job.args()) :: Job.changeset()
But I am not sure that this is a proper solution.
sorentwo commented
Setting default opts
defines both a new/1
and new/2
, and it correctly warns that new/1
already exists.
You should only define new/2
:
@impl Oban.Worker
def new(args, opts) do
args
|> Map.put(:foo, 1)
|> super(opts)
end
That clause will still be used when you call MyWorker.new(args)
.
arjan commented
Ah yes, of course, thanks for the explanation!