Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions arrow/arrow.py
Original file line number Diff line number Diff line change
Expand Up @@ -1380,6 +1380,16 @@ def dehumanize(self, input_string: str, locale: str = "en_us") -> "Arrow":

current_time = self.fromdatetime(self._datetime)

# Humanized strings encode direction with the locale's past/future
# wording ("ago"/"in"), not a sign on the number. A negative value such
# as "in -1 hours" would otherwise have its sign dropped by the number
# regex below and be parsed as positive, so reject it explicitly.
if re.search(r"-\d", input_string):
raise ValueError(
"Invalid input string: negative numbers are not supported. "
"Use the past or future wording instead, e.g. '1 hour ago' or 'in 1 hour'."
)

# Create an object containing the relative time info
time_object_info = dict.fromkeys(
["seconds", "minutes", "hours", "days", "weeks", "months", "years"], 0
Expand Down
11 changes: 11 additions & 0 deletions tests/test_arrow.py
Original file line number Diff line number Diff line change
Expand Up @@ -2930,6 +2930,17 @@ def test_no_units_modified(self, locale_list_no_weeks: List[str]):
with pytest.raises(ValueError):
arw.dehumanize(empty_future_string, locale=lang)

# Negative numbers are not valid: direction comes from "ago"/"in" wording,
# not a sign, and the sign would otherwise be silently dropped.
def test_negative_number(self):
arw = arrow.Arrow(2000, 6, 18, 5, 55, 0)

with pytest.raises(ValueError):
arw.dehumanize("in -1 hours")

with pytest.raises(ValueError):
arw.dehumanize("-2 days ago")

def test_slavic_locales(self, slavic_locales: List[str]):
# Relevant units for Slavic locale plural logic
units = [
Expand Down
Loading