fireproofsocks
Understanding Dialyzer error: The pattern can never match the type
I’m using Dialyzer to check over some code that uses the mongodb package. I’ve got a function that abstracts the Mongo.insert_one/4 function and adds in some extra logging for certain conditions:
def insert(collection, args) do
case Mongo.insert_one(mongo_pid(), collection, args) do
{:ok, %Mongo.InsertOneResult{acknowledged: true, inserted_id: id}} ->
{:ok, %{"_id" => id}}
# Check for 11000 error (usually this means a duplicate)
{:error, %Mongo.WriteError{write_errors: [%{"code" => 11_000} | _]} = err} -> {:error, "Duplicate entry"}
{:error, err} -> {:error, err}
end
end
When I run dialyzer, I get the following error:
The pattern can never match the type.
Pattern:
{:error, _err = %Mongo.WriteError{:write_errors => [%{<<99, 111, 100, 101>> => 11000} | _]}}
Type:
:ok
| {:error,
%Mongo.Error{
:__exception__ => _,
:code => number(),
:host => binary(),
:message => binary()
}}
| {:ok,
%Mongo.InsertOneResult{
:acknowledged => false,
:inserted_id => nil | %BSON.ObjectId{:value => <<_::96>>}
}}
After looking at the mongodb source code I can see that it appears to miss a viable return type:
@type result(t) :: :ok | {:ok, t} | {:error, Mongo.Error.t()}
should be (I think) something like:
@type result(t) :: :ok | {:ok, t} | {:error, Mongo.Error.t()} | {:error, Mongo.WriteError.t()}
My questions are:
- Am I correct in thinking that the dialyzer error in my app is resulting from what appears to be an incorrect type definition in a dependency (
mongodbin this case)? - Apart from filing an issue, what are the ways to configure dialyzer to ignore that particular error in that specific place?
- Why doesn’t Dialyzer catch this problem in
mongodb? I cloned the repo, I fired up Dialyzer, and it shows no errors. Is that expected? Common?
Most Liked
michallepicki
- It does seem like the function spec for
insert_onedoes not contain{:error, %Mongo.WriteError{}}in the return type. Whether theirresulttype should be updated or not, I’m not sure. Notice that I wrote{:error, %Mongo.WriteError{}}and not{:error, Mongo.WriteError.t()}- I checked and there is no@type tdefined in that module so it does not mean anything. This module got introduced in this commit and specs were not updated. - It depends on how you’re running dialyzer. If you run the standalone dialyzer executable, you can use the
@dialyzermodule attribute. If you usedialyxir, their README.md lists a few more ways to ignore warnings. To me, the ignore file with string matches seems to be the most commonly used way to do that. - Dialyzer accepts flags which configure what kinds of errors are shown. This kind of error would be shown if you used the
-Woverspecsoption. It is opt-in because it can introduce a lot of noise. Dialyzer has its limits and does not know how to deal with very complex types so it may sometimes “round up” some types to more general ones. I think Elixir’swithmay be sometimes confusing it as well? I’m not sure. Adding specs to private functions used may help it. But overall, Dialyzer doesn’t guarantee to find all problems in your code.
1
Popular in Questions
For example for a current url like
http://localhost:4000/cosmetic/products?_utf8=✓&query=perfume&page=2,
I would like to get:
...
New
Is there a way to get the call stack or stack trace at any point in the code? Not from exceptions, but an expression that returns how the...
New
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set?
Thanks.
New
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum.
...
New
Credo is smart enough to check for (something like) this:
assert length(the_list) == 0
with this response:
Checking if an enum is empt...
New
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
Hi everyone!
I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New
Other popular topics
After calling mix ecto.create I get this error:
17:00:32.162 [error] GenServer #PID<0.412.0> terminating
** (Postgrex.Error) FATAL...
New
Hi all,
I’ve just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage.
I’m trying to use Postgres...
New
lets say i have a sample like
a = 20; b = 10;
if (a > b) do
{:ok, "a"}
end
if (a < b) do
{:ok, b}
end
if (a == b) do
{:ok, "equa...
New
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors:
[WARN] - (starship::utils): Executing command ...
New
I have VueJS GUIs with the project generated using Webpack.
I have Elixir modules that will need to be used by the VueJS GUIs.
I forese...
New
Update:
How to use the Blogs & Podcasts section
You can post links to your blog posts or podcasts either in one of the Official Blog...
New
Seen any cool LiveView demos, sample apps or examples? Please post them here! :003:
New
I wrote this comment on r/haskell, and it’s not popular there. :wink: But I think I’m on to something…
Haskell reminds me of Java, and e...
New
Hi everyone!
I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance








