[mypy] Adding mypy linting (#7053)

This commit is contained in:
John Bodley 2019-03-24 20:35:48 -07:00 committed by Maxime Beauchemin
parent 60a7b6df59
commit 80d6f5a090
6 changed files with 49 additions and 15 deletions

View File

@ -74,6 +74,7 @@ jobs:
python: 3.6 python: 3.6
env: TOXENV=pylint env: TOXENV=pylint
- language: python - language: python
python: 3.6
env: env:
- TOXENV=license-check - TOXENV=license-check
- TRAVIS_CACHE=$HOME/.travis_cache/ - TRAVIS_CACHE=$HOME/.travis_cache/

View File

@ -462,6 +462,35 @@ Note that the test environment uses a temporary directory for defining the
SQLite databases which will be cleared each time before the group of test SQLite databases which will be cleared each time before the group of test
commands are invoked. commands are invoked.
#### Typing
To ensure clarity, consistency, all readability, _all_ new functions should use
[type hints](https://docs.python.org/3/library/typing.html) and include a
docstring using Sphinx documentation.
Note per [PEP-484](https://www.python.org/dev/peps/pep-0484/#exceptions) no
syntax for listing explicitly raised exceptions is proposed and thus the
recommendation is to put this information in a docstring, i.e.,
```python
import math
from typing import Union
def sqrt(x: Union[float, int]) -> Union[float, int]:
"""
Return the square root of x.
:param x: A number
:returns: The square root of the given number
:raises ValueError: If the number is negative
"""
return math.sqrt(x)
```
### JavaScript Testing ### JavaScript Testing
We use [Jest](https://jestjs.io/) and [Enzyme](https://airbnb.io/enzyme/) to test Javascript. Tests can be run with: We use [Jest](https://jestjs.io/) and [Enzyme](https://airbnb.io/enzyme/) to test Javascript. Tests can be run with:

View File

@ -17,10 +17,12 @@
console_log==0.2.10 console_log==0.2.10
flake8-commas==2.0.0 flake8-commas==2.0.0
flake8-import-order==0.18 flake8-import-order==0.18
flake8-mypy==17.8.0
flake8-quotes==1.0.0 flake8-quotes==1.0.0
flake8==3.6.0 flake8==3.6.0
flask-cors==3.0.6 flask-cors==3.0.6
ipdb==0.11 ipdb==0.11
mypy==0.670
mysqlclient==1.3.13 mysqlclient==1.3.13
pip-tools==3.5.0 pip-tools==3.5.0
psycopg2-binary==2.7.5 psycopg2-binary==2.7.5

View File

@ -54,7 +54,7 @@ class QueryContext:
custom_cache_timeout: int = None, custom_cache_timeout: int = None,
): ):
self.datasource = ConnectorRegistry.get_datasource(datasource.get('type'), self.datasource = ConnectorRegistry.get_datasource(datasource.get('type'),
int(datasource.get('id')), int(datasource.get('id')), # noqa: E501, T400
db.session) db.session)
self.queries = list(map(lambda query_obj: QueryObject(**query_obj), queries)) self.queries = list(map(lambda query_obj: QueryObject(**query_obj), queries))

View File

@ -61,8 +61,10 @@ class QueryObject:
# Temporal solution for backward compatability issue # Temporal solution for backward compatability issue
# due the new format of non-ad-hoc metric. # due the new format of non-ad-hoc metric.
self.metrics = [metric if 'expressionType' in metric else metric['label'] self.metrics = [
for metric in metrics] metric if 'expressionType' in metric else metric['label'] # noqa: T484
for metric in metrics
]
self.row_limit = row_limit self.row_limit = row_limit
self.filter = filters if filters is not None else [] self.filter = filters if filters is not None else []
self.timeseries_limit = timeseries_limit self.timeseries_limit = timeseries_limit

View File

@ -31,7 +31,7 @@ import os
import signal import signal
import smtplib import smtplib
import sys import sys
from typing import Optional from typing import Optional, Tuple
import uuid import uuid
import zlib import zlib
@ -925,7 +925,7 @@ def get_since_until(time_range: Optional[str] = None,
since: Optional[str] = None, since: Optional[str] = None,
until: Optional[str] = None, until: Optional[str] = None,
time_shift: Optional[str] = None, time_shift: Optional[str] = None,
relative_end: Optional[str] = None) -> (datetime, datetime): relative_end: Optional[str] = None) -> Tuple[datetime, datetime]:
"""Return `since` and `until` date time tuple from string representations of """Return `since` and `until` date time tuple from string representations of
time_range, since, until and time_shift. time_range, since, until and time_shift.
@ -953,11 +953,11 @@ def get_since_until(time_range: Optional[str] = None,
separator = ' : ' separator = ' : '
relative_end = parse_human_datetime(relative_end if relative_end else 'today') relative_end = parse_human_datetime(relative_end if relative_end else 'today')
common_time_frames = { common_time_frames = {
'Last day': (relative_end - relativedelta(days=1), relative_end), 'Last day': (relative_end - relativedelta(days=1), relative_end), # noqa: T400
'Last week': (relative_end - relativedelta(weeks=1), relative_end), 'Last week': (relative_end - relativedelta(weeks=1), relative_end), # noqa: T400
'Last month': (relative_end - relativedelta(months=1), relative_end), 'Last month': (relative_end - relativedelta(months=1), relative_end), # noqa: E501, T400
'Last quarter': (relative_end - relativedelta(months=3), relative_end), 'Last quarter': (relative_end - relativedelta(months=3), relative_end), # noqa: E501, T400
'Last year': (relative_end - relativedelta(years=1), relative_end), 'Last year': (relative_end - relativedelta(years=1), relative_end), # noqa: T400
} }
if time_range: if time_range:
@ -974,11 +974,11 @@ def get_since_until(time_range: Optional[str] = None,
else: else:
rel, num, grain = time_range.split() rel, num, grain = time_range.split()
if rel == 'Last': if rel == 'Last':
since = relative_end - relativedelta(**{grain: int(num)}) since = relative_end - relativedelta(**{grain: int(num)}) # noqa: T400
until = relative_end until = relative_end
else: # rel == 'Next' else: # rel == 'Next'
since = relative_end since = relative_end
until = relative_end + relativedelta(**{grain: int(num)}) until = relative_end + relativedelta(**{grain: int(num)}) # noqa: T400
else: else:
since = since or '' since = since or ''
if since: if since:
@ -988,13 +988,13 @@ def get_since_until(time_range: Optional[str] = None,
if time_shift: if time_shift:
time_shift = parse_human_timedelta(time_shift) time_shift = parse_human_timedelta(time_shift)
since = since if since is None else (since - time_shift) since = since if since is None else (since - time_shift) # noqa: T400
until = until if until is None else (until - time_shift) until = until if until is None else (until - time_shift) # noqa: T400
if since and until and since > until: if since and until and since > until:
raise ValueError(_('From date cannot be larger than to date')) raise ValueError(_('From date cannot be larger than to date'))
return since, until return since, until # noqa: T400
def add_ago_to_since(since): def add_ago_to_since(since):