Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
@core.off_by_default
def hacking_assert_greater_less(logical_line, noqa):
r"""Check that self.assert{Greater,Less}[Equal] are used.
Okay: self.assertGreater(x, y)
Okay: self.assertGreaterEqual(x, y)
Okay: self.assertLess(x, y)
Okay: self.assertLessEqual(x, y)
H205: self.assertTrue(x > y)
H205: self.assertTrue(x >= y)
H205: self.assertTrue(x < y)
H205: self.assertTrue(x <= y)
"""
if noqa:
return
methods = ['assertTrue', 'assertFalse']
@core.off_by_default
def hacking_assert_equal(logical_line, noqa):
r"""Check that self.assertEqual and self.assertNotEqual are used.
Okay: self.assertEqual(x, y)
Okay: self.assertNotEqual(x, y)
H204: self.assertTrue(x == y)
H204: self.assertTrue(x != y)
H204: self.assertFalse(x == y)
H204: self.assertFalse(x != y)
"""
if noqa:
return
methods = ['assertTrue', 'assertFalse']
for method in methods:
start = logical_line.find('.%s' % method) + 1
@core.off_by_default
def check_unittest_imports(logical_line):
"""N334 - Use unittest2 instead of unittest"""
if (re.match(unittest_imports_from, logical_line) or
re.match(unittest_imports_dot, logical_line)):
msg = "N334: '%s' must be used instead of '%s'." % (
logical_line.replace('unittest', 'unittest2'), logical_line)
yield (0, msg)
# 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 ast
from hacking import core
@core.off_by_default
@core.flake8ext
class MockAutospecCheck(object):
"""Check for 'autospec' in mock.patch/mock.patch.object calls
Okay: mock.patch('target_module_1', autospec=True)
Okay: mock.patch('target_module_1', autospec=False)
Okay: mock.patch('target_module_1', autospec=None)
Okay: mock.patch('target_module_1', defined_mock)
Okay: mock.patch('target_module_1', new=defined_mock)
Okay: mock.patch('target_module_1', new_callable=SomeFunc)
Okay: mock.patch('target_module_1', defined_mock)
Okay: mock.patch('target_module_1', spec=1000)
Okay: mock.patch('target_module_1', spec_set=['data'])
Okay: mock.patch('target_module_1', wraps=some_obj)
H210: mock.patch('target_module_1')
@core.off_by_default
def no_vim_headers(physical_line, line_number, lines):
r"""Check for vim editor configuration in source files.
By default vim modelines can only appear in the first or
last 5 lines of a source file.
Examples:
H106: # vim: set tabstop=4 shiftwidth=4\n#\n#\n#\n#\n#
H106: # Lic\n# vim: set tabstop=4 shiftwidth=4\n#\n#\n#\n#\n#
H106: # Lic\n#\n#\n#\n#\n#\n#\n#\n#\n# vim: set tabstop=4 shiftwidth=4
Okay: # Lic\n#\n#\n#\n#\n#\n#\n#
Okay: # viminal hill is located in Rome
Okay: # vim, ze nemluvis cesky
"""
if ((line_number <= 5 or line_number > len(lines) - 5) and
vim_header_re.match(physical_line)):
@core.off_by_default
def hacking_delayed_string_interpolation(logical_line, noqa):
r"""String interpolation should be delayed at logging calls.
H904: LOG.debug('Example: %s' % 'bad')
Okay: LOG.debug('Example: %s', 'good')
"""
msg = ("H904: String interpolation should be delayed to be "
"handled by the logging code, rather than being done "
"at the point of the logging call. "
"Use ',' instead of '%'.")
if noqa:
return
if log_string_interpolation.match(logical_line):
yield 0, msg
@core.off_by_default
def hacking_assert_is_none(logical_line, noqa):
"""Use assertIs(Not)None to check for None in assertions.
Okay: self.assertEqual('foo', 'bar')
Okay: self.assertNotEqual('foo', {}.get('bar', None))
Okay: self.assertIs('foo', 'bar')
Okay: self.assertIsNot('foo', 'bar', None)
Okay: foo(self.assertIsNot('foo', 'bar'))
H203: self.assertEqual(None, 'foo')
H203: self.assertNotEqual('foo', None)
H203: self.assertIs(None, 'foo', 'bar')
H203: self.assertIsNot('foo', None, 'bar')
H203: foo(self.assertIsNot('foo', None, 'bar'))
Okay: self.assertEqual(None, 'foo') # noqa
Okay: self.assertIs(None, 'foo') # noqa
Okay: self.assertIsNone('foo')