diff --git a/src/environs/__init__.py b/src/environs/__init__.py index d918666..f4072ff 100644 --- a/src/environs/__init__.py +++ b/src/environs/__init__.py @@ -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( diff --git a/tests/test_environs.py b/tests/test_environs.py index 7f8fa1d..752e28f 100644 --- a/tests/test_environs.py +++ b/tests/test_environs.py @@ -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() @@ -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() @@ -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( @@ -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,