Skip to content

Fix Duration.now/1 for minutes/hours/days/weeks and correct several typespecs - #798

Open
lukaszsamson wants to merge 1 commit into
bitwalker:mainfrom
lukaszsamson:fix-duration-now-and-specs
Open

lukaszsamson wants to merge 1 commit into
bitwalker:mainfrom
lukaszsamson:fix-duration-now-and-specs

Conversation

@lukaszsamson

Copy link
Copy Markdown

Summary of changes

Found with SpecLint on elixir 1.20.4

lib/time/duration.ex:615: warning: Timex.Duration.now/1: inferred clause (:days or :hours or :minutes or :weeks) -> float() has a return disjoint from the spec return integer()
    @spec now(nil) :: t()
    @spec now(units()) :: non_neg_integer()
    inferred: (:microsecond or :microseconds or :millisecond or :milliseconds or :nanosecond or :nanoseconds or
  :native or nil or :second or :seconds) -> dynamic()
    inferred: (:days or :hours or :minutes or :weeks) -> float()
lib/calendar/julian.ex:23: error: Timex.Calendar.Julian.julian_date/1: inferred return float() or {:error, :invalid_datetime} is disjoint from the spec return integer()
    @spec julian_date(Timex.Types.date()) :: integer()
    @spec julian_date(Timex.Types.datetime()) :: integer()
    inferred: ({term(), term(), term()}) -> dynamic(integer() or {:error, :invalid_date})
    inferred: ({{term(), term(), term()}, {term(), term(), term()}}) -> dynamic(float() or {:error, :invalid_datetime})
    inferred: (not ({term(), term(), term()} and not {{term(), term(), term()}, {term(), term(), term()}}) and
  not {{term(), term(), term()}, {term(), term(), term()}}) -> {:error, :invalid_date}
lib/timex.ex:1908: warning: Timex.normalize/2: inferred return includes {term(), term(), term(), term()}, which the spec does not declare
    @spec normalize(:date, {integer(), integer(), integer()}) :: Timex.Types.date()
    @spec normalize(:time, {integer(), integer(), integer()} | {integer(), integer(), integer(), integer()}) ::
  Timex.Types.time()
    @spec normalize(:day, {integer(), integer(), integer()}) :: non_neg_integer()
    @spec normalize(
  :year | :month | :day | :hour | :minute | :second | :millisecond | :microsecond,
  integer()
) :: non_neg_integer()
    inferred: (:date or :time, {term(), term(), term()}) -> dynamic({term(), term(), term()})
    inferred: (:year, term()) -> dynamic() or integer()
    inferred: (:hour or :millisecond or :minute or :month or :second, term()) -> dynamic()
    inferred: (:time, {term(), term(), term(), term()}) -> dynamic({term(), term(), term(), term()})
    inferred: (:microsecond, {term(), term()}) -> dynamic({term(), term()})
    inferred: (:microsecond, integer()) -> dynamic()
    inferred: (:day, {term(), term(), term()}) -> dynamic()
lib/datetime/datetime.ex:440: warning: Timex.Protocol.DateTime.shift/2: inferred return includes %Timex.AmbiguousDateTime{type: :ambiguous or :gap} or
  (%DateTime{} and
     not %DateTime{
       year: integer(),
       month: integer(),
       day: integer(),
       hour: integer(),
       minute: integer(),
       second: integer(),
       time_zone: binary(),
       zone_abbr: binary(),
       utc_offset: integer(),
       std_offset: integer(),
       microsecond: {integer(), integer()},
       calendar: atom()
     }), which the spec does not declare
    @spec shift(DateTime.t(), [{atom(), term()}]) :: DateTime.t() | {:error, term()}
    inferred: (%DateTime{}, empty_list() or non_empty_list(term(), term())) -> dynamic(%DateTime{} or %Timex.AmbiguousDateTime{type: :ambiguous or :gap} or {:error, term()})
lib/parse/zoneinfo/parser.ex:112: warning: Timex.Parse.ZoneInfo.Parser.parse/1: inferred clause (term()) -> {:error, :invalid_zoneinfo_content} has a return disjoint from the spec return {:error, binary()} or {:ok, term()}
    @spec parse(binary()) :: {:ok, Timex.Parse.ZoneInfo.Parser.Zone.t()} | {:error, binary()}
    inferred: (binary()) -> dynamic({:error, term()} or {:ok, %{..., leaps: term(), transitions: term()}})
    inferred: (term()) -> {:error, :invalid_zoneinfo_content}

Timex.Duration.now/1 raised FunctionClauseError for :minutes, :hours, :days and :weeks (it passed an integer to to_minutes/1 etc., which take a Duration); it now converts now() and truncates to the integer the spec declares.

Checklist

  • New functions have typespecs, changed functions were updated
  • Same for documentation, including moduledocs
  • Tests were added or updated to cover changes
  • Commits were squashed into a single coherent commit
  • Notes added to CHANGELOG file which describe changes at a high-level

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

Successfully merging this pull request may close these issues.

1 participant