diff --git a/pycodestyle.py b/pycodestyle.py index 5b7a39c1..6a587580 100755 --- a/pycodestyle.py +++ b/pycodestyle.py @@ -871,17 +871,15 @@ def whitespace_around_named_parameter_equals(logical_line, tokens): Don't use spaces around the '=' sign when used to indicate a keyword argument or a default parameter value. - Okay: def complex(real, imag=0.0): - Okay: return magic(r=real, i=imag) + Okay: def complex(real, imag=0.0):\n return magic(r=real, i=imag) Okay: boolean(a == b) Okay: boolean(a != b) Okay: boolean(a <= b) Okay: boolean(a >= b) - Okay: def foo(arg: int = 42): - Okay: async def foo(arg: int = 42): + Okay: def foo(arg: int = 42):\n pass + Okay: async def foo(arg: int = 42):\n pass - E251: def complex(real, imag = 0.0): - E251: return magic(r = real, i = imag) + E251: def complex(real, imag = 0.0):\n return magic(r = real, i = imag) """ parens = 0 no_space = False @@ -1148,7 +1146,7 @@ def break_around_binary_operator(logical_line, tokens): Okay: (width == 0 +\n height == 0) Okay: foo(\n -x) - Okay: foo(x\n []) + Okay: foo(x,\n []) Okay: x = '''\n''' + '' Okay: foo(x,\n -y) Okay: foo(x, # comment\n -y) @@ -1192,11 +1190,11 @@ def comparison_to_singleton(logical_line, noqa): Comparisons to singletons like None should always be done with "is" or "is not", never the equality operators. - Okay: if arg is not None: - E711: if arg != None: - E711: if None == arg: - E712: if arg == True: - E712: if False == arg: + Okay: arg is not None + E711: arg != None + E711: None == arg + E712: arg == True + E712: False == arg Also, beware of writing if x when you really mean if x is not None -- e.g. when testing whether a variable or argument that defaults to None was @@ -1248,15 +1246,15 @@ def comparison_type(logical_line, noqa): Do not compare types directly. - Okay: if isinstance(obj, int): - E721: if type(obj) is type(1): + Okay: isinstance(obj, int) + E721: type(obj) is type(1) When checking if an object is a string, keep in mind that it might be a unicode string too! In Python 2.3, str and unicode have a common base class, basestring, so you can do: - Okay: if isinstance(obj, basestring): - Okay: if type(a1) is type(b1): + Okay: isinstance(obj, basestring) + Okay: type(a1) is type(b1) """ match = COMPARE_TYPE_REGEX.search(logical_line) if match and not noqa: @@ -1270,9 +1268,9 @@ def comparison_type(logical_line, noqa): def bare_except(logical_line, noqa): r"""When catching exceptions, mention specific exceptions when possible. - Okay: except Exception: - Okay: except BaseException: - E722: except: + Okay: try:\n pass\nexcept Exception:\n pass + Okay: try:\n pass\nexcept BaseException:\n pass + E722: try:\n pass\nexcept:\n pass """ if noqa: return @@ -1301,10 +1299,10 @@ def ambiguous_identifier(logical_line, tokens): function definitions, 'global' and 'nonlocal' statements, exception handlers, and 'with' statements. - Okay: except AttributeError as o: - Okay: with lock as L: - E741: except AttributeError as O: - E741: with lock as l: + Okay: try:\n pass\nexcept AttributeError as o:\n pass + Okay: with lock as L:\n pass + E741: try:\n pass\nexcept AttributeError as O:\n pass + E741: with lock as l:\n pass E741: global I E741: nonlocal l E742: class I(object): @@ -1340,7 +1338,7 @@ def ambiguous_identifier(logical_line, tokens): def python_3000_has_key(logical_line, noqa): r"""The {}.has_key() method is removed in Python 3: use the 'in' operator. - Okay: if "alph" in d:\n print d["alph"] + Okay: "alph" in d W601: assert d.has_key('alph') """ pos = logical_line.find('.has_key(') @@ -1368,8 +1366,8 @@ def python_3000_not_equal(logical_line): The older syntax is removed in Python 3. - Okay: if a != 'no': - W603: if a <> 'no': + Okay: a != 'no' + W603: a <> 'no' """ pos = logical_line.find('<>') if pos > -1: diff --git a/testsuite/E11.py b/testsuite/E11.py index 4ed10963..6b5ee560 100644 --- a/testsuite/E11.py +++ b/testsuite/E11.py @@ -1,13 +1,15 @@ #: E111 if x > 2: - print x + print(x) #: E111 if True: print #: E112 +# Potential E901 if False: print #: E113 +# Potential E901 print print #: E114 E116 diff --git a/testsuite/E12.py b/testsuite/E12.py index a995c955..f33e0c3a 100644 --- a/testsuite/E12.py +++ b/testsuite/E12.py @@ -1,22 +1,22 @@ #: E121 -print "E121", ( - "dent") +print("E121", ( + "dent")) #: E122 -print "E122", ( -"dent") +print("E122", ( +"dent")) #: E123 my_list = [ 1, 2, 3, 4, 5, 6, ] #: E124 -print "E124", ("visual", +print("E124", ("visual", "indent_two" - ) + )) #: E124 -print "E124", ("visual", +print("E124", ("visual", "indent_five" -) +)) #: E124 a = (123, ) @@ -25,20 +25,20 @@ col < 0 or self.moduleCount <= col): raise Exception("%s,%s - %s" % (row, col, self.moduleCount)) #: E126 -print "E126", ( - "dent") +print("E126", ( + "dent")) #: E126 -print "E126", ( - "dent") +print("E126", ( + "dent")) #: E127 -print "E127", ("over-", - "over-indent") +print("E127", ("over-", + "over-indent")) #: E128 -print "E128", ("visual", - "hanging") +print("E128", ("visual", + "hanging")) #: E128 -print "E128", ("under-", - "under-indent") +print("E128", ("under-", + "under-indent")) #: @@ -63,11 +63,11 @@ 4 + \ 5 + 6 #: E131 -print "hello", ( +print("hello", ( "there", # "john", - "dude") + "dude")) #: E126 part = set_mimetype(( a.get('mime_type', 'text')), @@ -100,11 +100,11 @@ or another_very_long_variable_name: raise Exception() #: E122 -dictionary = [ +dictionary = { "is": { "nested": yes(), }, -] +} #: E122 setup('', scripts=[''], @@ -117,9 +117,9 @@ #: E123 W291 -print "E123", ( +print("E123", ( "bad", "hanging", "close" - ) + )) # #: E123 E123 E123 result = { @@ -358,7 +358,7 @@ def example_issue254(): """.strip().split(): print(foo) #: E122:6:5 E122:7:5 E122:8:1 -print dedent( +print(dedent( ''' mkdir -p ./{build}/ mv ./build/ ./{build}/%(revision)s/ @@ -366,7 +366,7 @@ def example_issue254(): build='build', # more stuff ) -) +)) #: E701:1:8 E122:2:1 E203:4:8 E128:5:1 if True:\ print(True) diff --git a/testsuite/E12not.py b/testsuite/E12not.py index 18c6a646..df581b6a 100644 --- a/testsuite/E12not.py +++ b/testsuite/E12not.py @@ -46,34 +46,34 @@ "indented for visual indent") -print "OK", ("visual", - "indent") +print("OK", ("visual", + "indent")) -print "Okay", ("visual", +print("Okay", ("visual", "indent_three" - ) + )) -print "a-ok", ( +print("a-ok", ( "there", "dude", -) +)) -print "hello", ( +print("hello", ( "there", - "dude") + "dude")) -print "hello", ( +print("hello", ( "there", # "john", - "dude") + "dude")) -print "hello", ( - "there", "dude") +print("hello", ( + "there", "dude")) -print "hello", ( +print("hello", ( "there", "dude", -) +)) # Aligned with opening delimiter foo = long_function_name(var_one, var_two, @@ -189,12 +189,12 @@ def long_function_name( if ((foo.bar("baz") and foo.bar("frop") )): - print "yes" + print("yes") # also ok, but starting to look like LISP if ((foo.bar("baz") and foo.bar("frop"))): - print "yes" + print("yes") if (a == 2 or b == "abc def ghi" @@ -214,12 +214,12 @@ def long_function_name( # -print 'l.{line}\t{pos}\t{name}\t{text}'.format( +print('l.{line}\t{pos}\t{name}\t{text}'.format( line=token[2][0], pos=pos, name=tokenize.tok_name[token[0]], text=repr(token[1]), -) +)) print('%-7d %s per second (%d total)' % ( options.counters[key] / elapsed, key, @@ -532,6 +532,8 @@ def f(): If you would like to see debugging output, try: %s -d5 ''' % sys.argv[0]) + except IndexError: + pass d = { # comment diff --git a/testsuite/E20.py b/testsuite/E20.py index 2f1ecc28..85d98ca6 100644 --- a/testsuite/E20.py +++ b/testsuite/E20.py @@ -37,18 +37,18 @@ #: E203:1:10 if x == 4 : - print x, y + print(x, y) x, y = y, x -#: E203:2:15 E702:2:16 +#: E203:2:16 E702:2:17 if x == 4: - print x, y ; x, y = y, x + print(x, y) ; x, y = y, x #: E203:3:13 if x == 4: - print x, y + print(x, y) x, y = y , x #: Okay if x == 4: - print x, y + print(x, y) x, y = y, x a[b1, :] == a[b1, ...] b = a[:, b1] diff --git a/testsuite/E27.py b/testsuite/E27.py index 888b3a80..32077b1c 100644 --- a/testsuite/E27.py +++ b/testsuite/E27.py @@ -6,6 +6,7 @@ True and False #: E271 if 1: + pass #: E273 True and False #: E273 E274 diff --git a/testsuite/E90.py b/testsuite/E90.py index 2c18e9af..5248962d 100644 --- a/testsuite/E90.py +++ b/testsuite/E90.py @@ -1,14 +1,18 @@ #: E901 +# Potential second E901 } #: E901 +# Potential second E901 = [x #: E901 E101 W191 +# Potential second E901 while True: try: pass except: print 'Whoops' #: E122 E225 E251 E251 +# Potential E901 # Do not crash if code is invalid if msg: diff --git a/testsuite/W19.py b/testsuite/W19.py index afdfb767..c952e3e4 100644 --- a/testsuite/W19.py +++ b/testsuite/W19.py @@ -70,12 +70,12 @@ def long_function_name( if ((foo.bar("baz") and foo.bar("frop") )): - print "yes" + print("yes") #: E101 W191 # also ok, but starting to look like LISP if ((foo.bar("baz") and foo.bar("frop"))): - print "yes" + print("yes") #: E101 W191 if (a == 2 or b == "abc def ghi" @@ -135,8 +135,8 @@ def long_function_name( def test_keys(self): """areas.json - All regions are accounted for.""" expected = set([ - u'Norrbotten', - u'V\xe4sterbotten', + 'Norrbotten', + 'V\xe4sterbotten', ]) #: W191 x = [ diff --git a/testsuite/W60.py b/testsuite/W60.py index 973d22ff..f4b44d39 100644 --- a/testsuite/W60.py +++ b/testsuite/W60.py @@ -1,6 +1,6 @@ #: W601 if a.has_key("b"): - print a + print(a) #: W602 raise DummyError, "Message" #: W602 diff --git a/testsuite/test_shell.py b/testsuite/test_shell.py index a80c8757..b7a7a5ea 100644 --- a/testsuite/test_shell.py +++ b/testsuite/test_shell.py @@ -78,7 +78,7 @@ def test_check_simple(self): self.assertEqual(errcode, 1) self.assertFalse(stderr) self.assertEqual(len(stdout), 17) - for line, num, col in zip(stdout, (3, 6, 9, 12), (3, 6, 1, 5)): + for line, num, col in zip(stdout, (3, 6, 10, 14), (3, 6, 1, 5)): path, x, y, msg = line.split(':') self.assertTrue(path.endswith(E11)) self.assertEqual(x, str(num))