Skip to content

Commit

Permalink
Added Error Checking For Empty Granularity List (#1016)
Browse files Browse the repository at this point in the history
* Added Error Checking

* Updated Error Message To Be More Descriptive

Co-authored-by: Chris <[email protected]>

* Improved Error Message String

Co-authored-by: Anish Nyayachavadi <[email protected]>
Co-authored-by: Chris <[email protected]>
Co-authored-by: Jad Chaar <[email protected]>
  • Loading branch information
4 people authored Oct 24, 2021
1 parent f7a3aa3 commit cc1cbeb
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
7 changes: 7 additions & 0 deletions arrow/arrow.py
Original file line number Diff line number Diff line change
Expand Up @@ -1263,6 +1263,13 @@ def humanize(
return locale.describe(granularity, delta, only_distance=only_distance)

else:

if not granularity:
raise ValueError(
"Empty granularity list provided. "
"Please select one or more from 'second', 'minute', 'hour', 'day', 'week', 'month', 'quarter', 'year'."
)

timeframes: List[Tuple[TimeFrameLiteral, float]] = []

def gather_timeframes(_delta: float, _frame: TimeFrameLiteral) -> float:
Expand Down
7 changes: 7 additions & 0 deletions tests/test_arrow.py
Original file line number Diff line number Diff line change
Expand Up @@ -2286,6 +2286,13 @@ def test_untranslated_granularity(self, mocker):
with pytest.raises(ValueError):
arw.humanize(later, granularity="week")

def test_empty_granularity_list(self):
arw = arrow.Arrow(2013, 1, 1, 0, 0, 0)
later = arw.shift(seconds=55000)

with pytest.raises(ValueError):
arw.humanize(later, granularity=[])

# Bulgarian is an example of a language that overrides _format_timeframe
# Applicabale to all locales. Note: Contributors need to make sure
# that if they override describe or describe_mutli, that delta
Expand Down

0 comments on commit cc1cbeb

Please sign in to comment.