Skip to content

Commit

Permalink
Fix dialyzer warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
kipcole9 committed Mar 20, 2019
1 parent 0fd9b8e commit 32352b6
Show file tree
Hide file tree
Showing 9 changed files with 62 additions and 38 deletions.
Empty file added .dialyzer_ignore_warnings
Empty file.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
# Changelog for Cldr_Units v2.3.2

This is the changelog for Cldr_units v2.3.2 released on March 20th, 2019. For older changelogs please consult the release tag on [GitHub](https://github.com/kipcole9/cldr_units/tags)

### Bug Fixes

* Fix dialyzer warnings

# Changelog for Cldr_Units v2.3.1

This is the changelog for Cldr_units v2.3.1 released on March 15th, 2019. For older changelogs please consult the release tag on [GitHub](https://github.com/kipcole9/cldr_units/tags)
Expand Down
4 changes: 3 additions & 1 deletion lib/cldr/backend.ex
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ defmodule Cldr.Unit.Backend do

@styles [:long, :short, :narrow]

alias Cldr.Math

defdelegate new(unit, value), to: Cldr.Unit
defdelegate new!(unit, value), to: Cldr.Unit
defdelegate compatible?(unit_1, unit_2), to: Cldr.Unit
Expand Down Expand Up @@ -183,7 +185,7 @@ defmodule Cldr.Unit.Backend do
"1 gelling"
"""
@spec to_string!(Math.decimal_or_number(), Keyword.t()) :: String.t() | no_return()
@spec to_string!(Cldr.Math.number_or_decimal(), Keyword.t()) :: String.t() | no_return()

def to_string!(number, options \\ []) do
Cldr.Unit.to_string!(number, unquote(backend), options)
Expand Down
2 changes: 1 addition & 1 deletion lib/cldr/conversion.ex
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ defmodule Cldr.Unit.Conversion do
** (Cldr.Unit.IncompatibleUnitsError) Operations can only be performed between units of the same type. Received :mile and :gallon
"""
@spec convert(Unit.t(), Unit.unit()) :: Unit.t() | {:error, {module(), String.t()}}
@spec convert!(Unit.t(), Unit.unit()) :: Unit.t() | no_return()

def convert!(%Unit{} = unit, to_unit) do
case convert(unit, to_unit) do
Expand Down
21 changes: 9 additions & 12 deletions lib/cldr/math.ex
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,7 @@ defmodule Cldr.Unit.Math do
"Operations can only be performed between units of the same type. Received #Unit<:foot, 1> and #Unit<:gallon, 1>"}}
"""
@spec add(unit_1 :: Unit.t(), unit_2 :: Unit.t()) ::
Unit.t() | {:error, {Unit.IncompatibleUnitError, String.t()}}
@spec add(Unit.t(), Unit.t()) :: Unit.t() | {:error, {module(), String.t()}}

def add(%Unit{unit: unit, value: value_1}, %Unit{unit: unit, value: value_2})
when is_number(value_1) and is_number(value_2) do
Expand Down Expand Up @@ -88,7 +87,7 @@ defmodule Cldr.Unit.Math do
* Raises an exception
"""
@spec add!(unit_1 :: Unit.t(), unit_2 :: Unit.t()) :: Unit.t() | no_return()
@spec add!(Unit.t(), Unit.t()) :: Unit.t() | no_return()

def add!(unit_1, unit_2) do
case add(unit_1, unit_2) do
Expand Down Expand Up @@ -125,8 +124,7 @@ defmodule Cldr.Unit.Math do
#Unit<:pint, 4>
"""
@spec sub(unit_1 :: Unit.t(), unit_2 :: Unit.t()) ::
Unit.t() | {:error, {Unit.IncompatibleUnitError, String.t()}}
@spec sub(Unit.t(), Unit.t()) :: Unit.t() | {:error, {module(), String.t()}}

def sub(%Unit{unit: unit, value: value_1}, %Unit{unit: unit, value: value_2})
when is_number(value_1) and is_number(value_2) do
Expand Down Expand Up @@ -176,7 +174,7 @@ defmodule Cldr.Unit.Math do
* Raises an exception
"""
@spec sub!(unit_1 :: Unit.t(), unit_2 :: Unit.t()) :: Unit.t() | no_return()
@spec sub!(Unit.t(), Unit.t()) :: Unit.t() | no_return()

def sub!(unit_1, unit_2) do
case sub(unit_1, unit_2) do
Expand Down Expand Up @@ -213,8 +211,8 @@ defmodule Cldr.Unit.Math do
#Unit<:pint, 5>
"""
@spec mult(unit_1 :: Unit.t(), unit_2 :: Unit.t()) ::
Unit.t() | {:error, {Unit.IncompatibleUnitError, String.t()}}
@spec mult(Unit.t(), Unit.t()) :: Unit.t() | {:error, {module(), String.t()}}

def mult(%Unit{unit: unit, value: value_1}, %Unit{unit: unit, value: value_2})
when is_number(value_1) and is_number(value_2) do
Unit.new!(unit, value_1 * value_2)
Expand Down Expand Up @@ -263,7 +261,7 @@ defmodule Cldr.Unit.Math do
* Raises an exception
"""
@spec mult!(unit_1 :: Unit.t(), unit_2 :: Unit.t()) :: Unit.t() | no_return()
@spec mult!(Unit.t(), Unit.t()) :: Unit.t() | no_return()

def mult!(unit_1, unit_2) do
case mult(unit_1, unit_2) do
Expand Down Expand Up @@ -300,8 +298,7 @@ defmodule Cldr.Unit.Math do
#Unit<:pint, 5.0>
"""
@spec div(unit_1 :: Unit.t(), unit_2 :: Unit.t()) ::
Unit.t() | {:error, {Unit.IncompatibleUnitError, String.t()}}
@spec div(Unit.t(), Unit.t()) :: Unit.t() | {:error, {module(), String.t()}}

def div(%Unit{unit: unit, value: value_1}, %Unit{unit: unit, value: value_2})
when is_number(value_1) and is_number(value_2) do
Expand Down Expand Up @@ -351,7 +348,7 @@ defmodule Cldr.Unit.Math do
* Raises an exception
"""
@spec div!(unit_1 :: Unit.t(), unit_2 :: Unit.t()) :: Unit.t() | no_return()
@spec div!(Unit.t(), Unit.t()) :: Unit.t() | no_return()

def div!(unit_1, unit_2) do
case div(unit_1, unit_2) do
Expand Down
38 changes: 21 additions & 17 deletions lib/cldr/unit.ex
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,8 @@ defmodule Cldr.Unit do

@type unit :: atom()
@type style :: atom()
@type t(unit, value_type) :: %Unit{unit: unit, value: value_type}
@type t(unit) :: t(unit, integer | float | Decimal.t)
@type t :: t(atom())
@type value :: Cldr.Math.number_or_decimal()
@type t :: %Unit{unit: unit, value: value}

@default_style :long
@styles [:long, :short, :narrow]
Expand Down Expand Up @@ -83,6 +82,8 @@ defmodule Cldr.Unit do
"The unit :gadzoots is not known."}}
"""
@spec new(unit() | value(), value() | unit()) :: t() | {:error, {module(), String.t()}}

def new(value, unit) when is_number(value) do
with {:ok, unit} <- validate_unit(unit) do
%Unit{unit: unit, value: value}
Expand Down Expand Up @@ -128,6 +129,8 @@ defmodule Cldr.Unit do
(ex_cldr_units) lib/cldr/unit.ex:57: Cldr.Unit.new!/2
"""
@spec new!(unit() | value(), value() | unit()) :: t() | no_return()

def new!(unit, value) do
case new(unit, value) do
{:error, {exception, message}} -> raise exception, message
Expand Down Expand Up @@ -160,6 +163,7 @@ defmodule Cldr.Unit do
false
"""
@spec compatible?(unit(), unit()) :: boolean
def compatible?(unit_1, unit_2) do
with {:ok, unit_1} <- validate_unit(unit_1),
{:ok, unit_2} <- validate_unit(unit_2) do
Expand Down Expand Up @@ -242,7 +246,7 @@ defmodule Cldr.Unit do
{:error, {Cldr.UnknownUnitError, "The unit :blabber is not known."}}
"""
@spec to_string(list_or_number :: Cldr.Math.number_or_decimal() | t() | [t(), ...], backend :: Cldr.backend(), options :: Keyword.t()) ::
@spec to_string(list_or_number :: value | t() | list(t()), backend :: Cldr.backend(), options :: Keyword.t()) ::
{:ok, String.t()} | {:error, {atom, binary}}

def to_string(list_or_number, backend, options \\ [])
Expand Down Expand Up @@ -320,7 +324,7 @@ defmodule Cldr.Unit do
"1 gelling"
"""
@spec to_string!(Math.decimal_or_number(), Cldr.backend(), Keyword.t()) :: String.t() | no_return()
@spec to_string!(value(), Cldr.backend(), Keyword.t()) :: String.t() | no_return()
def to_string!(number, backend, options \\ []) do
case to_string(number, backend, options) do
{:ok, string} -> string
Expand All @@ -329,7 +333,7 @@ defmodule Cldr.Unit do
end

@spec to_string(
Math.decimal_or_number(),
Cldr.Math.number_or_decimal(),
unit,
Locale.locale_name() | LanguageTag.t(),
style,
Expand All @@ -343,7 +347,7 @@ defmodule Cldr.Unit do
cardinal_module = Module.concat(backend, Number.Cardinal)
pattern = cardinal_module.pluralize(number, locale, patterns)

number_string
[number_string]
|> Substitution.substitute(pattern)
|> :erlang.iolist_to_binary()
end
Expand All @@ -367,7 +371,7 @@ defmodule Cldr.Unit do
23
"""
@spec value(unit :: Unit.t()) :: Cldr.Math.number_or_decimal()
@spec value(unit :: t()) :: value()
def value(%Unit{value: value}) do
value
end
Expand Down Expand Up @@ -521,7 +525,7 @@ defmodule Cldr.Unit do
...
"""
@spec unit_tree :: [atom, ...]
@spec unit_tree :: map()
def unit_tree do
@unit_tree
end
Expand Down Expand Up @@ -739,7 +743,7 @@ defmodule Cldr.Unit do
"""
@default_options [jaro: false, distance: @default_distance]
@spec compatible_units(unit, Keyword.t() | Map.t()) :: [unit, ...] | [{float, unit}, ...] | []
@spec compatible_units(unit(), Keyword.t() | map()) :: [unit(), ...] | [{float, unit}, ...] | []

def compatible_units(unit, options \\ @default_options)

Expand All @@ -756,16 +760,15 @@ defmodule Cldr.Unit do
end

def compatible_units(unit, %{jaro: true, distance: distance}) when is_number(distance) do
unit = Kernel.to_string(unit)

case jaro_match(unit, distance) do
jaro_list when is_list(jaro_list) -> compatible_list(jaro_list, unit)
other -> other
end
unit
|> Kernel.to_string
|> jaro_match(distance)
|> compatible_list(unit)
end

@string_units Enum.map(@units, &Atom.to_string/1)
@spec match_list(unit, number) :: [{float, unit}, ...] | []
@spec match_list(String.t(), float) :: list({float, unit()}) | []

defp match_list(unit, distance) when is_binary(unit) and is_number(distance) do
@string_units
|> Enum.map(fn u -> {String.jaro_distance(unit, u), String.to_existing_atom(u)} end)
Expand All @@ -774,6 +777,7 @@ defmodule Cldr.Unit do
end

@spec compatible_list([{float, unit}, ...], unit) :: [{float, unit}, ...] | []

defp compatible_list(jaro_list, unit) when is_list(jaro_list) do
with {:ok, unit} <- validate_unit(unit) do
Enum.filter(jaro_list, fn
Expand Down
9 changes: 7 additions & 2 deletions mix.exs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
defmodule CldrUnits.Mixfile do
use Mix.Project

@version "2.3.1"
@version "2.3.2"

def project do
[
Expand All @@ -15,7 +15,11 @@ defmodule CldrUnits.Mixfile do
docs: docs(),
start_permanent: Mix.env() == :prod,
deps: deps(),
elixirc_paths: elixirc_paths(Mix.env())
elixirc_paths: elixirc_paths(Mix.env()),
dialyzer: [
ignore_warnings: ".dialyzer_ignore_warnings",
plt_add_apps: ~w(inets jason mix)a
],
]
end

Expand All @@ -36,6 +40,7 @@ defmodule CldrUnits.Mixfile do
[
{:ex_cldr, "~> 2.4"},
{:ex_cldr_numbers, "~> 2.1"},
{:dialyxir, "~> 1.0.0-rc.4", only: [:dev], runtime: false},
{:ex_cldr_lists, "~> 2.0"},
{:ex_doc, "~> 0.18", only: [:dev, :release]},
{:jason, "~> 1.0", optional: true}
Expand Down
10 changes: 6 additions & 4 deletions mix.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@
"abnf2": {:hex, :abnf2, "0.1.4", "9a424cfe94556c0e3e58b02326c39383b170dde4777913fcbdbdfcd2f1b0a97f", [:mix], [], "hexpm"},
"cldr_utils": {:hex, :cldr_utils, "2.1.0", "15d82ef0e3931e5f54bdabcbe76bbac0114d59074f18ffbfa8d55df789940835", [:mix], [{:decimal, "~> 1.5", [hex: :decimal, repo: "hexpm", optional: false]}], "hexpm"},
"decimal": {:hex, :decimal, "1.7.0", "30d6b52c88541f9a66637359ddf85016df9eb266170d53105f02e4a67e00c5aa", [:mix], [], "hexpm"},
"dialyxir": {:hex, :dialyxir, "1.0.0-rc.4", "71b42f5ee1b7628f3e3a6565f4617dfb02d127a0499ab3e72750455e986df001", [:mix], [{:erlex, "~> 0.1", [hex: :erlex, repo: "hexpm", optional: false]}], "hexpm"},
"earmark": {:hex, :earmark, "1.3.1", "73812f447f7a42358d3ba79283cfa3075a7580a3a2ed457616d6517ac3738cb9", [:mix], [], "hexpm"},
"erlex": {:hex, :erlex, "0.2.1", "cee02918660807cbba9a7229cae9b42d1c6143b768c781fa6cee1eaf03ad860b", [:mix], [], "hexpm"},
"ex_abnf": {:hex, :ex_abnf, "0.3.0", "07847d0d6cb75b779948520d02cba80eb855e18acb1aa8d43b1e370ef31afbcb", [:mix], []},
"ex_cldr": {:hex, :ex_cldr, "2.4.2", "833e70712fc6209a095151a92ff4262a751d3f9ef3471d7a7f24fa9afdebcccc", [:mix], [{:cldr_utils, "~> 2.1", [hex: :cldr_utils, repo: "hexpm", optional: false]}, {:decimal, "~> 1.5", [hex: :decimal, repo: "hexpm", optional: false]}, {:gettext, "~> 0.13", [hex: :gettext, repo: "hexpm", optional: true]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}, {:nimble_parsec, "~> 0.5", [hex: :nimble_parsec, repo: "hexpm", optional: false]}, {:plug, "~> 1.4", [hex: :plug, repo: "hexpm", optional: true]}], "hexpm"},
"ex_cldr_currencies": {:hex, :ex_cldr_currencies, "2.2.4", "30243b2248920b1c640feb786ecbc804252d27a848f8845d97f96de16b08e4e3", [:mix], [{:ex_cldr, "~> 2.4", [hex: :ex_cldr, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}], "hexpm"},
"ex_cldr_lists": {:hex, :ex_cldr_lists, "2.0.1", "f67965f3ffb8f25e04d3f891dd48aea84505db3c82c83f75c4be04f275475e48", [:mix], [{:ex_cldr, "~> 2.4", [hex: :ex_cldr, repo: "hexpm", optional: false]}, {:ex_cldr_numbers, "~> 2.0", [hex: :ex_cldr_numbers, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}], "hexpm"},
"ex_cldr_numbers": {:hex, :ex_cldr_numbers, "2.4.2", "a3a720515b59799792cd5e73201306253e0bef86125df9a7f30436a0fc66f819", [:mix], [{:decimal, "~> 1.5", [hex: :decimal, repo: "hexpm", optional: false]}, {:ex_cldr, "~> 2.4", [hex: :ex_cldr, repo: "hexpm", optional: false]}, {:ex_cldr_currencies, "~> 2.2", [hex: :ex_cldr_currencies, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}], "hexpm"},
"ex_cldr": {:hex, :ex_cldr, "2.4.3", "e9007dff20c4af8d2b8959ff9fcac06fe36c925a130003190ca54e9d999ed30e", [:mix], [{:cldr_utils, "~> 2.1", [hex: :cldr_utils, repo: "hexpm", optional: false]}, {:decimal, "~> 1.5", [hex: :decimal, repo: "hexpm", optional: false]}, {:gettext, "~> 0.13", [hex: :gettext, repo: "hexpm", optional: true]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}, {:nimble_parsec, "~> 0.5", [hex: :nimble_parsec, repo: "hexpm", optional: false]}, {:plug, "~> 1.4", [hex: :plug, repo: "hexpm", optional: true]}], "hexpm"},
"ex_cldr_currencies": {:hex, :ex_cldr_currencies, "2.2.5", "2d0dcc30be45ec4468f01362b724bc9a459679ed2050817121e736710394f875", [:mix], [{:ex_cldr, "~> 2.4", [hex: :ex_cldr, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}], "hexpm"},
"ex_cldr_lists": {:hex, :ex_cldr_lists, "2.0.2", "c71f490633b96713dec97ff4b1b584deddac0ce6fa28d56e20af0cc06f0c131b", [:mix], [{:ex_cldr, "~> 2.4", [hex: :ex_cldr, repo: "hexpm", optional: false]}, {:ex_cldr_numbers, "~> 2.0", [hex: :ex_cldr_numbers, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}], "hexpm"},
"ex_cldr_numbers": {:hex, :ex_cldr_numbers, "2.4.3", "877dcd7fd21c4d63997cdb6a3618448cc80bf60dfe6b86eef9e94cd2b5bc0d00", [:mix], [{:decimal, "~> 1.5", [hex: :decimal, repo: "hexpm", optional: false]}, {:ex_cldr, "~> 2.4", [hex: :ex_cldr, repo: "hexpm", optional: false]}, {:ex_cldr_currencies, "~> 2.2", [hex: :ex_cldr_currencies, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}], "hexpm"},
"ex_doc": {:hex, :ex_doc, "0.19.3", "3c7b0f02851f5fc13b040e8e925051452e41248f685e40250d7e40b07b9f8c10", [:mix], [{:earmark, "~> 1.2", [hex: :earmark, repo: "hexpm", optional: false]}, {:makeup_elixir, "~> 0.10", [hex: :makeup_elixir, repo: "hexpm", optional: false]}], "hexpm"},
"flow": {:hex, :flow, "0.12.0", "32c5a5f3ff6693e004b6c17a8c64dce2f8cdaf9564912d79427176013a586ab6", [:mix], [{:gen_stage, "~> 0.12.0", [hex: :gen_stage, optional: false]}]},
"gen_stage": {:hex, :gen_stage, "0.12.2", "e0e347cbb1ceb5f4e68a526aec4d64b54ad721f0a8b30aa9d28e0ad749419cbb", [:mix], []},
Expand Down
8 changes: 7 additions & 1 deletion priv/conversion_factors.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,13 @@
"day": 7
},
"yard": {
"foot": 3
"foot": 3,
"inch": 36
},
"mile": {
"yard": 1760,
"feet": 5280,
"inch": 63360
}
},
"acceleration": {
Expand Down

0 comments on commit 32352b6

Please sign in to comment.