Skip to content

Commit 1056c73

Browse files
committed
Added type annotations
1 parent 494e187 commit 1056c73

29 files changed

+567
-433
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 & 8 deletions
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
@@ -39,13 +39,6 @@ jobs:
3939
python: 3.8
4040
services:
4141
- docker
42-
- name: "Ubuntu Focal (20.04) (Docker) with Python 3.5 (tox)"
43-
env: [TOXENV="py35", UBUNTU_VERSION="20.04"]
44-
group: edge
45-
language: python
46-
python: 3.5
47-
services:
48-
- docker
4942
- name: "Ubuntu Focal (20.04) (Docker) with Python 3.6 (tox)"
5043
env: [TOXENV="py36", UBUNTU_VERSION="20.04"]
5144
group: edge

config/dpkg/changelog

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
dfdatetime (20200501-1) unstable; urgency=low
1+
dfdatetime (20200601-1) unstable; urgency=low
22

33
* Auto-generated
44

5-
-- Log2Timeline maintainers <log2timeline-maintainers@googlegroups.com> Fri, 01 May 2020 13:44:09 +0200
5+
-- Log2Timeline maintainers <log2timeline-maintainers@googlegroups.com> Mon, 01 Jun 2020 07:47:13 +0200

config/dpkg/control

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ Source: dfdatetime
22
Section: python
33
Priority: extra
44
Maintainer: Log2Timeline maintainers <log2timeline-maintainers@googlegroups.com>
5-
Build-Depends: debhelper (>= 9), dh-python, python3-all (>= 3.5~), python3-setuptools
5+
Build-Depends: debhelper (>= 9), dh-python, python3-all (>= 3.6~), python3-setuptools
66
Standards-Version: 4.1.4
7-
X-Python3-Version: >= 3.5
7+
X-Python3-Version: >= 3.6
88
Homepage: https://github.com/log2timeline/dfdatetime
99

1010
Package: python3-dfdatetime

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} mypy pylint python3-distutils";
9393
fi
9494
if test "${TARGET}" != "jenkins3";
9595
then
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+
RESULT=${EXIT_FAILURE};
28+
fi
2229
done
30+
31+
mypy --version
32+
33+
mypy --strict dfdatetime;
34+
35+
if test $? -ne ${EXIT_SUCCESS};
36+
then
37+
RESULT=${EXIT_FAILURE};
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/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@
55
objects to preserve accuracy and precision.
66
"""
77

8-
__version__ = '20200501'
8+
__version__ = '20200601'

dfdatetime/apfs_time.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55

66
import decimal
77

8+
from typing import Union # pylint: disable=unused-import
9+
810
from dfdatetime import definitions
911
from dfdatetime import posix_time
1012

@@ -19,14 +21,14 @@ class APFSTime(posix_time.PosixTimeInNanoseconds):
1921
is_local_time (bool): True if the date and time value is in local time.
2022
"""
2123

22-
def _GetNormalizedTimestamp(self):
24+
def _GetNormalizedTimestamp(self) -> 'Union[decimal.Decimal, None]':
2325
"""Retrieves the normalized timestamp.
2426
2527
Returns:
2628
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.
29+
seconds since January 1, 1970 00:00:00 and a fraction of second
30+
used for increased precision, or None if the normalized timestamp
31+
cannot be determined.
3032
"""
3133
if self._normalized_timestamp is None:
3234
if (self._timestamp is not None and self._timestamp >= self._INT64_MIN and
@@ -37,7 +39,7 @@ def _GetNormalizedTimestamp(self):
3739

3840
return self._normalized_timestamp
3941

40-
def CopyFromDateTimeString(self, time_string):
42+
def CopyFromDateTimeString(self, time_string: 'str') -> 'None':
4143
"""Copies a APFS timestamp from a date and time string.
4244
4345
Args:
@@ -58,7 +60,7 @@ def CopyFromDateTimeString(self, time_string):
5860
self._timestamp > self._INT64_MAX):
5961
raise ValueError('Date time value not supported.')
6062

61-
def CopyToDateTimeString(self):
63+
def CopyToDateTimeString(self) -> 'Union[str, None]':
6264
"""Copies the APFS timestamp to a date and time string.
6365
6466
Returns:

dfdatetime/cocoa_time.py

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,16 @@
55

66
import decimal
77

8+
from typing import Optional, Union # pylint: disable=unused-import
9+
810
from dfdatetime import definitions
911
from dfdatetime import interface
1012

1113

1214
class CocoaTimeEpoch(interface.DateTimeEpoch):
1315
"""Cocoa time epoch."""
1416

15-
def __init__(self):
17+
def __init__(self) -> 'None':
1618
"""Initializes a Cocoa time epoch."""
1719
super(CocoaTimeEpoch, self).__init__(2001, 1, 1)
1820

@@ -32,32 +34,33 @@ class CocoaTime(interface.DateTimeValues):
3234
is_local_time (bool): True if the date and time value is in local time.
3335
"""
3436
# The difference between January 1, 2001 and January 1, 1970 in seconds.
35-
_COCOA_TO_POSIX_BASE = -978307200
37+
_COCOA_TO_POSIX_BASE: 'int' = -978307200
3638

37-
_EPOCH = CocoaTimeEpoch()
39+
_EPOCH: 'interface.DateTimeEpoch' = CocoaTimeEpoch()
3840

39-
def __init__(self, timestamp=None):
41+
def __init__(self, timestamp: 'Optional[float]' = None) -> 'None':
4042
"""Initializes a Cocoa timestamp.
4143
4244
Args:
4345
timestamp (Optional[float]): Cocoa timestamp.
4446
"""
4547
super(CocoaTime, self).__init__()
46-
self._precision = definitions.PRECISION_1_SECOND
47-
self._timestamp = timestamp
48+
self._precision: 'str' = definitions.PRECISION_1_SECOND
49+
self._timestamp: 'Union[float, None]' = timestamp
4850

4951
@property
50-
def timestamp(self):
52+
def timestamp(self) -> 'Union[float, None]':
5153
"""float: Cocoa timestamp or None if timestamp is not set."""
5254
return self._timestamp
5355

54-
def _GetNormalizedTimestamp(self):
56+
def _GetNormalizedTimestamp(self) -> 'Union[decimal.Decimal, None]':
5557
"""Retrieves the normalized timestamp.
5658
5759
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.
60+
decimal.Decimal: normalized timestamp, which contains the number of
61+
seconds since January 1, 1970 00:00:00 and a fraction of second
62+
used for increased precision, or None if the normalized timestamp
63+
cannot be determined.
6164
"""
6265
if self._normalized_timestamp is None:
6366
if self._timestamp is not None:
@@ -66,7 +69,7 @@ def _GetNormalizedTimestamp(self):
6669

6770
return self._normalized_timestamp
6871

69-
def CopyFromDateTimeString(self, time_string):
72+
def CopyFromDateTimeString(self, time_string: 'str') -> 'None':
7073
"""Copies a Cocoa timestamp from a date and time string.
7174
7275
Args:
@@ -92,19 +95,20 @@ def CopyFromDateTimeString(self, time_string):
9295
microseconds = date_time_values.get('microseconds', None)
9396
time_zone_offset = date_time_values.get('time_zone_offset', 0)
9497

95-
timestamp = self._GetNumberOfSecondsFromElements(
96-
year, month, day_of_month, hours, minutes, seconds, time_zone_offset)
97-
timestamp += self._COCOA_TO_POSIX_BASE
98+
number_of_seconds = self._GetNumberOfSecondsFromElements(
99+
year, month, day_of_month, hours, minutes, seconds,
100+
time_zone_offset=time_zone_offset)
101+
number_of_seconds += self._COCOA_TO_POSIX_BASE
98102

99-
timestamp = float(timestamp)
103+
timestamp = float(number_of_seconds)
100104
if microseconds is not None:
101105
timestamp += float(microseconds) / definitions.MICROSECONDS_PER_SECOND
102106

103107
self._normalized_timestamp = None
104108
self._timestamp = timestamp
105109
self._time_zone_offset = time_zone_offset
106110

107-
def CopyToDateTimeString(self):
111+
def CopyToDateTimeString(self) -> 'Union[str, None]':
108112
"""Copies the Cocoa timestamp to a date and time string.
109113
110114
Returns:

0 commit comments

Comments
 (0)