Skip to content

Coding conventions

Peter Corke edited this page Aug 17, 2026 · 5 revisions

Style

PEP 8 rules

We aspire to the Zen of Python

Enforced/auto-formatted via ruff ([tool.ruff] in pyproject.toml), line length 88.

docstrings

see detailed doc string conventions here

Package imports

At the top of each file we import packages in the following order

  • Standard Python packages
  • NumPy: import numpy as np
  • SciPy: import script as sp
  • Matplotlib
    • import matplotlib as mpl
    • import matplotlib.pyplot as ppt
    • import mpl_toolkits.mplot3d as mpl3d`
  • Spatial Math Toolbox
    • import spatialmath as sm
    • from spatialmath import base
  • Robotics Toolbox
    • for toolbox code, to avoid circular imports always just import the required class, eg. from roboticstoolbox.module import class
    • for application code, import roboticstoolbox as rtb
  • all other imports

Unit testing

We use pytest

Code quality

We use codecov to analyse and display unit test coverage, we aim for over 90% coverage.

LGTM.com, previously used for static code-quality grading, shut down in December 2022 — RTB currently has no replacement quality-grade tool wired in (some sibling toolboxes use Codacy for this; it isn't set up on RTB).

Coding patterns

We strive to be Pythonic rather than MATLABish.

  • we prefer for x in X rather than for i in range() and indexing. If X is a NumPy array then x iterates over matrix rows. If we do need an index as well as the object, then use enumerate().
  • we test arguments for appropriateness and raise a ValueError or TypeError
  • for argument checking we use spatialmath.base.argcheck
    • we test for a scalar argument using isscalar
    • we test for a vector argument using isvector which accepts a Python list, tuple or NumPy array

Clone this wiki locally