Skip to content

Commit 1aca997

Browse files
committed
Added more type annotations
1 parent 318f982 commit 1aca997

File tree

4 files changed

+42
-33
lines changed

4 files changed

+42
-33
lines changed

doc-source/dates.rst

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,15 @@
22
:mod:`domdf_python_tools.dates`
33
*********************************
44

5+
.. extras-require:: dates
6+
7+
pytz >=2019.1
8+
59
.. contents:: Table of Contents
610

711

812
.. warning:: This module has not been fully tested. Use with caution.
913

10-
.. note:: This module requires the `pytz <https://pypi.org/project/pytz/>`_ package to be installed.
1114

1215
.. automodule:: domdf_python_tools.dates
1316
:members:

domdf_python_tools/dates.py

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -34,22 +34,23 @@
3434
# stdlib
3535
import datetime
3636
from collections import OrderedDict
37+
from typing import Union
3738

3839
# 3rd party
3940
try:
4041
import pytz
4142

42-
def get_utc_offset(tz, date=None):
43+
def get_utc_offset(tz, date: datetime.datetime = None) -> datetime.timedelta:
4344
"""
4445
Returns the offset between UTC and the requested timezone on the given date.
4546
If ``date`` is ``None`` then the current date is used.
4647
47-
:param tz: :class:`pytz.timezone` or a string representing the timezone
48+
:param tz: ``pytz.timezone`` or a string representing the timezone
4849
:type tz:
4950
:param date:
50-
:type date: :class:`python:datetime.datetime` or None, optional
51+
:type date: python:datetime.datetime, optional
5152
:return:
52-
:rtype: :class:`python:datetime.timedelta`
53+
:rtype: datetime.timedelta
5354
"""
5455

5556
if date is None:
@@ -60,17 +61,18 @@ def get_utc_offset(tz, date=None):
6061

6162
return date.replace(tzinfo=pytz.utc).astimezone(tz).utcoffset()
6263

63-
def get_timezone(tz, date=None):
64+
65+
def get_timezone(tz: str, date: datetime.datetime = None) -> datetime.timedelta:
6466
"""
6567
Returns a localized :class:`pytz.timezone` object for the given date.
6668
If ``date`` is ``None`` then the current date is used.
6769
6870
:param tz: A string representing a pytz timezone
69-
:type tz:
71+
:type tz: str
7072
:param date:
71-
:type date: :class:`python:datetime.datetime` or None, optional
73+
:type date: python:datetime.datetime, optional
7274
:return:
73-
:rtype: :class:`python:datetime.timedelta`
75+
:rtype: datetime.timedelta
7476
"""
7577

7678
if date is None:
@@ -80,6 +82,7 @@ def get_timezone(tz, date=None):
8082

8183
return pytz.timezone(tz).localize(d).tzinfo
8284

85+
8386
def current_tzinfo():
8487
"""
8588
Returns a tzinfo object for the current timezone
@@ -124,7 +127,8 @@ def set_timezone(obj, tzinfo):
124127

125128
return obj.replace(tzinfo=tzinfo)
126129

127-
def utc_timestamp_to_datetime(utc_timestamp, output_tz=None):
130+
131+
def utc_timestamp_to_datetime(utc_timestamp: Union[float, int], output_tz: datetime.tzinfo = None) -> datetime.datetime:
128132
"""
129133
Convert UTC timestamp (seconds from UNIX epoch) to a :class:`datetime.datetime` object
130134
@@ -139,10 +143,10 @@ def utc_timestamp_to_datetime(utc_timestamp, output_tz=None):
139143
:type utc_timestamp: float, int
140144
:param output_tz: The timezone to output the datetime object for.
141145
If None it will be inferred.
142-
:type output_tz: :class:`~python:datetime.tzinfo`
146+
:type output_tz: datetime.tzinfo
143147
144148
:return: The timestamp as a datetime object.
145-
:rtype: :class:`datetime.datetime`
149+
:rtype: datetime.datetime
146150
147151
:raises: :class:`~python:OverflowError` if the timestamp is out of the range
148152
of values supported by the platform C localtime() or gmtime() functions,
@@ -153,6 +157,7 @@ def utc_timestamp_to_datetime(utc_timestamp, output_tz=None):
153157
new_datetime = datetime.datetime.fromtimestamp(utc_timestamp, output_tz)
154158
return new_datetime.astimezone(output_tz)
155159

160+
156161
# List of months and their 3-character shortcodes.
157162
months = OrderedDict(
158163
Jan="January",
@@ -169,7 +174,8 @@ def utc_timestamp_to_datetime(utc_timestamp, output_tz=None):
169174
Dec="December",
170175
)
171176

172-
def parse_month(month):
177+
178+
def parse_month(month: Union[str, int]) -> str:
173179
"""
174180
Converts an integer or shorthand month into the full month name
175181
@@ -194,7 +200,8 @@ def parse_month(month):
194200
else:
195201
raise ValueError("Unrecognised month value")
196202

197-
def get_month_number(month):
203+
204+
def get_month_number(month: Union[str, int]) -> int:
198205
"""
199206
Returns the number of the given month. If ``month`` is already a
200207
number between 1 and 12 it will be returned immediately.
@@ -203,7 +210,7 @@ def get_month_number(month):
203210
:type month: str or int
204211
205212
:return: The number of the month
206-
:rtype:
213+
:rtype: int
207214
"""
208215

209216
if isinstance(month, int):
@@ -215,7 +222,7 @@ def get_month_number(month):
215222
month = parse_month(month)
216223
return list(months.values()).index(month) + 1
217224

218-
def check_date(month, day, leap_year=True):
225+
def check_date(month: Union[str, int], day: int, leap_year: bool = True) -> bool:
219226
"""
220227
Returns ``True`` if the day number is valid for the given month.
221228
Note that this will return ``True`` for the 29th Feb. If you don't
@@ -225,8 +232,8 @@ def check_date(month, day, leap_year=True):
225232
:type month: str, int
226233
:param day: The day number to test
227234
:type day: int
228-
:param leap_year: Whether to return ``True`` for 29th Feb
229-
:type leap_year: bool
235+
:param leap_year: Whether to return ``True`` for 29th Feb. Default ``True``
236+
:type leap_year: bool, optional
230237
231238
:return: ``True`` if the date is valid
232239
:rtype: bool

domdf_python_tools/doctools.py

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,12 @@
2727
# MA 02110-1301, USA.
2828
#
2929

30+
# stdlib
3031
import builtins
32+
from typing import Callable, Type, Union
3133

3234

33-
def deindent_string(string):
35+
def deindent_string(string: str) -> str:
3436
"""
3537
Removes all indentation from the given string
3638
@@ -47,22 +49,20 @@ def deindent_string(string):
4749

4850

4951
# Functions that do the work
50-
def document_object_from_another(target, original):
52+
def document_object_from_another(target: Union[Type, Callable], original: Union[Type, Callable]):
5153
"""
5254
Sets the docstring of the ``target`` function to that of the ``original`` function.
5355
5456
This may be useful for subclasses or wrappers that use the same arguments.
5557
5658
:param target: The object to set the docstring for
57-
:type target: any
5859
:param original: The object to copy the docstring from
59-
:type original: any
6060
"""
6161

6262
target.__doc__ = original.__doc__
6363

6464

65-
def append_doctring_from_another(target, original):
65+
def append_doctring_from_another(target: Union[Type, Callable], original: Union[Type, Callable]):
6666
"""
6767
Sets the docstring of the ``target`` function to that of the ``original`` function.
6868
@@ -73,9 +73,7 @@ def append_doctring_from_another(target, original):
7373
Bear this in mind if additional indentation is used in the docstring.
7474
7575
:param target: The object to append the docstring to
76-
:type target: any
7776
:param original: The object to copy the docstring from
78-
:type original: any
7977
"""
8078

8179
deindented_target_doc = deindent_string(target.__doc__)
@@ -115,23 +113,23 @@ def make_sphinx_links(input_string, builtins_list=None):
115113

116114

117115
# Decorators that call the above functions
118-
def is_documented_by(original):
116+
def is_documented_by(original: Union[Type, Callable]):
119117
"""
120-
Sets the docstring of the ``target`` function to that of the ``original`` function.
118+
Decorator to set the docstring of the ``target`` function to that of the ``original`` function.
121119
122120
This may be useful for subclasses or wrappers that use the same arguments.
123121
"""
124122

125-
def wrapper(target):
123+
def wrapper(target: Union[Type, Callable]):
126124
document_object_from_another(target, original)
127125
return target
128126

129127
return wrapper
130128

131129

132-
def append_docstring_from(original):
130+
def append_docstring_from(original: Union[Type, Callable]):
133131
"""
134-
Appends the docstring from the ``original`` function to the ``target`` function.
132+
Decorator to appends the docstring from the ``original`` function to the ``target`` function.
135133
136134
This may be useful for subclasses or wrappers that use the same arguments.
137135
@@ -140,7 +138,7 @@ def append_docstring_from(original):
140138
Bear this in mind if additional indentation is used in the docstring.
141139
"""
142140

143-
def wrapper(target):
141+
def wrapper(target: Union[Type, Callable]):
144142
append_doctring_from_another(target, original)
145143
return target
146144

@@ -149,15 +147,15 @@ def wrapper(target):
149147

150148
def sphinxify_docstring():
151149
r"""
152-
Make proper sphinx links out of double-backticked strings in docstring.
150+
Decorator to make proper sphinx links out of double-backticked strings in the docstring.
153151
154152
i.e. \`\`str\`\` becomes \:class\:\`~python:str\`
155153
156154
Make sure to have `'python': ('https://docs.python.org/3/', None),` in the
157155
`intersphinx_mapping` dict of your conf.py for sphinx.
158156
"""
159157

160-
def wrapper(target):
158+
def wrapper(target: Union[Type, Callable]):
161159
target.__doc__ = make_sphinx_links(target.__doc__)
162160
return target
163161

tests/test_enums.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ def test_str_enum():
2626
assert DramatisPersonae("The sender") == DramatisPersonae.Bob == "The sender"
2727
assert repr(DramatisPersonae.Bob) == "<DramatisPersonae.Bob: 'The sender'>"
2828

29+
2930
class Numbers(enums.IntEnum):
3031
One = 1
3132
Two = 2

0 commit comments

Comments
 (0)