Debian Codegraph @

context/coding-style.md

raw
1 # Coding style and syntax
2
3 For new code and updates to existing code, including unit tests.
4
5 In general, prefer newer Python features as they become available.
6 A feature is available when the minimum supported Python version for the code being written supports it,
7 and it doesn't conflict with other supported versions.
8
9 ## Python version support
10
11 - Controller code: minimum version defined in `pyproject.toml` (`requires-python`).
12 - Modules/module_utils: minimum version in `lib/ansible/module_utils/basic.py` (`_PY_MIN`).
13 - Modules support a wider Python version range than controller code.
14 - Supported Python versions for testing are defined within `ansible-test`.
15
16 ## Dependencies
17
18 - Prefer Python stdlib over external dependencies.
19 - Use existing code from within the Ansible project.
20
21 ## Markdown
22
23 Markdown files use [GitHub Flavored Markdown](https://github.github.com/gfm/) (verified by the "pymarkdown" sanity test).
24
25 Use dashes (`-`) for unordered list items, not asterisks.
26 End list items with periods.
27
28 ## ASCII characters
29
30 - Use ASCII quotes (`'` and `"`) instead of Unicode smart quotes (verified by the "no-smart-quotes" sanity test).
31 - Use ASCII dashes (`-` or `--`) instead of em dashes.
32
33 ## Line length
34
35 The line limit is 160 characters.
36
37 ## Trailing whitespace
38
39 Don't leave trailing whitespace on lines.
40
41 ## Docstrings
42
43 Explain what the annotated code does, but don't create structured entries for parameters.
44 Don't document parameter types in docstrings -- use type hints instead.
45
46 Anything considered a public API must have a docstring.
47 Internal code should, and it often makes sense for unit tests, too.
48
49 ## Line breaks in source text
50
51 Try to stick to one sentence per line in text like docstrings, comments and changelog fragments.
52
53 ## Native type annotations
54
55 Use native type hints with `from __future__ import annotations` (verified by the "boilerplate" sanity test).
56 Include type annotations on function/method arguments and return types, unless the annotation becomes too complex (e.g. `TypedDict`).
57 The "mypy" sanity test only performs type checking on annotated functions/methods.
58
59 Prefer PEP 695 type parameter syntax over `TypeVar` and `ParamSpec` declarations.
60 For example, use `def foo[T](x: T) -> T` instead of declaring `T = TypeVar('T')` separately.
61 This does not apply to `module_utils/` code, which must support older Python versions that lack the feature.
62
63 ## Format strings
64
65 Use f-strings instead of `%` strings or `str.format`, except for logging where formatting is deferred.
66
67 ## Quoting strings
68
69 Use the `!r` format qualifier to quote a value instead of manually quoting.
70
71 Example: `f"A string with a {quoted!r} value."`
72
73 ## Code formatting check/fix for internals
74
75 The `black` sanity test runs against all `_internal` packages, using default settings with an increased line length of 160 and no quote conversion.
76 Use `ansible-test` to automatically apply required formatting changes.
77
78 Example: `ansible-test sanity --test black --fix`
79
80 ## Import ordering in modules
81
82 The E402 pycodestyle rule (module level import not at top) is ignored.
83 In `lib/ansible/modules/`, imports must come after the `DOCUMENTATION`, `EXAMPLES`, and `RETURN` definitions.