Skip to content

Commit

Permalink
fix: timedelta parsing for int and floats
Browse files Browse the repository at this point in the history
  • Loading branch information
Thomas Leonard committed Nov 15, 2024
1 parent a873df3 commit d9d93c0
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 6 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ The following are all type-casting methods of `Env`:
- `env.datetime`
- `env.date`
- `env.time`
- `env.timedelta` (assumes value is an integer in seconds, or an ordered duration string like `7h7s` or `7w 7d 7h 7m 7s 7ms 7us`)
- `env.timedelta` (assumes value is an float in seconds, or an ordered duration string like `7h7s` or `7w 7d 7h 7m 7s 7ms 7us`)
- `env.url`
- `env.uuid`
- `env.log_level`
Expand Down
8 changes: 8 additions & 0 deletions src/environs/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,14 @@ class TimeDeltaField(ma.fields.TimeDelta):
def _deserialize(self, value, *args, **kwargs) -> timedelta:
if isinstance(value, timedelta):
return value
if isinstance(value, (int, float)):
seconds = int(value)
return timedelta(
seconds=seconds,
microseconds=value-seconds,
)
if not isinstance(value, str):
raise ValueError("Not a valid period of time (must be either a int, float, timedelta or string).")
match = _TIMEDELTA_PATTERN.match(value)
if match is not None and match.group(0): # disallow "", allow "0s"
return timedelta(
Expand Down
40 changes: 35 additions & 5 deletions tests/test_environs.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,26 +229,45 @@ def test_date_cast(self, set_env, env):
assert env.date("DATE") == date

def test_timedelta_cast(self, set_env, env):
# seconds as integer
# default as an integer, a float or a timedelta
assert env.timedelta("NO_VALUE", default=0) == dt.timedelta()
assert env.timedelta("NO_VALUE", default=42) == dt.timedelta(seconds=42)
assert env.timedelta("NO_VALUE", default=-42) == dt.timedelta(seconds=-42)
assert env.timedelta("NO_VALUE", default=42.3) == dt.timedelta(seconds=42, microseconds=3)
assert env.timedelta("NO_VALUE", default=dt.timedelta(seconds=42)) == dt.timedelta(seconds=42)
assert env.timedelta("NO_VALUE", default=dt.timedelta(seconds=42, microseconds=3)) == dt.timedelta(seconds=42, microseconds=3)
# seconds as integer string
set_env({"TIMEDELTA": "0"})
assert env.timedelta("TIMEDELTA") == dt.timedelta()
assert env.timedelta("NO_VALUE", default="0") == dt.timedelta()
set_env({"TIMEDELTA": "42"})
assert env.timedelta("TIMEDELTA") == dt.timedelta(seconds=42)
assert env.timedelta("NO_VALUE", default="42") == dt.timedelta(seconds=42)
set_env({"TIMEDELTA": "-42"})
assert env.timedelta("TIMEDELTA") == dt.timedelta(seconds=-42)
assert env.timedelta("NO_VALUE", default="-42") == dt.timedelta(seconds=-42)
# seconds as a float string
set_env({"TIMEDELTA": "42.3"})
assert env.timedelta("TIMEDELTA") == dt.timedelta(seconds=42, microseconds=3)
assert env.timedelta("NO_VALUE", default="42.3") == dt.timedelta(seconds=42, microseconds=3)
# seconds as duration string
set_env({"TIMEDELTA": "0s"})
assert env.timedelta("TIMEDELTA") == dt.timedelta()
assert env.timedelta("NO_VALUE", default="0s") == dt.timedelta()
set_env({"TIMEDELTA": "42s"})
assert env.timedelta("TIMEDELTA") == dt.timedelta(seconds=42)
assert env.timedelta("NO_VALUE", default="42s") == dt.timedelta(seconds=42)
set_env({"TIMEDELTA": "-42s"})
assert env.timedelta("TIMEDELTA") == dt.timedelta(seconds=-42)
assert env.timedelta("NO_VALUE", default="-42s") == dt.timedelta(seconds=-42)
# whitespaces, units subselection (but descending ordering)
set_env({"TIMEDELTA": " 42 d -42s "})
assert env.timedelta("TIMEDELTA") == dt.timedelta(days=42, seconds=-42)
assert env.timedelta("NO_VALUE", default=" 42 d -42s ") == dt.timedelta(days=42, seconds=-42)
# unicode µs (in addition to us below)
set_env({"TIMEDELTA": "42µs"})
assert env.timedelta("TIMEDELTA") == dt.timedelta(microseconds=42)
assert env.timedelta("NO_VALUE", default="42µs") == dt.timedelta(microseconds=42)
# all supported units
set_env({"TIMEDELTA": "42w 42d 42h 42m 42s 42ms 42us"})
assert env.timedelta("TIMEDELTA") == dt.timedelta(
Expand All @@ -260,17 +279,28 @@ def test_timedelta_cast(self, set_env, env):
milliseconds=42,
microseconds=42,
)
assert env.timedelta("NO_VALUE", default="42w 42d 42h 42m 42s 42ms 42us") == dt.timedelta(
weeks=42,
days=42,
hours=42,
minutes=42,
seconds=42,
milliseconds=42,
microseconds=42,
)
# empty string not allowed
set_env({"TIMEDELTA": ""})
with pytest.raises(environs.EnvError):
env.timedelta("TIMEDELTA")
# float not allowed
set_env({"TIMEDELTA": "4.2"})
with pytest.raises(environs.EnvError):
env.timedelta("TIMEDELTA")
set_env({"TIMEDELTA": "4.2s"})
with pytest.raises(environs.EnvError):
env.timedelta("TIMEDELTA")
with pytest.raises(environs.EnvError):
env.timedelta("NO_VALUE", default="")
with pytest.raises(environs.EnvError):
env.timedelta("NO_VALUE", default="4.2s")
with pytest.raises(environs.EnvError):
env.timedelta("NO_VALUE", default=True)

def test_time_cast(self, set_env, env):
set_env({"TIME": "10:30"})
Expand Down

0 comments on commit d9d93c0

Please sign in to comment.