keystone.tests.hacking package

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.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]
class keystone.tests.hacking.checks.CheckForTranslationIssues(tree, filename)[source]

Bases: keystone.tests.hacking.checks.BaseASTChecker

I18N_MODULES = ('keystone.i18n._',)
LOGGING_CHECK_DESC = 'K005 Using translated string in logging'
LOG_MODULES = ('logging', 'oslo_log.log')
TRANS_HELPER_MAP = {'info': '_LI', 'exception': '_LE', 'warning': '_LW', 'critical': '_LC', 'error': '_LE', 'debug': None}
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]
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

Creative Commons Attribution 3.0 License

Except where otherwise noted, this document is licensed under Creative Commons Attribution 3.0 License. See all OpenStack Legal Documents.