Skip to content

Commit

Permalink
[pre-commit.ci] auto fixes from pre-commit.com hooks
Browse files Browse the repository at this point in the history
for more information, see https://pre-commit.ci
  • Loading branch information
pre-commit-ci[bot] committed Nov 15, 2024
1 parent d9d93c0 commit 070d1a1
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 9 deletions.
6 changes: 4 additions & 2 deletions src/environs/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -382,10 +382,12 @@ def _deserialize(self, value, *args, **kwargs) -> timedelta:
seconds = int(value)
return timedelta(
seconds=seconds,
microseconds=value-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).")
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
28 changes: 21 additions & 7 deletions tests/test_environs.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,9 +233,15 @@ def test_timedelta_cast(self, set_env, env):
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)
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()
Expand All @@ -249,7 +255,9 @@ def test_timedelta_cast(self, set_env, env):
# 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)
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()
Expand All @@ -263,11 +271,15 @@ def test_timedelta_cast(self, set_env, env):
# 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)
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)
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 @@ -279,7 +291,9 @@ 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(
assert env.timedelta(
"NO_VALUE", default="42w 42d 42h 42m 42s 42ms 42us"
) == dt.timedelta(
weeks=42,
days=42,
hours=42,
Expand Down

0 comments on commit 070d1a1

Please sign in to comment.