keystone.tests.hacking package

Submodules

keystone.tests.hacking.checks module

Keystone’s pep8 extensions.

In order to make the review process faster and easier for core devs we are adding some Keystone specific pep8 checks. This will catch common errors so that core devs don’t have to.

There are two types of pep8 extensions. One is a function that takes either a physical or logical line. The physical or logical line is the first param in the function definition and can be followed by other parameters supported by pep8. The second type is a class that parses AST trees. For more info please see pep8.py.

class keystone.tests.hacking.checks.BaseASTChecker(tree, filename)[source]

Bases: ast.NodeVisitor

Provides a simple framework for writing AST-based checks.

Subclasses should implement visit_* methods like any other AST visitor implementation. When they detect an error for a particular node the method should call self.add_error(offending_node). Details about where in the code the error occurred will be pulled from the node object.

Subclasses should also provide a class variable named CHECK_DESC to be used for the human readable error message.

add_error(node, message=None)[source]

Add an error caused by a node to the list of errors for pep8.

run()[source]

Called automatically by pep8.

class keystone.tests.hacking.checks.CheckForAssertingNoneEquality(tree, filename)[source]

Bases: keystone.tests.hacking.checks.BaseASTChecker

Ensure that code does not use a None with assert(Not*)Equal.

CHECK_DESC_IS = 'K003 Use self.assertIsNone(...) when comparing against None'
CHECK_DESC_ISNOT = 'K004 Use assertIsNotNone(...) when comparing against None'
visit_Call(node)[source]
class keystone.tests.hacking.checks.CheckForLoggingIssues(tree, filename)[source]

Bases: keystone.tests.hacking.checks.BaseASTChecker

DEBUG_CHECK_DESC = 'K005 Using translated string in debug logging'
EXCESS_HELPER_CHECK_DESC = 'K007 Using hints when _ is necessary'
I18N_MODULES = ('keystone.i18n._', 'keystone.i18n._LI', 'keystone.i18n._LW', 'keystone.i18n._LE', 'keystone.i18n._LC')
LOG_MODULES = ('logging', 'oslo_log.log')
NONDEBUG_CHECK_DESC = 'K006 Not using translating helper for logging'
TRANS_HELPER_MAP = {'debug': None, 'info': '_LI', 'error': '_LE', 'warning': '_LW', 'critical': '_LC', 'exception': '_LE'}
USING_DEPRECATED_WARN = 'K009 Using the deprecated Logger.warn'
generic_visit(node)[source]

Called if no explicit visitor function exists for a node.

visit_Assign(node)[source]

Look for ‘LOG = logging.getLogger’.

This handles the simple case:

name = [logging_module].getLogger(...)

  • or -

name = [i18n_name](...)

And some much more comple ones:

name = [i18n_name](...) % X

  • or -

self.name = [i18n_name](...) % X

visit_Call(node)[source]

Look for the ‘LOG.*’ calls.

visit_Import(node)[source]
visit_ImportFrom(node)[source]
class keystone.tests.hacking.checks.CheckForMutableDefaultArgs(tree, filename)[source]

Bases: keystone.tests.hacking.checks.BaseASTChecker

Check for the use of mutable objects as function/method defaults.

We are only checking for list and dict literals at this time. This means that a developer could specify an instance of their own and cause a bug. The fix for this is probably more work than it’s worth because it will get caught during code review.

CHECK_DESC = 'K001 Using mutable as a function/method default'
MUTABLES = (<class '_ast.List'>, <class '_ast.ListComp'>, <class '_ast.Dict'>, <class '_ast.DictComp'>, <class '_ast.Set'>, <class '_ast.SetComp'>, <class '_ast.Call'>)
visit_FunctionDef(node)[source]
keystone.tests.hacking.checks.block_comments_begin_with_a_space(physical_line, line_number)[source]

There should be a space after the # of block comments.

There is already a check in pep8 that enforces this rule for inline comments.

Okay: # this is a comment Okay: #!/usr/bin/python Okay: # this is a comment K002: #this is a comment

keystone.tests.hacking.checks.dict_constructor_with_sequence_copy(logical_line)[source]

Should use a dict comprehension instead of a dict constructor.

PEP-0274 introduced dict comprehension with performance enhancement and it also makes code more readable.

Okay: lower_res = {k.lower(): v for k, v in res[1].items()} Okay: fool = dict(a=’a’, b=’b’) K008: lower_res = dict((k.lower(), v) for k, v in res[1].items()) K008: attrs = dict([(k, _from_json(v)) K008: dict([[i,i] for i in range(3)])

keystone.tests.hacking.checks.factory(register)[source]

Module contents

Table Of Contents

Previous topic

keystone.tests.functional.shared package

Next topic

keystone.tests.unit package

Project Source

This Page