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

Add more flake8 tests and fail build if any test fails #4055

Merged
merged 2 commits into from
Aug 18, 2019
Merged
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
4 changes: 3 additions & 1 deletion bin/flake8_tests.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
#!/bin/sh

set -o errexit # fail the build if any task fails

flake8 --version ; pip --version
# stop the build if there are Python syntax errors or undefined names
flake8 . --count --select=E901,E999,F821,F822,F823 --show-source --statistics
flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics
# exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide
flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics
2 changes: 1 addition & 1 deletion redash/authentication/saml_auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ def sp_initiated(org_slug=None):
redirect_url = None
# Select the IdP URL to send the AuthN request to
for key, value in info['headers']:
if key is 'Location':
if key == 'Location':
redirect_url = value
response = redirect(redirect_url, code=302)

Expand Down
12 changes: 7 additions & 5 deletions redash/models/parameterized_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@
from funcy import distinct
from dateutil.parser import parse

from six import string_types, text_type


def _pluck_name_and_value(default_column, row):
row = {k.lower(): v for k, v in row.items()}
name_column = "name" if "name" in row.keys() else default_column.lower()
value_column = "value" if "value" in row.keys() else default_column.lower()

return {"name": row[name_column], "value": unicode(row[value_column])}
return {"name": row[name_column], "value": text_type(row[value_column])}


def _load_result(query_id, org):
Expand Down Expand Up @@ -107,8 +109,8 @@ def _is_date_range(obj):

def _is_value_within_options(value, dropdown_options, allow_list=False):
if isinstance(value, list):
return allow_list and set(map(unicode, value)).issubset(set(dropdown_options))
return unicode(value) in dropdown_options
return allow_list and set(map(text_type, value)).issubset(set(dropdown_options))
return text_type(value) in dropdown_options


class ParameterizedQuery(object):
Expand Down Expand Up @@ -142,11 +144,11 @@ def _valid(self, name, value):
query_id = definition.get('queryId')
allow_multiple_values = isinstance(definition.get('multiValuesOptions'), dict)

if isinstance(enum_options, basestring):
if isinstance(enum_options, string_types):
enum_options = enum_options.split('\n')

validators = {
"text": lambda value: isinstance(value, basestring),
"text": lambda value: isinstance(value, string_types),
"number": _is_number,
"enum": lambda value: _is_value_within_options(value,
enum_options,
Expand Down
4 changes: 3 additions & 1 deletion redash/query_runner/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
from dateutil import parser
import requests

from six import text_type

from redash import settings
from redash.utils import json_loads

Expand Down Expand Up @@ -299,7 +301,7 @@ def guess_type_from_string(string_value):
except (ValueError, OverflowError):
pass

if unicode(string_value).lower() in ('true', 'false'):
if text_type(string_value).lower() in ('true', 'false'):
return TYPE_BOOLEAN

try:
Expand Down
4 changes: 3 additions & 1 deletion redash/query_runner/couchbase.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@
import logging

from dateutil.parser import parse
from six import text_type

from redash.query_runner import *
from redash.utils import JSONEncoder, json_dumps, json_loads, parse_human_time
from redash.utils.compat import long
import json

logger = logging.getLogger(__name__)
Expand All @@ -17,7 +19,7 @@

TYPES_MAP = {
str: TYPE_STRING,
unicode: TYPE_STRING,
text_type: TYPE_STRING,
Copy link
Member

@jezdez jezdez Jan 10, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@arikfr @cclauss I think this and other cases of the TYPES_MAP are incorrect, since we're now on Python 3 and str is Unicode, while text_type is as well. While we were on Python 2 that wasn't the case. I'll send a PR.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

https://pypi.org/project/pyupgrade is pretty useful for finding and fixing these things.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

int: TYPE_INTEGER,
long: TYPE_INTEGER,
float: TYPE_FLOAT,
Expand Down
6 changes: 4 additions & 2 deletions redash/query_runner/drill.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

from dateutil import parser

from six import text_type

from redash.query_runner import (
BaseHTTPQueryRunner, register,
TYPE_DATETIME, TYPE_INTEGER, TYPE_FLOAT, TYPE_BOOLEAN,
Expand All @@ -26,12 +28,12 @@ def convert_type(string_value, actual_type):
return float(string_value)

if actual_type == TYPE_BOOLEAN:
return unicode(string_value).lower() == 'true'
return text_type(string_value).lower() == 'true'

if actual_type == TYPE_DATETIME:
return parser.parse(string_value)

return unicode(string_value)
return text_type(string_value)


# Parse Drill API response and translate it to accepted format
Expand Down
6 changes: 4 additions & 2 deletions redash/query_runner/elasticsearch.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@

import requests
from requests.auth import HTTPBasicAuth
from six import string_types, text_type

from redash.query_runner import *
from redash.utils import json_dumps, json_loads
from redash.utils.compat import long

try:
import http.client as http_client
Expand Down Expand Up @@ -35,7 +37,7 @@

PYTHON_TYPES_MAPPING = {
str: TYPE_STRING,
unicode: TYPE_STRING,
text_type: TYPE_STRING,
jezdez marked this conversation as resolved.
Show resolved Hide resolved
bool: TYPE_BOOLEAN,
int: TYPE_INTEGER,
long: TYPE_INTEGER,
Expand Down Expand Up @@ -345,7 +347,7 @@ def run_query(self, query, user):

result_columns = []
result_rows = []
if isinstance(query_data, str) or isinstance(query_data, unicode):
if isinstance(query_data, string_types):
_from = 0
while True:
query_size = size if limit >= (_from + size) else (limit - _from)
Expand Down
8 changes: 5 additions & 3 deletions redash/query_runner/json_ds.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
import datetime
from urlparse import urlparse
from funcy import compact, project
from six import text_type
from redash.utils import json_dumps
from redash.utils.compat import long
from redash.query_runner import (BaseHTTPQueryRunner, register,
TYPE_BOOLEAN, TYPE_DATETIME, TYPE_FLOAT,
TYPE_INTEGER, TYPE_STRING)
Expand All @@ -25,19 +27,19 @@ def parse_query(query):
return params
except ValueError as e:
logging.exception(e)
error = unicode(e)
error = text_type(e)
raise QueryParseError(error)


def is_private_address(url):
hostname = urlparse(url).hostname
ip_address = socket.gethostbyname(hostname)
return ipaddress.ip_address(unicode(ip_address)).is_private
return ipaddress.ip_address(text_type(ip_address)).is_private


TYPES_MAP = {
str: TYPE_STRING,
unicode: TYPE_STRING,
text_type: TYPE_STRING,
int: TYPE_INTEGER,
long: TYPE_INTEGER,
float: TYPE_FLOAT,
Expand Down
6 changes: 4 additions & 2 deletions redash/query_runner/mongodb.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
import re

from dateutil.parser import parse
from six import string_types, text_type

from redash.query_runner import *
from redash.utils import JSONEncoder, json_dumps, json_loads, parse_human_time
from redash.utils.compat import long

logger = logging.getLogger(__name__)

Expand All @@ -24,7 +26,7 @@

TYPES_MAP = {
str: TYPE_STRING,
unicode: TYPE_STRING,
text_type: TYPE_STRING,
int: TYPE_INTEGER,
long: TYPE_INTEGER,
float: TYPE_FLOAT,
Expand Down Expand Up @@ -56,7 +58,7 @@ def parse_oids(oids):

def datetime_parser(dct):
for k, v in dct.iteritems():
if isinstance(v, basestring):
if isinstance(v, string_types):
m = date_regex.findall(v)
if len(m) > 0:
dct[k] = parse(m[0], yearfirst=True)
Expand Down
5 changes: 5 additions & 0 deletions redash/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@

from .human_time import parse_human_time

try:
buffer
except NameError:
buffer = bytes

COMMENTS_REGEX = re.compile("/\*.*?\*/")
WRITER_ENCODING = os.environ.get('REDASH_CSV_WRITER_ENCODING', 'utf-8')
WRITER_ERRORS = os.environ.get('REDASH_CSV_WRITER_ERRORS', 'strict')
Expand Down
4 changes: 4 additions & 0 deletions redash/utils/compat.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
try:
long = long
except NameError:
long = int
4 changes: 2 additions & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ pytz==2016.7
PyYAML==3.12
redis==3.2.1
requests==2.21.0
six==1.11.0
six==1.12.0
SQLAlchemy==1.2.12
# We can't upgrade SQLAlchemy-Searchable version as newer versions require PostgreSQL > 9.6, but we target older versions at the moment.
SQLAlchemy-Searchable==0.10.6
Expand Down Expand Up @@ -55,7 +55,7 @@ user-agents==1.1.0
python-geoip-geolite2==2015.303
chromelogger==0.4.3
pypd==1.1.0
disposable-email-domains
disposable-email-domains>=0.0.52
gevent==1.4.0
# Install the dependencies of the bin/bundle-extensions script here.
# It has its own requirements file to simplify the frontend client build process
Expand Down
5 changes: 5 additions & 0 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@
from redash.utils import (build_url, collect_parameters_from_request,
filter_none, json_dumps, generate_token)

try:
buffer
except NameError:
buffer = bytes

DummyRequest = namedtuple('DummyRequest', ['host', 'scheme'])


Expand Down