10000 WIP: Add lint stage to CI (#586) · rwky/client_python@3889a5d · GitHub
[go: up one dir, main page]

Skip to content

Commit 3889a5d

Browse files
yzdannyzdann
and
yzdann
authored
WIP: Add lint stage to CI (prometheus#586)
* Add lint and test stage * Add E731 to skip do not assign a lambda expression, use a def in flake8 * Fix indentations * Fix flake8 W504 line break after binary operator * Remove unused imports and reorder with isort * Remove deprecated key sudo * Add os to remove info warn in travis-ci * Add isort to tox and CI * Move flake8 and isort to lint stage with name Signed-off-by: yzdann <22779039+yzdann@users.noreply.github.com> Co-authored-by: yzdann <1543472-yzdann@users.noreply.gitlab.com>
1 parent af6bfd3 commit 3889a5d

16 files changed

+98
-75
lines changed

.travis.yml

Lines changed: 32 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,54 @@
1+
os: linux
12
dist: xenial
2-
sudo: false
33
cache:
44
directories:
55
- $HOME/.cache/pip
66

77
language: python
88

9-
matrix:
9+
jobs:
1010
include:
11-
- python: "2.6"
11+
- stage: lint
12+
name: flake8_lint
13+
script:
14+
- tox -e flake8
15+
- stage: lint
16+
name: isort_lint
17+
script:
18+
- tox -e isort
19+
- stage: test
20+
python: "2.6"
1221
env: TOXENV=py26
1322
dist: trusty
14-
- python: "2.7"
23+
- stage: test
24+
python: "2.7"
1525
env: TOXENV=py27
16-
- python: "2.7"
26+
- stage: test
27+
python: "2.7"
1728
env: TOXENV=py27-nooptionals
18-
- python: "3.4"
29+
- stage: test
30+
python: "3.4"
1931
env: TOXENV=py34
20-
- python: "3.5"
32+
- stage: test
33+
python: "3.5"
2134
env: TOXENV=py35
22-
- python: "3.6"
35+
- stage: test
36+
python: "3.6"
2337
env: TOXENV=py36
24-
- python: "3.7"
38+
- stage: test
39+
python: "3.7"
2540
env: TOXENV=py37
26-
sudo: true
27-
- python: "3.8"
41+
- stage: test
42+
python: "3.8"
2843
env: TOXENV=py38
29-
sudo: true
30-
- python: "3.8"
44+
- stage: test
45+
python: "3.8"
3146
env: TOXENV=py38-nooptionals
32-
sudo: true
33-
- python: "pypy"
47+
- stage: test
48+
python: "pypy"
3449
env: TOXENV=pypy
35-
- python: "pypy3"
50+
- stage: test
51+
python: "pypy3"
3652
env: TOXENV=pypy3
3753

3854
install:

prometheus_client/__init__.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
11
#!/usr/bin/python
22

3-
from . import exposition
4-
from . import gc_collector
5-
from . import metrics
6-
from . import metrics_core
7-
from . import platform_collector
8-
from . import process_collector
9-
from . import registry
3+
from . import (
4+
exposition, gc_collector, metrics, metrics_core, platform_collector,
5+
process_collector, registry,
6+
)
107

118
__all__ = ['Counter', 'Gauge', 'Summary', 'Histogram', 'Info', 'Enum']
129

prometheus_client/core.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22

33
from .metrics import Counter, Enum, Gauge, Histogram, Info, Summary
44
from .metrics_core import (
5-
CounterMetricFamily, GaugeHistogramMetricFamily, GaugeMetricFamily, HistogramMetricFamily, InfoMetricFamily,
6-
Metric, StateSetMetricFamily, SummaryMetricFamily, UnknownMetricFamily, UntypedMetricFamily)
5+
CounterMetricFamily, GaugeHistogramMetricFamily, GaugeMetricFamily,
6+
HistogramMetricFamily, InfoMetricFamily, Metric, StateSetMetricFamily,
7+
SummaryMetricFamily, UnknownMetricFamily, UntypedMetricFamily,
8+
)
79
from .registry import CollectorRegistry, REGISTRY
810
from .samples import Exemplar, Sample, Timestamp
911

prometheus_client/exposition.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,25 @@
66
import socket
77
import sys
88
import threading
9-
from wsgiref.simple_server import make_server, WSGIServer, WSGIRequestHandler
9+
from wsgiref.simple_server import make_server, WSGIRequestHandler, WSGIServer
1010

1111
from .openmetrics import exposition as openmetrics
1212
from .registry import REGISTRY
1313
from .utils import floatToGoString
1414

1515
try:
16-
from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer
17-
from SocketServer import ThreadingMixIn
18-
from urllib2 import build_opener, Request, HTTPHandler
1916
from urllib import quote_plus
17+
18+
from BaseHTTPServer import BaseHTTPRequestHandler
19+
from SocketServer import ThreadingMixIn
20+
from urllib2 import build_opener, HTTPHandler, Request
2021
from urlparse import parse_qs, urlparse
2122
except ImportError:
2223
# Python 3
23-
from http.server import BaseHTTPRequestHandler, HTTPServer
24+
from http.server import BaseHTTPRequestHandler
2425
from socketserver import ThreadingMixIn
25-
from urllib.request import build_opener, Request, HTTPHandler
26-
from urllib.parse import quote_plus, parse_qs, urlparse
26+
from urllib.parse import parse_qs, quote_plus, urlparse
27+
from urllib.request import build_opener, HTTPHandler, Request
2728

2829
CONTENT_TYPE_LATEST = str('text/plain; version=0.0.4; charset=utf-8')
2930
"""Content type of the latest text format"""
@@ -362,7 +363,7 @@ def _use_gateway(method, gateway, job, registry, grouping_key, timeout, handler)
362363

363364

364365
def _escape_grouping_key(k, v):
365-
if v == "" :
366+
if v == "":
366367
# Per https://github.com/prometheus/pushgateway/pull/346.
367368
return k + "@base64", "="
368369
elif '/' in v:
@@ -381,6 +382,6 @@ def instance_ip_grouping_key():
381382

382383
try:
383384
# Python >3.5 only
384-
from .asgi import make_asgi_app
385+
from .asgi import make_asgi_app # noqa
385386
except:
386387
pass

prometheus_client/metrics_core.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,12 @@ def add_sample(self, name, labels, value, timestamp=None, exemplar=None):
4242
self.samples.append(Sample(name, labels, value, timestamp, exemplar))
4343

4444
def __eq__(self, other):
45-
return (isinstance(other, Metric) and
46-
self.name == other.name and
47-
self.documentation == other.documentation and
48-
self.type == other.type and
49-
self.unit == other.unit and
50-
self.samples == other.samples)
45+
return (isinstance(other, Metric)
46+
and self.name == other.name
47+
and self.documentation == other.documentation
48+
and self.type == other.type
49+
and self.unit == other.unit
50+
and self.samples == other.samples)
5151

5252
def __repr__(self):
5353
return "Metric(%s, %s, %s, %s, %s)" % (
@@ -218,11 +218,11 @@ def add_metric(self, labels, buckets, sum_value, timestamp=None):
218218
))
219219
# +Inf is last and provides the count value.
220220
self.samples.append(
221-
Sample(self.name + '_count', dict(zip(self._labelnames, labels)), buckets[-1][1], timestamp))
221+
Sample(self.name + '_count', dict(zip(self._labelnames, labels)), buckets[-1][1], timestamp))
222222
# Don't iunclude sum if there's negative buckets.
223223
if float(buckets[0][0]) >= 0 and sum_value is not None:
224224
self.samples.append(
225-
Sample(self.name + '_sum', dict(zip(self._labelnames, labels)), sum_value, timestamp))
225+
Sample(self.name + '_sum', dict(zip(self._labelnames, labels)), sum_value, timestamp))
226226

227227

228228

prometheus_client/mmap_dict.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ def _read_all_values(data, used=0):
3737
if encoded_len + pos > used:
3838
raise RuntimeError('Read beyond file size detected, file is corrupted.')
3939
pos += 4
40-
encoded_key = data[pos : pos + encoded_len]
40+
encoded_key = data[pos:pos + encoded_len]
4141
padded_len = encoded_len + (8 - (encoded_len + 4) % 8)
4242
pos += padded_len
4343
value = _unpack_double(data, pos)[0]

prometheus_client/openmetrics/parser.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
# Python 3
1616
import io as StringIO
1717

18+
1819
def text_string_to_metric_families(text):
1920
"""Parse Openmetrics text format from a unicode string.
2021
@@ -113,8 +114,8 @@ def _parse_timestamp(timestamp):
113114

114115
def _is_character_escaped(s, charpos):
115116
num_bslashes = 0
116-
while (charpos > num_bslashes and
117-
s[charpos - 1 - num_bslashes] == '\\'):
117+
while (charpos > num_bslashes
118+
and s[charpos - 1 - num_bslashes] == '\\'):
118119
num_bslashes += 1
119120
return num_bslashes % 2 == 1
120121

@@ -560,7 +561,7 @@ def build_metric(name, documentation, typ, unit, samples):
560561
raise ValueError("Invalid le label: " + line)
561562
if (typ == 'summary' and name == sample.name
562563
and (not (0 <= float(sample.labels.get('quantile', -1)) <= 1)
563-
or _isUncanonicalNumber(sample.labels['quantile']))):
564+
or _isUncanonicalNumber(sample.labels['quantile']))):
564565
raise ValueError("Invalid quantile label: " + line)
565566

566567
g = tuple(sorted(_group_for_sample(sample, name, typ).items()))

prometheus_client/parser.py

Lines changed: 3 additions & 3 deletions
< 10000 tr class="diff-line-row">
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ def _replace_escaping(s):
4646

4747
def _is_character_escaped(s, charpos):
4848
num_bslashes = 0
49-
while (charpos > num_bslashes and
50-
s[charpos - 1 - num_bslashes] == '\\'):
49+
while (charpos > num_bslashes
50+
and s[charpos - 1 - num_bslashes] == '\\'):
5151
num_bslashes += 1
5252
return num_bslashes % 2 == 1
5353

@@ -112,7 +112,7 @@ def _parse_value_and_timestamp(s):
112112
if not values:
113113
return float(s), None
114114
value = float(values[0])
115-
timestamp = (float(values[-1])/1000) if len(values) > 1 else None
115+
timestamp = (float(values[-1]) / 1000) if len(values) > 1 else None
116116
return value, timestamp
117117

118118

prometheus_client/twisted/_exposition.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from __future__ import absolute_import, unicode_literals
22

3-
from twisted.web.wsgi import WSGIResource
43
from twisted.internet import reactor
4+
from twisted.web.wsgi import WSGIResource
55

66
from .. import exposition, REGISTRY
77

setup.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
from setuptools import setup
2-
import sys
31
from os import path
2+
import sys
3+
4+
from setuptools import setup
45

56
if sys.version_info >= (2, 7):
67
with open(path.join(path.abspath(path.dirname(__file__)), 'README.md')) as f:
78
long_description = f.read()
8-
else: # Assuming we don't run setup in order to publish under python 2.6
9+
else: # Assuming we don't run setup in order to publish under python 2.6
910
long_description = "NA"
1011

1112

tests/openmetrics/test_parser.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -468,6 +468,7 @@ def test_exemplars_with_hash_in_label_values(self):
468468
@unittest.skipIf(sys.version_info < (3, 3), "Test requires Python 3.3+.")
469469
def test_fallback_to_state_machine_label_parsing(self):
470470
from unittest.mock import patch
471+
471472
from prometheus_client.openmetrics.parser import _parse_sample
472473

473474
parse_sample_function = "prometheus_client.openmetrics.parser._parse_sample"

tests/test_asgi.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import sys
44
from unittest import TestCase
55

6-
from prometheus_client import CollectorRegistry, Counter, generate_latest
6+
from prometheus_client import CollectorRegistry, Counter
77
from prometheus_client.exposition import CONTENT_TYPE_LATEST
88

99
if sys.version_info < (2, 7):
@@ -13,9 +13,11 @@
1313

1414
try:
1515
# Python >3.5 only
16-
from prometheus_client import make_asgi_app
1716
import asyncio
17+
1818
from asgiref.testing import ApplicationCommunicator
19+
20+
from prometheus_client import make_asgi_app
1921
HAVE_ASYNCIO_AND_ASGI = True
2022
except ImportError:
2123
HAVE_ASYNCIO_AND_ASGI = False

tests/test_exposition.py

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,13 @@
77
import pytest
88

99
from prometheus_client import (
10-
CollectorRegistry, CONTENT_TYPE_LATEST, Counter, delete_from_gateway, Enum,
11-
Gauge, generate_latest, Histogram, Info, instance_ip_grouping_key, Metric,
12-
push_to_gateway, pushadd_to_gateway, Summary,
10+
CollectorRegistry, CONTENT_TYPE_LATEST, core, Counter, delete_from_gateway,
11+
Enum, Gauge, generate_latest, Histogram, Info, instance_ip_grouping_key,
12+
Metric, push_to_gateway, pushadd_to_gateway, Summary,
1313
)
14-
from prometheus_client import core
1514
from prometheus_client.core import GaugeHistogramMetricFamily, Timestamp
1615
from prometheus_client.exposition import (
17-
basic_auth_handler, default_handler, MetricsHandler
16+
basic_auth_handler, default_handler, MetricsHandler,
1817
)
1918

2019
if sys.version_info < (2, 7):
@@ -24,12 +23,10 @@
2423
import unittest
2524

2625
try:
27-
from BaseHTTPServer import BaseHTTPRequestHandler
28-
from BaseHTTPServer import HTTPServer
26+
from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer
2927
except ImportError:
3028
# Python 3
31-
from http.server import BaseHTTPRequestHandler
32-
from http.server import HTTPServer
29+
from http.server import BaseHTTPRequestHandler, HTTPServer
3330

3431

3532
class TestGenerateText(unittest.TestCase):

tests/test_twisted.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,13 @@
1010
from unittest import skipUnless
1111

1212
try:
13-
from prometheus_client.twisted import MetricsResource
14-
13+
from twisted.internet import reactor
1514
from twisted.trial.unittest import TestCase
16-
from twisted.web.server import Site
15+
from twisted.web.client import Agent, readBody
1716
from twisted.web.resource import Resource
18-
from twisted.internet import reactor
19-
from twisted.web.client import Agent
20-
from twisted.web.client import readBody
17+
from twisted.web.server import Site
18+
19+
from prometheus_client.twisted import MetricsResource
2120

2221
HAVE_TWISTED = True
2322
except ImportError:

tests/test_wsgi.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
from __future__ import absolute_import, unicode_literals
22

3-
import sys
43
from unittest import TestCase
54
from wsgiref.util import setup_testing_defaults
6-
from prometheus_client import make_wsgi_app
75

8-
from prometheus_client import CollectorRegistry, Counter, generate_latest
6+
from prometheus_client import CollectorRegistry, Counter, make_wsgi_app
97
from prometheus_client.exposition import CONTENT_TYPE_LATEST
108

119

tox.ini

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[tox]
2-
envlist = coverage-clean,py26,py27,py34,py35,py36,py37,py38,pypy,pypy3,{py27,py38}-nooptionals,coverage-report,flake8
2+
envlist = coverage-clean,py26,py27,py34,py35,py36,py37,py38,pypy,pypy3,{py27,py38}-nooptionals,coverage-report,flake8,isort
33

44

55
[base]
@@ -66,6 +66,12 @@ skip_install = true
6666
commands =
6767
flake8 prometheus_client/ tests/ setup.py
6868

69+
[testenv:isort]
70+
deps =
71+
isort==5.5.4
72+
skip_install = true
73+
commands =
74+
isort --check prometheus_client/ tests/ setup.py
6975

7076
[flake8]
7177
ignore =
@@ -80,7 +86,9 @@ ignore =
8086
W291,
8187
W293,
8288
W503,
83-
E129
89+
E129,
90+
E731
91+
8492
import-order-style = google
8593
application-import-names = prometheus_client
8694

0 commit comments

Comments
 (0)
0