Skip to content

add type hints to make "mypy" happy about "import pytimeparse" #25

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
Empty file added pytimeparse/py.typed
Empty file.
11 changes: 7 additions & 4 deletions pytimeparse/timeparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
# SOFTWARE.

import re
from typing import Optional, Union, Dict

SIGN = r'(?P<sign>[+|-])?'
#YEARS = r'(?P<years>\d+)\s*(?:ys?|yrs?.?|years?)'
Expand All @@ -48,7 +49,7 @@
DAYCLOCK = (r'(?P<days>\d+):(?P<hours>\d{2}):'
r'(?P<mins>\d{2}):(?P<secs>\d{2}(?:\.\d+)?)')

OPT = lambda x: r'(?:{x})?'.format(x=x, SEPARATORS=SEPARATORS)
OPT = lambda x: r'(?:{x})?'.format(x=x)
OPTSEP = lambda x: r'(?:{x}\s*(?:{SEPARATORS}\s*)?)?'.format(
x=x, SEPARATORS=SEPARATORS)

Expand Down Expand Up @@ -91,7 +92,7 @@
('secs', 1)
])

def _interpret_as_minutes(sval, mdict):
def _interpret_as_minutes(sval: str, mdict: Dict[str, str]) -> Dict[str, str]:
"""
Times like "1:22" are ambiguous; do they represent minutes and seconds
or hours and minutes? By default, timeparse assumes the latter. Call
Expand All @@ -115,7 +116,7 @@ def _interpret_as_minutes(sval, mdict):
pass
return mdict

def timeparse(sval, granularity='seconds'):
def timeparse(sval: str, granularity: str = 'seconds') -> Optional[Union[int, float]]:
'''
Parse a time expression, returning it as a number of seconds. If
possible, the return value will be an `int`; if this is not
Expand Down Expand Up @@ -154,6 +155,7 @@ def timeparse(sval, granularity='seconds'):
5400
'''
match = COMPILED_SIGN.match(sval)
assert match is not None
sign = -1 if match.groupdict()['sign'] == '-' else 1
sval = match.groupdict()['unsigned']
for timefmt in COMPILED_TIMEFORMATS:
Expand All @@ -177,5 +179,6 @@ def timeparse(sval, granularity='seconds'):
(int(mdict['secs'], 10) if mdict['secs'] else 0))
else:
# SECS is a float, we will return a float
return sign * sum([MULTIPLIERS[k] * float(v) for (k, v) in
return float(sign) * sum([MULTIPLIERS[k] * float(v) for (k, v) in
list(mdict.items()) if v is not None])
return None
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@
# installed, specify them here. If using Python 2.6 or less, then these
# have to be included in MANIFEST.in as well.
package_data={
'pytimeparse': ['VERSION'],
'pytimeparse': ['VERSION', 'py.typed'],
},

# Although 'package_data' is the preferred approach, in some case you may
Expand Down