Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Add documentation and type hints to parse_duration. #9432

Merged
merged 2 commits into from
Feb 19, 2021
Merged
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
1 change: 1 addition & 0 deletions changelog.d/9432.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add documentation and type hints to `parse_duration`.
17 changes: 15 additions & 2 deletions synapse/config/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
from collections import OrderedDict
from hashlib import sha256
from textwrap import dedent
from typing import Any, Iterable, List, MutableMapping, Optional
from typing import Any, Iterable, List, MutableMapping, Optional, Union

import attr
import jinja2
Expand Down Expand Up @@ -147,7 +147,20 @@ def parse_size(value):
return int(value) * size

@staticmethod
def parse_duration(value):
def parse_duration(value: Union[str, int]) -> int:
"""Convert a duration as a string or integer to a number of milliseconds.

If an integer is provided it is treated as milliseconds and is unchanged.

String durations can have a suffix of 's', 'm', 'h', 'd', 'w', or 'y'.
No suffix is treated as milliseconds.

Args:
value: The duration to parse.

Returns:
The number of milliseconds in the duration.
"""
if isinstance(value, int):
return value
second = 1000
Expand Down