OpenStack Hacking Style Checks
Go to file
Stephen Finucane e8a45225e1 Remove Python 2->3 checks
These were not running on Python 3 codebases which is everything now.

Note that 'assertEquals'/'assertNotEquals', which is checked for by one
test, is still present in some codebases, despite it being removed in
Python 3. This is because this variant is added by testtools, which
adds its own deprecation warning that we don't need to duplicate.

Change-Id: I3f15376db78886b7eecad3fdfdd363c6ea5d19c6
Signed-off-by: Stephen Finucane <stephenfin@redhat.com>
2024-08-02 11:24:25 +01:00
doc remove unicode from code 2022-01-10 17:10:15 +08:00
hacking Remove Python 2->3 checks 2024-08-02 11:24:25 +01:00
integration-test Fix nova integration job 2023-08-13 08:40:13 +02:00
releasenotes Remove Python 2->3 checks 2024-08-02 11:24:25 +01:00
.gitignore Switch to stestr 2018-07-24 10:54:56 +07:00
.gitreview OpenDev Migration Patch 2019-04-19 19:33:16 +00:00
.mailmap Add .mailmap file 2013-05-24 19:58:36 +00:00
.pre-commit-hooks.yaml Allow hacking to be used as a pre-commit hook 2021-04-01 15:10:52 +01:00
.stestr.conf Switch to stestr 2018-07-24 10:54:56 +07:00
.zuul.yaml Fix nova integration job 2023-08-13 08:40:13 +02:00
CONTRIBUTING.rst [ussuri][goal] Update contributor documentation 2020-04-22 14:06:03 +00:00
HACKING.rst Add H216 to flag use of third party mock 2020-12-01 14:23:27 -06:00
LICENSE Make hacking a flake8 plugin. 2013-03-18 12:19:25 -07:00
MANIFEST.in Move the hacking guidelines to sphinx docs 2013-09-20 16:09:39 -07:00
README.rst Document new way of registering local plugins 2020-04-04 14:21:12 +00:00
requirements.txt Bump flake8 version 2023-12-05 12:22:59 +00:00
setup.cfg Remove Python 2->3 checks 2024-08-02 11:24:25 +01:00
setup.py Small cleanups 2020-04-04 14:22:15 +00:00
test-requirements.txt Add H216 to flag use of third party mock 2020-12-01 14:23:27 -06:00
tox.ini Drop tox target for python 3.5 2024-01-31 13:04:12 +09:00

Introduction

hacking is a set of flake8 plugins that test and enforce the OpenStack StyleGuide

Hacking pins its dependencies, as a new release of some dependency can break hacking based gating jobs. This is because new versions of dependencies can introduce new rules, or make existing rules stricter.

Installation

hacking is available from pypi, so just run:

pip install hacking

This will install specific versions of flake8 with the hacking, pep8, mccabe and pyflakes plugins.

Origin

Hacking started its life out as a text file in Nova's first commit. It was initially based on the Google Python Style Guide, and over time more OpenStack specific rules were added. Hacking serves several purposes:

  1. Agree on a common style guide so reviews don't get bogged down on style nit picks. (example: docstring guidelines)
  2. Make code written by many different authors easier to read by making the style more uniform. (example: unix vs windows newlines)
  3. Call out dangerous patterns and avoid them. (example: shadowing built-in or reserved words)

Initially the hacking style guide was enforced manually by reviewers, but this was a big waste of time so hacking, the tool, was born to automate the process and remove the extra burden from human reviewers.

Versioning

hacking uses the major.minor.maintenance release notation, where maintenance releases cannot contain new checks. This way projects can gate on hacking by pinning on the major.minor number while accepting maintenance updates without being concerned that a new version will break the gate with a new check.

For example a project can depend on hacking>=0.10.0,<0.11.0, and can know that 0.10.1 will not fail in places where 0.10.0 passed.

Adding additional checks

Each check is a pep8 plugin so read

The focus of new or changed rules should be to do one of the following

  • Substantially increase the reviewability of the code (eg: H301, H303) as they make it easy to understand where symbols come from)
  • Catch a common programming error that may arise in the future (H201)
  • Prevent a situation that would 100% of the time be -1ed by developers (H903)

But, as always, remember that these are Guidelines. Treat them as such. There are always times for exceptions. All new rules should support noqa.

If a check needs to be staged in, or it does not apply to every project or its branch, it can be added as off by default.

Requirements

  • The check must already have community support. We do not want to dictate style, only enforce it.
  • The canonical source of the OpenStack Style Guidelines is StyleGuide, and hacking just enforces them; so when adding a new check, it must be in HACKING.rst
  • False negatives are ok, but false positives are not
  • Cannot be project specific, project specific checks should be Local Checks
  • Include extensive tests
  • Registered as entry_points in setup.cfg
  • Error code must be in the relevant Hxxx group
  • The check should not attempt to import modules from the code being checked. Importing random modules, has caused all kinds of trouble for us in the past.

Enabling off-by-default checks

Some of the available checks are disabled by default. These checks are:

  • [H106] Don't put vim configuration in source files.
  • [H203] Use assertIs(Not)None to check for None.
  • [H204] Use assert(Not)Equal to check for equality.
  • [H205] Use assert(Greater|Less)(Equal) for comparison.
  • [H210] Require 'autospec', 'spec', or 'spec_set' in mock.patch/mock.patch.object calls
  • [H904] Delay string interpolations at logging calls.

To enable these checks, edit the flake8 section of the tox.ini file. For example to enable H106 and H203:

[flake8]
enable-extensions = H106,H203

Local Checks

hacking supports having local changes in a source tree. They need to be registered individually in tox.ini:

Add to tox.ini a new section flake8:local-plugins and list each plugin with its entry-point. Additionally, you can add the path to the files containing the plugins so that the repository does not need to be installed with the paths directive.

[flake8:local-plugins]
extension =
  N307 = checks:import_no_db_in_virt
  N325 = checks:CheckForStrUnicodeExc
paths =
  ./nova/hacking

The plugins, in the example above they live in nova/hacking/checks.py, need to annotate all functions with @core.flake8ext

from hacking import core
...
@core.flake8ext
def import_no_db_in_virt(logical_line, filename):
    ...

class CheckForStrUnicodeExc(BaseASTChecker):
   name = "check_for_str_unicode_exc"
   version = "1.0"
   ...

Further details are part of the flake8 documentation.