Skip to content

Commit 2bd423b

Browse files
committed
Added type annotations
1 parent 494e187 commit 2bd423b

24 files changed

+469
-366
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@
2121
# Pycharm files
2222
/.idea
2323

24+
# Type check files
25+
/.mypy_cache
26+
2427
# Test coverage files
2528
.coverage
2629
tests-coverage.txt

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ dist: bionic
55
jobs:
66
include:
77
- name: "Pylint on Ubuntu Bionic (20.04) (Docker) with Python 3.8"
8-
env: [TARGET="pylint", UBUNTU_VERSION="20.04"]
8+
env: [TARGET="lint_and_type_check", UBUNTU_VERSION="20.04"]
99
group: edge
1010
language: python
1111
python: 3.8

config/travis/install.sh

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,9 @@ then
3737
else
3838
RPM_PACKAGES="";
3939

40-
if test ${TARGET} = "pylint";
40+
if test ${TARGET} = "lint_and_type_check";
4141
then
42-
RPM_PACKAGES="${RPM_PACKAGES} findutils pylint";
42+
RPM_PACKAGES="${RPM_PACKAGES} findutils pylint python3-mypy";
4343
fi
4444
RPM_PACKAGES="${RPM_PACKAGES} python3 ${RPM_PYTHON3_DEPENDENCIES} ${RPM_PYTHON3_TEST_DEPENDENCIES}";
4545
fi
@@ -87,9 +87,9 @@ then
8787
then
8888
DPKG_PACKAGES="${DPKG_PACKAGES} sudo";
8989

90-
elif test ${TARGET} = "pylint";
90+
elif test ${TARGET} = "lint_and_type_check";
9191
then
92-
DPKG_PACKAGES="${DPKG_PACKAGES} python3-distutils pylint";
92+
DPKG_PACKAGES="${DPKG_PACKAGES} pylint python3-distutils python3-mypy";
9393
fi
9494
if test "${TARGET}" != "jenkins3";
9595
then

config/travis/run_pylint.sh renamed to config/travis/run_checks.sh

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
#!/bin/bash
22
#
3-
# Script to run pylint on Travis-CI.
3+
# Script to run lint and type checks on Travis-CI.
44
#
55
# This file is generated by l2tdevtools update-dependencies.py, any dependency
66
# related changes should be made in dependencies.ini.
77

8-
# Exit on error.
9-
set -e;
8+
EXIT_SUCCESS=0;
9+
EXIT_FAILURE=1;
10+
11+
RESULT=${EXIT_SUCCESS};
1012

1113
pylint --version
1214

@@ -19,4 +21,20 @@ do
1921
echo "Checking: ${FILE}";
2022

2123
pylint --rcfile=.pylintrc ${FILE};
24+
25+
if test $? -ne ${EXIT_SUCCESS};
26+
then
27+
REUSLT=${EXIT_SUCCESS};
28+
fi
2229
done
30+
31+
mypy --version
32+
33+
mypy --strict dfdatetime tests;
34+
35+
if test $? -ne ${EXIT_SUCCESS};
36+
then
37+
REUSLT=${EXIT_SUCCESS};
38+
fi
39+
40+
exit ${RESULT};

config/travis/runtests.sh

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ then
1717
then
1818
TEST_COMMAND="tox -e ${TOXENV}";
1919

20-
elif test "${TARGET}" = "pylint";
20+
elif test "${TARGET}" = "lint_and_type_check";
2121
then
22-
TEST_COMMAND="./config/travis/run_pylint.sh";
22+
TEST_COMMAND="./config/travis/run_checks.sh";
2323
else
2424
TEST_COMMAND="./config/travis/run_python3.sh";
2525
fi
@@ -51,9 +51,9 @@ then
5151
then
5252
TEST_COMMAND="./config/jenkins/linux/run_end_to_end_tests_py3.sh travis";
5353

54-
elif test "${TARGET}" = "pylint";
54+
elif test "${TARGET}" = "lint_and_type_check";
5555
then
56-
TEST_COMMAND="./config/travis/run_pylint.sh";
56+
TEST_COMMAND="./config/travis/run_checks.sh";
5757
else
5858
TEST_COMMAND="./config/travis/run_python3.sh";
5959
fi

dfdatetime/apfs_time.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from __future__ import unicode_literals
55

66
import decimal
7+
import typing
78

89
from dfdatetime import definitions
910
from dfdatetime import posix_time
@@ -19,14 +20,14 @@ class APFSTime(posix_time.PosixTimeInNanoseconds):
1920
is_local_time (bool): True if the date and time value is in local time.
2021
"""
2122

22-
def _GetNormalizedTimestamp(self):
23+
def _GetNormalizedTimestamp(self) -> typing.Union[decimal.Decimal, None]:
2324
"""Retrieves the normalized timestamp.
2425
2526
Returns:
2627
decimal.Decimal: normalized timestamp, which contains the number of
27-
seconds since January 1, 1970 00:00:00 and a fraction of second used
28-
for increased precision, or None if the normalized timestamp cannot be
29-
determined.
28+
seconds since January 1, 1970 00:00:00 and a fraction of second
29+
used for increased precision, or None if the normalized timestamp
30+
cannot be determined.
3031
"""
3132
if self._normalized_timestamp is None:
3233
if (self._timestamp is not None and self._timestamp >= self._INT64_MIN and
@@ -37,7 +38,7 @@ def _GetNormalizedTimestamp(self):
3738

3839
return self._normalized_timestamp
3940

40-
def CopyFromDateTimeString(self, time_string):
41+
def CopyFromDateTimeString(self, time_string: str) -> None:
4142
"""Copies a APFS timestamp from a date and time string.
4243
4344
Args:
@@ -58,7 +59,7 @@ def CopyFromDateTimeString(self, time_string):
5859
self._timestamp > self._INT64_MAX):
5960
raise ValueError('Date time value not supported.')
6061

61-
def CopyToDateTimeString(self):
62+
def CopyToDateTimeString(self) -> typing.Union[str, None]:
6263
"""Copies the APFS timestamp to a date and time string.
6364
6465
Returns:

dfdatetime/cocoa_time.py

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from __future__ import unicode_literals
55

66
import decimal
7+
import typing
78

89
from dfdatetime import definitions
910
from dfdatetime import interface
@@ -12,7 +13,7 @@
1213
class CocoaTimeEpoch(interface.DateTimeEpoch):
1314
"""Cocoa time epoch."""
1415

15-
def __init__(self):
16+
def __init__(self) -> None:
1617
"""Initializes a Cocoa time epoch."""
1718
super(CocoaTimeEpoch, self).__init__(2001, 1, 1)
1819

@@ -36,28 +37,29 @@ class CocoaTime(interface.DateTimeValues):
3637

3738
_EPOCH = CocoaTimeEpoch()
3839

39-
def __init__(self, timestamp=None):
40+
def __init__(self, timestamp: typing.Optional[float] = None) -> None:
4041
"""Initializes a Cocoa timestamp.
4142
4243
Args:
4344
timestamp (Optional[float]): Cocoa timestamp.
4445
"""
4546
super(CocoaTime, self).__init__()
46-
self._precision = definitions.PRECISION_1_SECOND
47-
self._timestamp = timestamp
47+
self._precision: str = definitions.PRECISION_1_SECOND
48+
self._timestamp: typing.Union[float, None] = timestamp
4849

4950
@property
50-
def timestamp(self):
51+
def timestamp(self) -> typing.Union[float, None]:
5152
"""float: Cocoa timestamp or None if timestamp is not set."""
5253
return self._timestamp
5354

54-
def _GetNormalizedTimestamp(self):
55+
def _GetNormalizedTimestamp(self) -> typing.Union[decimal.Decimal, None]:
5556
"""Retrieves the normalized timestamp.
5657
5758
Returns:
58-
float: normalized timestamp, which contains the number of seconds since
59-
January 1, 1970 00:00:00 and a fraction of second used for increased
60-
precision, or None if the normalized timestamp cannot be determined.
59+
decimal.Decimal: normalized timestamp, which contains the number of
60+
seconds since January 1, 1970 00:00:00 and a fraction of second
61+
used for increased precision, or None if the normalized timestamp
62+
cannot be determined.
6163
"""
6264
if self._normalized_timestamp is None:
6365
if self._timestamp is not None:
@@ -66,7 +68,7 @@ def _GetNormalizedTimestamp(self):
6668

6769
return self._normalized_timestamp
6870

69-
def CopyFromDateTimeString(self, time_string):
71+
def CopyFromDateTimeString(self, time_string: str) -> None:
7072
"""Copies a Cocoa timestamp from a date and time string.
7173
7274
Args:
@@ -92,19 +94,19 @@ def CopyFromDateTimeString(self, time_string):
9294
microseconds = date_time_values.get('microseconds', None)
9395
time_zone_offset = date_time_values.get('time_zone_offset', 0)
9496

95-
timestamp = self._GetNumberOfSecondsFromElements(
97+
number_of_seconds = self._GetNumberOfSecondsFromElements(
9698
year, month, day_of_month, hours, minutes, seconds, time_zone_offset)
97-
timestamp += self._COCOA_TO_POSIX_BASE
99+
number_of_seconds += self._COCOA_TO_POSIX_BASE
98100

99-
timestamp = float(timestamp)
101+
timestamp = float(number_of_seconds)
100102
if microseconds is not None:
101103
timestamp += float(microseconds) / definitions.MICROSECONDS_PER_SECOND
102104

103105
self._normalized_timestamp = None
104106
self._timestamp = timestamp
105107
self._time_zone_offset = time_zone_offset
106108

107-
def CopyToDateTimeString(self):
109+
def CopyToDateTimeString(self) -> typing.Union[str, None]:
108110
"""Copies the Cocoa timestamp to a date and time string.
109111
110112
Returns:

dfdatetime/decorators.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,19 @@
44
from __future__ import unicode_literals
55

66
import warnings
7+
import typing
78

89

9-
def deprecated(function): # pylint: disable=invalid-name
10+
# pylint: disable=invalid-name
11+
RETURN_TYPE = typing.TypeVar('RETURN_TYPE')
12+
13+
14+
def deprecated(function: typing.Callable[..., RETURN_TYPE]) -> typing.Callable[
15+
..., RETURN_TYPE]:
1016
"""Decorator to mark functions or methods as deprecated."""
1117

12-
def IssueDeprecationWarning(*args, **kwargs):
18+
def IssueDeprecationWarning(
19+
*args: typing.Any, **kwargs: typing.Any) -> RETURN_TYPE:
1320
"""Issue a deprecation warning."""
1421
warnings.simplefilter('default', DeprecationWarning)
1522
warnings.warn('Call to deprecated function: {0:s}.'.format(

dfdatetime/delphi_date_time.py

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from __future__ import unicode_literals
55

66
import decimal
7+
import typing
78

89
from dfdatetime import definitions
910
from dfdatetime import interface
@@ -12,7 +13,7 @@
1213
class DelphiDateTimeEpoch(interface.DateTimeEpoch):
1314
"""Delphi TDateTime epoch."""
1415

15-
def __init__(self):
16+
def __init__(self) -> None:
1617
"""Initializes a Delphi TDateTime epoch."""
1718
super(DelphiDateTimeEpoch, self).__init__(1899, 12, 30)
1819

@@ -38,29 +39,29 @@ class DelphiDateTime(interface.DateTimeValues):
3839

3940
_EPOCH = DelphiDateTimeEpoch()
4041

41-
def __init__(self, timestamp=None):
42+
def __init__(self, timestamp: typing.Optional[float] = None) -> None:
4243
"""Initializes a Delphi TDateTime timestamp.
4344
4445
Args:
4546
timestamp (Optional[float]): Delphi TDateTime timestamp.
4647
"""
4748
super(DelphiDateTime, self).__init__()
48-
self._precision = definitions.PRECISION_1_MILLISECOND
49-
self._timestamp = timestamp
49+
self._precision: str = definitions.PRECISION_1_MILLISECOND
50+
self._timestamp: typing.Union[float, None] = timestamp
5051

5152
@property
52-
def timestamp(self):
53+
def timestamp(self) -> typing.Union[float, None]:
5354
"""float: Delphi TDateTime timestamp or None if timestamp is not set."""
5455
return self._timestamp
5556

56-
def _GetNormalizedTimestamp(self):
57+
def _GetNormalizedTimestamp(self) -> typing.Union[decimal.Decimal, None]:
5758
"""Retrieves the normalized timestamp.
5859
5960
Returns:
6061
decimal.Decimal: normalized timestamp, which contains the number of
61-
seconds since January 1, 1970 00:00:00 and a fraction of second used
62-
for increased precision, or None if the normalized timestamp cannot be
63-
determined.
62+
seconds since January 1, 1970 00:00:00 and a fraction of second
63+
used for increased precision, or None if the normalized timestamp
64+
cannot be determined.
6465
"""
6566
if self._normalized_timestamp is None:
6667
if self._timestamp is not None:
@@ -70,7 +71,7 @@ def _GetNormalizedTimestamp(self):
7071

7172
return self._normalized_timestamp
7273

73-
def CopyFromDateTimeString(self, time_string):
74+
def CopyFromDateTimeString(self, time_string: str) -> None:
7475
"""Copies a Delphi TDateTime timestamp from a string.
7576
7677
Args:
@@ -99,10 +100,10 @@ def CopyFromDateTimeString(self, time_string):
99100
if year > 9999:
100101
raise ValueError('Unsupported year value: {0:d}.'.format(year))
101102

102-
timestamp = self._GetNumberOfSecondsFromElements(
103+
number_of_seconds = self._GetNumberOfSecondsFromElements(
103104
year, month, day_of_month, hours, minutes, seconds, time_zone_offset)
104105

105-
timestamp = float(timestamp) / definitions.SECONDS_PER_DAY
106+
timestamp = float(number_of_seconds) / definitions.SECONDS_PER_DAY
106107
timestamp += self._DELPHI_TO_POSIX_BASE
107108
if microseconds is not None:
108109
timestamp += float(microseconds) / definitions.MICROSECONDS_PER_DAY
@@ -111,7 +112,7 @@ def CopyFromDateTimeString(self, time_string):
111112
self._timestamp = timestamp
112113
self._time_zone_offset = time_zone_offset
113114

114-
def CopyToDateTimeString(self):
115+
def CopyToDateTimeString(self) -> typing.Union[str, None]:
115116
"""Copies the Delphi TDateTime timestamp to a date and time string.
116117
117118
Returns:

0 commit comments

Comments
 (0)