Skip to content

Commit fe8fa22

Browse files
author
MarcoFalke
committed
Merge #12987: tests/tools: Enable additional Python flake8 rules for automatic linting via Travis
643aad1 Enable additional flake8 rules (practicalswift) f020aca Minor Python cleanups to make flake8 pass with the new rules enabled (practicalswift) Pull request description: Enabled rules: ``` * E242: tab after ',' * E266: too many leading '#' for block comment * E401: multiple imports on one line * E402: module level import not at top of file * E701: multiple statements on one line (colon) * E901: SyntaxError: invalid syntax * E902: TokenError: EOF in multi-line string * F821: undefined name 'Foo' * W293: blank line contains whitespace * W606: 'async' and 'await' are reserved keywords starting with Python 3.7 ``` Note to reviewers: * In general we don't allow whitespace cleanups to existing code, but in order to allow for enabling Travis checking for these rules a few smaller whitespace cleanups had to made as part of this PR. * Use [this `?w=1` link](https://github.com/bitcoin/bitcoin/pull/12987/files?w=1) to show a diff without whitespace changes. Before this commit: ``` $ flake8 -qq --statistics --ignore=B,C,E,F,I,N,W --select=E112,E113,E115,E116,E125,E131,E133,E223,E224,E242,E266,E271,E272,E273,E274,E275,E304,E306,E401,E402,E502,E701,E702,E703,E714,E721,E741,E742,E743,F401,E901,E902,F402,F404,F406,F407,F601,F602,F621,F622,F631,F701,F702,F703,F704,F705,F706,F707,F811,F812,F821,F822,F823,F831,F841,W292,W293,W504,W601,W602,W603,W604,W605,W606 . 5 E266 too many leading '#' for block comment 4 E401 multiple imports on one line 6 E402 module level import not at top of file 5 E701 multiple statements on one line (colon) 1 F812 list comprehension redefines 'n' from line 159 4 F821 undefined name 'ConnectionRefusedError' 28 W293 blank line contains whitespace ``` After this commit: ``` $ flake8 -qq --statistics --ignore=B,C,E,F,I,N,W --select=E112,E113,E115,E116,E125,E131,E133,E223,E224,E242,E266,E271,E272,E273,E274,E275,E304,E306,E401,E402,E502,E701,E702,E703,E714,E721,E741,E742,E743,F401,E901,E902,F402,F404,F406,F407,F601,F602,F621,F622,F631,F701,F702,F703,F704,F705,F706,F707,F811,F812,F821,F822,F823,F831,F841,W292,W293,W504,W601,W602,W603,W604,W605,W606 . $ ``` Tree-SHA512: fc7d5e752298a50d4248afc620ee2c173135b4ca008e48e02913ac968e5a24a5fd5396926047ec62f1d580d537434ccae01f249bb2f3338fa59dc630bf97ca7a
2 parents 0d69921 + 643aad1 commit fe8fa22

22 files changed

+69
-51
lines changed

contrib/devtools/copyright_header.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ def exec_report(base_directory, verbose):
286286
def report_cmd(argv):
287287
if len(argv) == 2:
288288
sys.exit(REPORT_USAGE)
289-
289+
290290
base_directory = argv[2]
291291
if not os.path.exists(base_directory):
292292
sys.exit("*** bad <base_directory>: %s" % base_directory)
@@ -444,7 +444,7 @@ def print_file_action_message(filename, action):
444444
def update_cmd(argv):
445445
if len(argv) != 3:
446446
sys.exit(UPDATE_USAGE)
447-
447+
448448
base_directory = argv[2]
449449
if not os.path.exists(base_directory):
450450
sys.exit("*** bad base_directory: %s" % base_directory)
@@ -570,13 +570,13 @@ def insert_cmd(argv):
570570
_, extension = os.path.splitext(filename)
571571
if extension not in ['.h', '.cpp', '.cc', '.c', '.py']:
572572
sys.exit("*** cannot insert for file extension %s" % extension)
573-
573+
574574
if extension == '.py':
575575
style = 'python'
576576
else:
577577
style = 'cpp'
578578
exec_insert_header(filename, style)
579-
579+
580580
################################################################################
581581
# UI
582582
################################################################################

contrib/devtools/github-merge.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@
2121
import hashlib
2222
import subprocess
2323
import sys
24-
import json,codecs
24+
import json
25+
import codecs
2526
try:
2627
from urllib.request import Request,urlopen
2728
except:

contrib/devtools/lint-python.sh

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,21 +15,28 @@
1515
# E133 closing bracket is missing indentation
1616
# E223 tab before operator
1717
# E224 tab after operator
18+
# E242 tab after ','
19+
# E266 too many leading '#' for block comment
1820
# E271 multiple spaces after keyword
1921
# E272 multiple spaces before keyword
2022
# E273 tab after keyword
2123
# E274 tab before keyword
2224
# E275 missing whitespace after keyword
2325
# E304 blank lines found after function decorator
2426
# E306 expected 1 blank line before a nested definition
27+
# E401 multiple imports on one line
28+
# E402 module level import not at top of file
2529
# E502 the backslash is redundant between brackets
30+
# E701 multiple statements on one line (colon)
2631
# E702 multiple statements on one line (semicolon)
2732
# E703 statement ends with a semicolon
2833
# E714 test for object identity should be "is not"
2934
# E721 do not compare types, use "isinstance()"
3035
# E741 do not use variables named "l", "O", or "I"
3136
# E742 do not define classes named "l", "O", or "I"
3237
# E743 do not define functions named "l", "O", or "I"
38+
# E901 SyntaxError: invalid syntax
39+
# E902 TokenError: EOF in multi-line string
3340
# F401 module imported but unused
3441
# F402 import module from line N shadowed by loop variable
3542
# F404 future import(s) name after other statements
@@ -49,16 +56,19 @@
4956
# F707 an except: block as not the last exception handler
5057
# F811 redefinition of unused name from line N
5158
# F812 list comprehension redefines 'foo' from line N
59+
# F821 undefined name 'Foo'
5260
# F822 undefined name name in __all__
5361
# F823 local variable name … referenced before assignment
5462
# F831 duplicate argument name in function definition
5563
# F841 local variable 'foo' is assigned to but never used
5664
# W292 no newline at end of file
65+
# W293 blank line contains whitespace
5766
# W504 line break after binary operator
5867
# W601 .has_key() is deprecated, use "in"
5968
# W602 deprecated form of raising exception
6069
# W603 "<>" is deprecated, use "!="
6170
# W604 backticks are deprecated, use "repr()"
6271
# W605 invalid escape sequence "x"
72+
# W606 'async' and 'await' are reserved keywords starting with Python 3.7
6373

64-
flake8 --ignore=B,C,E,F,I,N,W --select=E112,E113,E115,E116,E125,E131,E133,E223,E224,E271,E272,E273,E274,E275,E304,E306,E502,E702,E703,E714,E721,E741,E742,E743,F401,F402,F404,F406,F407,F601,F602,F621,F622,F631,F701,F702,F703,F704,F705,F706,F707,F811,F812,F822,F823,F831,F841,W292,W504,W601,W602,W603,W604,W605 .
74+
flake8 --ignore=B,C,E,F,I,N,W --select=E112,E113,E115,E116,E125,E131,E133,E223,E224,E242,E266,E271,E272,E273,E274,E275,E304,E306,E401,E402,E502,E701,E702,E703,E714,E721,E741,E742,E743,F401,E901,E902,F402,F404,F406,F407,F601,F602,F621,F622,F631,F701,F702,F703,F704,F705,F706,F707,F811,F812,F821,F822,F823,F831,F841,W292,W293,W504,W601,W602,W603,W604,W605,W606 .

contrib/devtools/optimize-pngs.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,19 +41,19 @@ def content_hash(filename):
4141
file_path = os.path.join(absFolder, file)
4242
fileMetaMap = {'file' : file, 'osize': os.path.getsize(file_path), 'sha256Old' : file_hash(file_path)}
4343
fileMetaMap['contentHashPre'] = content_hash(file_path)
44-
44+
4545
try:
4646
subprocess.call([pngcrush, "-brute", "-ow", "-rem", "gAMA", "-rem", "cHRM", "-rem", "iCCP", "-rem", "sRGB", "-rem", "alla", "-rem", "text", file_path],
4747
stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
4848
except:
4949
print("pngcrush is not installed, aborting...")
5050
sys.exit(0)
51-
51+
5252
#verify
5353
if "Not a PNG file" in subprocess.check_output([pngcrush, "-n", "-v", file_path], stderr=subprocess.STDOUT, universal_newlines=True):
5454
print("PNG file "+file+" is corrupted after crushing, check out pngcursh version")
5555
sys.exit(1)
56-
56+
5757
fileMetaMap['sha256New'] = file_hash(file_path)
5858
fileMetaMap['contentHashPost'] = content_hash(file_path)
5959

@@ -72,5 +72,5 @@ def content_hash(filename):
7272
totalSaveBytes += fileDict['osize'] - fileDict['psize']
7373
noHashChange = noHashChange and (oldHash == newHash)
7474
print(fileDict['file']+"\n size diff from: "+str(fileDict['osize'])+" to: "+str(fileDict['psize'])+"\n old sha256: "+oldHash+"\n new sha256: "+newHash+"\n")
75-
75+
7676
print("completed. Checksum stable: "+str(noHashChange)+". Total reduction: "+str(totalSaveBytes)+" bytes")

contrib/linearize/linearize-data.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121

2222
settings = {}
2323

24-
##### Switch endian-ness #####
2524
def hex_switchEndian(s):
2625
""" Switches the endianness of a hex string (in pairs of hex chars) """
2726
pairList = [s[i:i+2].encode() for i in range(0, len(s), 2)]

contrib/linearize/linearize-hashes.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121

2222
settings = {}
2323

24-
##### Switch endian-ness #####
2524
def hex_switchEndian(s):
2625
""" Switches the endianness of a hex string (in pairs of hex chars) """
2726
pairList = [s[i:i+2].encode() for i in range(0, len(s), 2)]

contrib/seeds/generate-seeds.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@
3434

3535
from base64 import b32decode
3636
from binascii import a2b_hex
37-
import sys, os
37+
import sys
38+
import os
3839
import re
3940

4041
# ipv4 in ipv6 prefix
@@ -46,7 +47,7 @@ def name_to_ipv6(addr):
4647
if len(addr)>6 and addr.endswith('.onion'):
4748
vchAddr = b32decode(addr[0:-6], True)
4849
if len(vchAddr) != 16-len(pchOnionCat):
49-
raise ValueError('Invalid onion %s' % s)
50+
raise ValueError('Invalid onion %s' % vchAddr)
5051
return pchOnionCat + vchAddr
5152
elif '.' in addr: # IPv4
5253
return pchIPv4 + bytearray((int(x) for x in addr.split('.')))
@@ -132,7 +133,7 @@ def main():
132133
with open(os.path.join(indir,'nodes_test.txt'),'r') as f:
133134
process_nodes(g, f, 'pnSeed6_test', 18333)
134135
g.write('#endif // BITCOIN_CHAINPARAMSSEEDS_H\n')
135-
136+
136137
if __name__ == '__main__':
137138
main()
138139

contrib/seeds/makeseeds.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@
66
# Generate seeds.txt from Pieter's DNS seeder
77
#
88

9+
import re
10+
import sys
11+
import dns.resolver
12+
import collections
13+
914
NSEEDS=512
1015

1116
MAX_SEEDS_PER_ASN=2
@@ -22,11 +27,6 @@
2227
"54.94.195.96", "54.94.200.247"
2328
}
2429

25-
import re
26-
import sys
27-
import dns.resolver
28-
import collections
29-
3030
PATTERN_IPV4 = re.compile(r"^((\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})):(\d+)$")
3131
PATTERN_IPV6 = re.compile(r"^\[([0-9a-z:]+)\]:(\d+)$")
3232
PATTERN_ONION = re.compile(r"^([abcdefghijklmnopqrstuvwxyz234567]{16}\.onion):(\d+)$")

contrib/testgen/base58.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,10 @@ def b58encode(v):
4343
# leading 0-bytes in the input become leading-1s
4444
nPad = 0
4545
for c in v:
46-
if c == 0: nPad += 1
47-
else: break
46+
if c == 0:
47+
nPad += 1
48+
else:
49+
break
4850

4951
return (__b58chars[0]*nPad) + result
5052

@@ -98,7 +100,8 @@ def b58decode_chk(v):
98100
def get_bcaddress_version(strAddress):
99101
""" Returns None if strAddress is invalid. Otherwise returns integer version of address. """
100102
addr = b58decode_chk(strAddress)
101-
if addr is None or len(addr)!=21: return None
103+
if addr is None or len(addr)!=21:
104+
return None
102105
version = addr[0]
103106
return ord(version)
104107

contrib/testgen/gen_base58_test_vectors.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,12 +74,12 @@ def gen_invalid_vector(template, corrupt_prefix, randomize_payload_size, corrupt
7474
prefix = os.urandom(1)
7575
else:
7676
prefix = bytearray(template[0])
77-
77+
7878
if randomize_payload_size:
7979
payload = os.urandom(max(int(random.expovariate(0.5)), 50))
8080
else:
8181
payload = os.urandom(template[1])
82-
82+
8383
if corrupt_suffix:
8484
suffix = os.urandom(len(template[2]))
8585
else:
@@ -114,7 +114,8 @@ def gen_invalid_vectors():
114114
yield val,
115115

116116
if __name__ == '__main__':
117-
import sys, json
117+
import sys
118+
import json
118119
iters = {'valid':gen_valid_vectors, 'invalid':gen_invalid_vectors}
119120
try:
120121
uiter = iters[sys.argv[1]]
@@ -124,7 +125,7 @@ def gen_invalid_vectors():
124125
count = int(sys.argv[2])
125126
except IndexError:
126127
count = 0
127-
128+
128129
data = list(islice(uiter(), count))
129130
json.dump(data, sys.stdout, sort_keys=True, indent=4)
130131
sys.stdout.write('\n')

0 commit comments

Comments
 (0)