The cinder.hacking.checks Module

The cinder.hacking.checks Module

class BaseASTChecker(tree, filename)

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)

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


Called automatically by pep8.

class CheckForStrUnicodeExc(tree, filename)

Bases: cinder.hacking.checks.BaseASTChecker

Checks for the use of str() or unicode() on an exception.

This currently only handles the case where str() or unicode() is used in the scope of an exception handler. If the exception is passed into a function, returned from an assertRaises, or used on an exception created in the same scope, this does not catch it.

CHECK_DESC = 'N325 str() and unicode() cannot be used on an exception. Remove or use six.text_type()'
class CheckLoggingFormatArgs(tree, filename)

Bases: cinder.hacking.checks.BaseASTChecker

Check for improper use of logging format arguments.

LOG.debug(“Volume %s caught fire and is at %d degrees C and climbing.”,
(‘volume1’, 500))

The format arguments should not be a tuple as it is easy to miss.

CHECK_DESC = 'C310 Log method arguments should not be a tuple.'
LOG_METHODS = ['debug', 'info', 'warn', 'warning', 'error', 'exception', 'critical', 'fatal', 'trace', 'log']

Look for the ‘LOG.*’ calls.

class CheckOptRegistrationArgs(tree, filename)

Bases: cinder.hacking.checks.BaseASTChecker

Verifying the registration of options are well formed

This class creates a check for single opt or list/tuple of opts when register_opt() or register_opts() are being called.

CHECK_DESC = 'C311: Arguments being passed to register_opt/register_opts must be a single option or list/tuple of options respectively. Options must also end with _opt or _opts respectively.'
plural_method = 'register_opts'
register_methods = ['register_opt', 'register_opts']
singular_method = 'register_opt'

Look for the register_opt/register_opts calls.

check_datetime_now(logical_line, noqa)
check_explicit_underscore_import(logical_line, filename)

Check for explicit import of the _ function

We need to ensure that any files that are using the _() function to translate messages are explicitly importing the _ function. We can’t trust unit test to catch whether the import has been added so we need to check for it here.


Ensure that we are not using LOG.audit messages

Plans are in place going forward as discussed in the following spec ( to take out LOG.audit messages. Given that audit was a concept invented for OpenStack we can enforce not using it.

check_no_print_statements(logical_line, filename, noqa)
check_unicode_usage(logical_line, noqa)
no_test_log(logical_line, filename, noqa)
no_translate_logs(logical_line, filename)

Check for ‘LOG.*(_(‘

Starting with the Pike series, OpenStack no longer supports log translation. We shouldn’t translate logs.

  • This check assumes that ‘LOG’ is a logger.
  • Use filename so we can start enforcing this in specific folders instead of needing to do so all at once.


no_vi_headers(physical_line, line_number, lines)

Check for vi editor configuration in source files.

By default vi modelines can only appear in the first or last 5 lines of a source file.


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.