1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
|
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
import re
import tokenize
from hacking import core
RE_OCTAL = re.compile(r"0+([1-9]\d*)")
RE_PRINT = re.compile(r"\bprint(?:$|\s+[^\(])")
@core.skip_on_py3
@core.flake8ext
def hacking_python3x_except_compatible(logical_line, physical_line, noqa):
r"""Check for except statements to be Python 3.x compatible
As of Python 3.x, the construct 'except x,y:' has been removed.
Use 'except x as y:' instead.
Okay: try:\n pass\nexcept Exception:\n pass
Okay: try:\n pass\nexcept (Exception, AttributeError):\n pass
H231: try:\n pass\nexcept AttributeError, e:\n pass
Okay: try:\n pass\nexcept AttributeError, e: # noqa\n pass
"""
if noqa:
return
def is_old_style_except(logical_line):
return (',' in logical_line
and ')' not in logical_line.rpartition(',')[2])
if (logical_line.startswith("except ")
and logical_line.endswith(':')
and is_old_style_except(logical_line)):
yield 0, "H231: Python 3.x incompatible 'except x,y:' construct"
@core.skip_on_py3
@core.flake8ext
def hacking_python3x_octal_literals(logical_line, tokens, noqa):
r"""Check for octal literals in Python 3.x compatible form.
As of Python 3.x, the construct "0755" has been removed.
Use "0o755" instead".
Okay: f(0o755)
Okay: 'f(0755)'
Okay: f(755)
Okay: f(0)
Okay: f(000)
Okay: MiB = 1.0415
H232: f(0755)
Okay: f(0755) # noqa
"""
if noqa:
return
for token_type, text, _, _, _ in tokens:
if token_type == tokenize.NUMBER:
match = RE_OCTAL.match(text)
if match:
yield 0, ("H232: Python 3.x incompatible octal %s should be "
"written as 0o%s " %
(match.group(0)[1:], match.group(1)))
@core.skip_on_py3
@core.flake8ext
def hacking_python3x_print_function(logical_line, physical_line, noqa):
r"""Check that all print occurrences look like print functions.
Check that all occurrences of print look like functions, not
print operator. As of Python 3.x, the print operator has
been removed.
Okay: print(msg)
Okay: print (msg)
Okay: print msg # noqa
Okay: print()
H233: print msg
H233: print >>sys.stderr, "hello"
H233: print msg,
H233: print
"""
if noqa:
return
for match in RE_PRINT.finditer(logical_line):
yield match.start(0), (
"H233: Python 3.x incompatible use of print operator")
@core.flake8ext
def hacking_no_assert_equals(logical_line, tokens, noqa):
r"""assert(Not)Equals() is deprecated, use assert(Not)Equal instead.
Okay: self.assertEqual(0, 0)
Okay: self.assertNotEqual(0, 1)
H234: self.assertEquals(0, 0)
H234: self.assertNotEquals(0, 1)
Okay: self.assertEquals(0, 0) # noqa
Okay: self.assertNotEquals(0, 1) # noqa
"""
if noqa:
return
for token_type, text, start_index, _, _ in tokens:
if token_type == tokenize.NAME:
if text == "assertEquals" or text == "assertNotEquals":
yield (start_index[1],
"H234: %s is deprecated, use %s" % (text, text[:-1]))
@core.flake8ext
def hacking_no_assert_underscore(logical_line, tokens, noqa):
r"""assert_() is deprecated, use assertTrue instead.
Okay: self.assertTrue(foo)
H235: self.assert_(foo)
Okay: self.assert_(foo) # noqa
"""
if noqa:
return
for token_type, text, start_index, _, _ in tokens:
if token_type == tokenize.NAME and text == "assert_":
yield (
start_index[1],
"H235: assert_ is deprecated, use assertTrue")
@core.flake8ext
def hacking_python3x_metaclass(logical_line, physical_line, noqa):
r"""Check for metaclass to be Python 3.x compatible.
Okay: @six.add_metaclass(Meta)\nclass Foo(object):\n pass
Okay: @six.with_metaclass(Meta)\nclass Foo(object):\n pass
Okay: class Foo(object):\n '''docstring\n\n __metaclass__ = Meta\n'''
H236: class Foo(object):\n __metaclass__ = Meta
H236: class Foo(object):\n foo=bar\n __metaclass__ = Meta
H236: class Foo(object):\n '''docstr.'''\n __metaclass__ = Meta
H236: class Foo(object):\n __metaclass__ = \\\n Meta
Okay: class Foo(object):\n __metaclass__ = Meta # noqa
"""
if noqa:
return
split_line = logical_line.split()
if(len(split_line) > 2 and split_line[0] == '__metaclass__' and
split_line[1] == '='):
yield (logical_line.find('__metaclass__'),
"H236: Python 3.x incompatible __metaclass__, "
"use six.add_metaclass()")
# NOTE(guochbo): This is removed module list:
# http://python3porting.com/stdlib.html#removed-modules
removed_modules = [
'audiodev', 'Bastion', 'bsddb185', 'bsddb3',
'Canvas', 'cfmfile', 'cl', 'commands', 'compiler'
'dircache', 'dl', 'exception', 'fpformat',
'htmllib', 'ihooks', 'imageop', 'imputil'
'linuxaudiodev', 'md5', 'mhlib', 'mimetools'
'MimeWriter', 'mimify', 'multifile', 'mutex',
'new', 'popen2', 'posixfile', 'pure', 'rexec'
'rfc822', 'sha', 'sgmllib', 'sre', 'stat'
'stringold', 'sunaudio' 'sv', 'test.testall',
'thread', 'timing', 'toaiff', 'user'
]
@core.flake8ext
def hacking_no_removed_module(logical_line, noqa):
r"""Check for removed modules in Python 3.
Examples:
Okay: from os import path
Okay: from os import path as p
Okay: from os import (path as p)
Okay: import os.path
H237: import thread
Okay: import thread # noqa
H237: import commands
H237: import md5 as std_md5
"""
if noqa:
return
line = core.import_normalize(logical_line.strip())
if line and line.split()[0] == 'import':
module_name = line.split()[1].split('.')[0]
if module_name in removed_modules:
yield 0, ("H237: module %s is "
"removed in Python 3" % module_name)
RE_NEW_STYLE_CLASS = re.compile(r"^class [^(]+\(.+\):")
@core.flake8ext
def hacking_no_old_style_class(logical_line, noqa):
r"""Check for old style classes.
Examples:
Okay: class Foo(object):\n pass
Okay: class Foo(Bar, Baz):\n pass
Okay: class Foo(object, Baz):\n pass
Okay: class Foo(somefunc()):\n pass
H238: class Bar:\n pass
H238: class Bar():\n pass
"""
if noqa:
return
line = core.import_normalize(logical_line.strip())
if line.startswith("class ") and not RE_NEW_STYLE_CLASS.match(line):
yield (0, "H238: old style class declaration, "
"use new style (inherit from `object`)")
|