Skip to content
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

Added type annotations #157

Open
wants to merge 1 commit into
base: main
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
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@
# Pycharm files
/.idea

# Type check files
/.mypy_cache

# Test coverage files
.coverage
tests-coverage.txt
9 changes: 1 addition & 8 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ dist: bionic
jobs:
include:
- name: "Pylint on Ubuntu Bionic (20.04) (Docker) with Python 3.8"
env: [TARGET="pylint", UBUNTU_VERSION="20.04"]
env: [TARGET="lint_and_type_check", UBUNTU_VERSION="20.04"]
group: edge
language: python
python: 3.8
Expand Down Expand Up @@ -39,13 +39,6 @@ jobs:
python: 3.8
services:
- docker
- name: "Ubuntu Focal (20.04) (Docker) with Python 3.5 (tox)"
env: [TOXENV="py35", UBUNTU_VERSION="20.04"]
group: edge
language: python
python: 3.5
services:
- docker
- name: "Ubuntu Focal (20.04) (Docker) with Python 3.6 (tox)"
env: [TOXENV="py36", UBUNTU_VERSION="20.04"]
group: edge
Expand Down
4 changes: 2 additions & 2 deletions config/dpkg/changelog
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
dfdatetime (20200501-1) unstable; urgency=low
dfdatetime (20200601-1) unstable; urgency=low

* Auto-generated

-- Log2Timeline maintainers <[email protected]> Fri, 01 May 2020 13:44:09 +0200
-- Log2Timeline maintainers <[email protected]> Mon, 01 Jun 2020 07:47:13 +0200
4 changes: 2 additions & 2 deletions config/dpkg/control
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ Source: dfdatetime
Section: python
Priority: extra
Maintainer: Log2Timeline maintainers <[email protected]>
Build-Depends: debhelper (>= 9), dh-python, python3-all (>= 3.5~), python3-setuptools
Build-Depends: debhelper (>= 9), dh-python, python3-all (>= 3.6~), python3-setuptools
Standards-Version: 4.1.4
X-Python3-Version: >= 3.5
X-Python3-Version: >= 3.6
Homepage: https://github.com/log2timeline/dfdatetime

Package: python3-dfdatetime
Expand Down
8 changes: 4 additions & 4 deletions config/travis/install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ then
else
RPM_PACKAGES="";

if test ${TARGET} = "pylint";
if test ${TARGET} = "lint_and_type_check";
then
RPM_PACKAGES="${RPM_PACKAGES} findutils pylint";
RPM_PACKAGES="${RPM_PACKAGES} findutils pylint python3-mypy";
fi
RPM_PACKAGES="${RPM_PACKAGES} python3 ${RPM_PYTHON3_DEPENDENCIES} ${RPM_PYTHON3_TEST_DEPENDENCIES}";
fi
Expand Down Expand Up @@ -87,9 +87,9 @@ then
then
DPKG_PACKAGES="${DPKG_PACKAGES} sudo";

elif test ${TARGET} = "pylint";
elif test ${TARGET} = "lint_and_type_check";
then
DPKG_PACKAGES="${DPKG_PACKAGES} python3-distutils pylint";
DPKG_PACKAGES="${DPKG_PACKAGES} mypy pylint python3-distutils";
fi
if test "${TARGET}" != "jenkins3";
then
Expand Down
24 changes: 21 additions & 3 deletions config/travis/run_pylint.sh → config/travis/run_checks.sh
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
#!/bin/bash
#
# Script to run pylint on Travis-CI.
# Script to run lint and type checks on Travis-CI.
#
# This file is generated by l2tdevtools update-dependencies.py, any dependency
# related changes should be made in dependencies.ini.

# Exit on error.
set -e;
EXIT_SUCCESS=0;
EXIT_FAILURE=1;

RESULT=${EXIT_SUCCESS};

pylint --version

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

pylint --rcfile=.pylintrc ${FILE};

if test $? -ne ${EXIT_SUCCESS};
then
RESULT=${EXIT_FAILURE};
fi
done

mypy --version

mypy --strict dfdatetime;

if test $? -ne ${EXIT_SUCCESS};
then
RESULT=${EXIT_FAILURE};
fi

exit ${RESULT};
8 changes: 4 additions & 4 deletions config/travis/runtests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ then
then
TEST_COMMAND="tox -e ${TOXENV}";

elif test "${TARGET}" = "pylint";
elif test "${TARGET}" = "lint_and_type_check";
then
TEST_COMMAND="./config/travis/run_pylint.sh";
TEST_COMMAND="./config/travis/run_checks.sh";
else
TEST_COMMAND="./config/travis/run_python3.sh";
fi
Expand Down Expand Up @@ -51,9 +51,9 @@ then
then
TEST_COMMAND="./config/jenkins/linux/run_end_to_end_tests_py3.sh travis";

elif test "${TARGET}" = "pylint";
elif test "${TARGET}" = "lint_and_type_check";
then
TEST_COMMAND="./config/travis/run_pylint.sh";
TEST_COMMAND="./config/travis/run_checks.sh";
else
TEST_COMMAND="./config/travis/run_python3.sh";
fi
Expand Down
2 changes: 1 addition & 1 deletion dfdatetime/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@
objects to preserve accuracy and precision.
"""

__version__ = '20200501'
__version__ = '20200601'
14 changes: 8 additions & 6 deletions dfdatetime/apfs_time.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

import decimal

from typing import Union # pylint: disable=unused-import

from dfdatetime import definitions
from dfdatetime import posix_time

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

def _GetNormalizedTimestamp(self):
def _GetNormalizedTimestamp(self) -> 'Union[decimal.Decimal, None]':
"""Retrieves the normalized timestamp.

Returns:
decimal.Decimal: normalized timestamp, which contains the number of
seconds since January 1, 1970 00:00:00 and a fraction of second used
for increased precision, or None if the normalized timestamp cannot be
determined.
seconds since January 1, 1970 00:00:00 and a fraction of second
used for increased precision, or None if the normalized timestamp
cannot be determined.
"""
if self._normalized_timestamp is None:
if (self._timestamp is not None and self._timestamp >= self._INT64_MIN and
Expand All @@ -37,7 +39,7 @@ def _GetNormalizedTimestamp(self):

return self._normalized_timestamp

def CopyFromDateTimeString(self, time_string):
def CopyFromDateTimeString(self, time_string: 'str') -> 'None':
"""Copies a APFS timestamp from a date and time string.

Args:
Expand All @@ -58,7 +60,7 @@ def CopyFromDateTimeString(self, time_string):
self._timestamp > self._INT64_MAX):
raise ValueError('Date time value not supported.')

def CopyToDateTimeString(self):
def CopyToDateTimeString(self) -> 'Union[str, None]':
"""Copies the APFS timestamp to a date and time string.

Returns:
Expand Down
38 changes: 21 additions & 17 deletions dfdatetime/cocoa_time.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,16 @@

import decimal

from typing import Optional, Union # pylint: disable=unused-import

from dfdatetime import definitions
from dfdatetime import interface


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

def __init__(self):
def __init__(self) -> 'None':
"""Initializes a Cocoa time epoch."""
super(CocoaTimeEpoch, self).__init__(2001, 1, 1)

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

_EPOCH = CocoaTimeEpoch()
_EPOCH: 'interface.DateTimeEpoch' = CocoaTimeEpoch()

def __init__(self, timestamp=None):
def __init__(self, timestamp: 'Optional[float]' = None) -> 'None':
"""Initializes a Cocoa timestamp.

Args:
timestamp (Optional[float]): Cocoa timestamp.
"""
super(CocoaTime, self).__init__()
self._precision = definitions.PRECISION_1_SECOND
self._timestamp = timestamp
self._precision: 'str' = definitions.PRECISION_1_SECOND
self._timestamp: 'Union[float, None]' = timestamp

@property
def timestamp(self):
def timestamp(self) -> 'Union[float, None]':
"""float: Cocoa timestamp or None if timestamp is not set."""
return self._timestamp

def _GetNormalizedTimestamp(self):
def _GetNormalizedTimestamp(self) -> 'Union[decimal.Decimal, None]':
"""Retrieves the normalized timestamp.

Returns:
float: normalized timestamp, which contains the number of seconds since
January 1, 1970 00:00:00 and a fraction of second used for increased
precision, or None if the normalized timestamp cannot be determined.
decimal.Decimal: normalized timestamp, which contains the number of
seconds since January 1, 1970 00:00:00 and a fraction of second
used for increased precision, or None if the normalized timestamp
cannot be determined.
"""
if self._normalized_timestamp is None:
if self._timestamp is not None:
Expand All @@ -66,7 +69,7 @@ def _GetNormalizedTimestamp(self):

return self._normalized_timestamp

def CopyFromDateTimeString(self, time_string):
def CopyFromDateTimeString(self, time_string: 'str') -> 'None':
"""Copies a Cocoa timestamp from a date and time string.

Args:
Expand All @@ -92,19 +95,20 @@ def CopyFromDateTimeString(self, time_string):
microseconds = date_time_values.get('microseconds', None)
time_zone_offset = date_time_values.get('time_zone_offset', 0)

timestamp = self._GetNumberOfSecondsFromElements(
year, month, day_of_month, hours, minutes, seconds, time_zone_offset)
timestamp += self._COCOA_TO_POSIX_BASE
number_of_seconds = self._GetNumberOfSecondsFromElements(
year, month, day_of_month, hours, minutes, seconds,
time_zone_offset=time_zone_offset)
number_of_seconds += self._COCOA_TO_POSIX_BASE

timestamp = float(timestamp)
timestamp = float(number_of_seconds)
if microseconds is not None:
timestamp += float(microseconds) / definitions.MICROSECONDS_PER_SECOND

self._normalized_timestamp = None
self._timestamp = timestamp
self._time_zone_offset = time_zone_offset

def CopyToDateTimeString(self):
def CopyToDateTimeString(self) -> 'Union[str, None]':
"""Copies the Cocoa timestamp to a date and time string.

Returns:
Expand Down
12 changes: 10 additions & 2 deletions dfdatetime/decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,20 @@
from __future__ import unicode_literals

import warnings
import typing

from typing import Any, Callable # pylint: disable=unused-import

def deprecated(function): # pylint: disable=invalid-name

# pylint: disable=invalid-name
RETURN_TYPE = typing.TypeVar('RETURN_TYPE')


def deprecated(
function: 'Callable[..., RETURN_TYPE]') -> 'Callable[..., RETURN_TYPE]':
"""Decorator to mark functions or methods as deprecated."""

def IssueDeprecationWarning(*args, **kwargs):
def IssueDeprecationWarning(*args: 'Any', **kwargs: 'Any') -> 'RETURN_TYPE':
"""Issue a deprecation warning."""
warnings.simplefilter('default', DeprecationWarning)
warnings.warn('Call to deprecated function: {0:s}.'.format(
Expand Down
Loading