2016-03-03 11:41:46 -07:00
|
|
|
.. _coding-style:
|
|
|
|
|
2012-12-20 12:35:27 -07:00
|
|
|
=================
|
|
|
|
Salt Coding Style
|
|
|
|
=================
|
|
|
|
|
2019-12-30 14:31:59 -06:00
|
|
|
To make it easier to contribute and read Salt code, SaltStack has `adopted
|
|
|
|
Black <SEP 15_>`_ as its code formatter. There are a few places where Black is
|
|
|
|
silent, and this guide should be used in those cases.
|
2012-12-20 12:35:27 -07:00
|
|
|
|
2019-12-30 14:31:59 -06:00
|
|
|
Coding style is NEVER grounds to reject code contributions, and is never
|
|
|
|
grounds to talk down to another member of the community (There are no grounds
|
|
|
|
to treat others without respect, especially people working to improve Salt)!
|
2012-12-20 12:35:27 -07:00
|
|
|
|
|
|
|
|
2016-03-03 11:41:46 -07:00
|
|
|
.. _pylint-instructions:
|
|
|
|
|
2013-12-13 10:11:23 -07:00
|
|
|
Linting
|
|
|
|
=======
|
|
|
|
|
2020-11-25 05:31:48 +00:00
|
|
|
Most Salt style conventions are codified in Salt's ``.pylintrc`` file.
|
2021-05-25 20:50:28 +01:00
|
|
|
Salt's linting has two major dependencies: pylint_ and saltpylint_, the full lint
|
|
|
|
requirements can be found under ``requirements/static/ci/lint.in`` and the pinned
|
|
|
|
requirements at ``requirements/static/ci/py3.<minor-version>/lint.txt``, however,
|
|
|
|
linting should be done using :ref:`nox <getting_set_up_for_tests>`, which is how
|
|
|
|
pull requests are checked.
|
2016-03-03 10:56:22 -07:00
|
|
|
|
|
|
|
.. code-block:: bash
|
|
|
|
|
2020-11-25 05:31:48 +00:00
|
|
|
nox -e lint
|
2016-03-03 10:56:22 -07:00
|
|
|
|
2020-11-25 05:31:48 +00:00
|
|
|
One can target either salt's source code or the test suite(different pylint rules apply):
|
2013-12-13 10:11:23 -07:00
|
|
|
|
2014-04-08 15:15:00 -07:00
|
|
|
.. code-block:: bash
|
|
|
|
|
2020-11-25 05:31:48 +00:00
|
|
|
nox -e lint-salt
|
|
|
|
nox -e lint-tests
|
2018-07-27 09:09:28 -04:00
|
|
|
|
2013-12-13 10:11:23 -07:00
|
|
|
|
2020-05-04 03:28:38 -05:00
|
|
|
.. _pylint: https://www.pylint.org/
|
2016-03-03 10:56:22 -07:00
|
|
|
.. _saltpylint: https://github.com/saltstack/salt-pylint
|
2013-12-13 10:11:23 -07:00
|
|
|
|
2015-11-19 12:01:18 -07:00
|
|
|
Variables
|
|
|
|
=========
|
|
|
|
|
|
|
|
Variables should be a minimum of three characters and should provide an
|
|
|
|
easy-to-understand name of the object being represented.
|
|
|
|
|
|
|
|
When keys and values are iterated over, descriptive names should be used
|
|
|
|
to represent the temporary variables.
|
|
|
|
|
|
|
|
Multi-word variables should be separated by an underscore.
|
|
|
|
|
|
|
|
Variables which are two-letter words should have an underscore appended
|
|
|
|
to them to pad them to three characters.
|
|
|
|
|
2012-12-20 12:35:27 -07:00
|
|
|
Formatting Strings
|
|
|
|
------------------
|
|
|
|
|
|
|
|
All strings which require formatting should use the `.format` string method:
|
|
|
|
|
2020-11-25 05:31:48 +00:00
|
|
|
Please do NOT use printf formatting, unless it's a log message.
|
|
|
|
|
|
|
|
Good:
|
|
|
|
|
2012-12-20 12:35:27 -07:00
|
|
|
.. code-block:: python
|
|
|
|
|
|
|
|
data = "some text"
|
2020-11-25 05:31:48 +00:00
|
|
|
more = "{} and then some".format(data)
|
|
|
|
log.debug("%s and then some", data)
|
|
|
|
|
|
|
|
Bad:
|
2012-12-20 12:35:27 -07:00
|
|
|
|
2020-11-25 05:31:48 +00:00
|
|
|
.. code-block:: python
|
|
|
|
|
|
|
|
data = "some text"
|
|
|
|
log.debug("{} and then some".format(data))
|
2012-12-20 12:35:27 -07:00
|
|
|
|
|
|
|
|
|
|
|
Docstring Conventions
|
|
|
|
---------------------
|
|
|
|
|
2013-12-26 04:15:58 -06:00
|
|
|
When adding a new function or state, where possible try to use a
|
2019-12-30 14:31:59 -06:00
|
|
|
``versionadded`` directive to denote when the function, state, or parameter was added.
|
2013-12-26 04:15:58 -06:00
|
|
|
|
|
|
|
.. code-block:: python
|
|
|
|
|
2013-12-26 12:11:26 -06:00
|
|
|
def new_func(msg=""):
|
2020-06-09 09:58:34 +01:00
|
|
|
"""
|
2013-12-26 12:11:26 -06:00
|
|
|
.. versionadded:: 0.16.0
|
2013-12-26 04:15:58 -06:00
|
|
|
|
2013-12-26 12:11:26 -06:00
|
|
|
Prints what was passed to the function.
|
|
|
|
|
|
|
|
msg : None
|
|
|
|
The string to be printed.
|
2020-06-09 09:58:34 +01:00
|
|
|
"""
|
2019-12-30 14:31:59 -06:00
|
|
|
print(msg)
|
2013-12-26 04:15:58 -06:00
|
|
|
|
2014-02-26 13:28:13 -07:00
|
|
|
If you are uncertain what version should be used, either consult a core
|
2024-11-26 17:21:16 -06:00
|
|
|
developer in the Community Discord or bring this up when opening your :ref:`pull request
|
2019-12-30 14:31:59 -06:00
|
|
|
<installing-for-development>` and a core developer will let you know what
|
|
|
|
version to add. Typically this will be the next element in the `periodic table
|
|
|
|
<https://en.wikipedia.org/wiki/List_of_chemical_elements>`_.
|
2013-12-26 04:15:58 -06:00
|
|
|
|
|
|
|
Similar to the above, when an existing function or state is modified (for
|
|
|
|
example, when an argument is added), then under the explanation of that new
|
2013-12-26 12:11:26 -06:00
|
|
|
argument a ``versionadded`` directive should be used to note the version in
|
|
|
|
which the new argument was added. If an argument's function changes
|
|
|
|
significantly, the ``versionchanged`` directive can be used to clarify this:
|
2013-12-26 04:15:58 -06:00
|
|
|
|
|
|
|
.. code-block:: python
|
|
|
|
|
2013-12-26 12:11:26 -06:00
|
|
|
def new_func(msg="", signature=""):
|
2020-06-09 09:58:34 +01:00
|
|
|
"""
|
2013-12-26 12:11:26 -06:00
|
|
|
.. versionadded:: 0.16.0
|
2013-12-26 04:15:58 -06:00
|
|
|
|
2013-12-26 12:11:26 -06:00
|
|
|
Prints what was passed to the function.
|
|
|
|
|
|
|
|
msg : None
|
|
|
|
The string to be printed. Will be prepended with 'Greetings! '.
|
2013-12-26 04:15:58 -06:00
|
|
|
|
|
|
|
.. versionchanged:: 0.17.1
|
2013-12-26 12:11:26 -06:00
|
|
|
|
|
|
|
signature : None
|
|
|
|
An optional signature.
|
|
|
|
|
2020-07-16 21:30:01 +02:00
|
|
|
.. versionadded:: 0.17.0
|
2020-06-09 09:58:34 +01:00
|
|
|
"""
|
2019-12-30 14:31:59 -06:00
|
|
|
print("Greetings! {0}\n\n{1}".format(msg, signature))
|
2013-12-26 04:15:58 -06:00
|
|
|
|
|
|
|
|
2014-06-24 18:34:25 -06:00
|
|
|
Dictionaries
|
|
|
|
============
|
|
|
|
|
|
|
|
Dictionaries should be initialized using `{}` instead of `dict()`.
|
|
|
|
|
|
|
|
See here_ for an in-depth discussion of this topic.
|
|
|
|
|
2021-03-01 00:31:28 -05:00
|
|
|
.. _here: https://doughellmann.com/posts/the-performance-impact-of-using-dict-instead-of-in-cpython-2-7-2/
|
2014-06-24 18:34:25 -06:00
|
|
|
|
|
|
|
|
2012-12-20 12:35:27 -07:00
|
|
|
Imports
|
|
|
|
=======
|
|
|
|
|
|
|
|
Salt code prefers importing modules and not explicit functions. This is both a
|
|
|
|
style and functional preference. The functional preference originates around
|
|
|
|
the fact that the module import system used by pluggable modules will include
|
|
|
|
callable objects (functions) that exist in the direct module namespace. This
|
|
|
|
is not only messy, but may unintentionally expose code python libs to the Salt
|
|
|
|
interface and pose a security problem.
|
|
|
|
|
|
|
|
To say this more directly with an example, this is `GOOD`:
|
|
|
|
|
|
|
|
.. code-block:: python
|
|
|
|
|
|
|
|
import os
|
|
|
|
|
2020-06-09 09:58:34 +01:00
|
|
|
|
2012-12-20 12:35:27 -07:00
|
|
|
def minion_path():
|
|
|
|
path = os.path.join(self.opts["cachedir"], "minions")
|
|
|
|
return path
|
|
|
|
|
|
|
|
This on the other hand is `DISCOURAGED`:
|
|
|
|
|
|
|
|
.. code-block:: python
|
|
|
|
|
|
|
|
from os.path import join
|
|
|
|
|
2020-06-09 09:58:34 +01:00
|
|
|
|
2012-12-20 12:35:27 -07:00
|
|
|
def minion_path():
|
2013-07-13 12:32:49 +07:00
|
|
|
path = join(self.opts["cachedir"], "minions")
|
|
|
|
return path
|
2012-12-20 12:35:27 -07:00
|
|
|
|
|
|
|
The time when this is changed is for importing exceptions, generally directly
|
|
|
|
importing exceptions is preferred:
|
|
|
|
|
|
|
|
This is a good way to import exceptions:
|
|
|
|
|
|
|
|
.. code-block:: python
|
|
|
|
|
|
|
|
from salt.exceptions import CommandExecutionError
|
|
|
|
|
2013-08-21 00:04:21 +01:00
|
|
|
|
|
|
|
Absolute Imports
|
|
|
|
----------------
|
|
|
|
|
2014-12-11 07:51:43 -08:00
|
|
|
Although `absolute imports`_ seems like an awesome idea, please do not use it.
|
|
|
|
Extra care would be necessary all over salt's code in order for absolute
|
|
|
|
imports to work as supposed. Believe it, it has been tried before and, as a
|
|
|
|
tried example, by renaming ``salt.modules.sysmod`` to ``salt.modules.sys``, all
|
|
|
|
other salt modules which needed to import :mod:`sys<python2:sys>` would have to
|
|
|
|
also import :mod:`absolute_import<python2:__future__>`, which should be
|
2013-08-21 00:04:21 +01:00
|
|
|
avoided.
|
|
|
|
|
2016-05-20 11:32:25 -06:00
|
|
|
.. note::
|
|
|
|
|
|
|
|
An exception to this rule is the ``absolute_import`` from ``__future__`` at
|
|
|
|
the top of each file within the Salt project. This import is necessary for
|
|
|
|
Py3 compatibility. This particular import looks like this:
|
|
|
|
|
|
|
|
.. code-block:: python
|
|
|
|
|
|
|
|
from __future__ import absolute_import
|
|
|
|
|
|
|
|
This import is required for all new Salt files and is a good idea to add to
|
|
|
|
any custom states or modules. However, the practice of avoiding absolute
|
|
|
|
imports still applies to all other cases as to avoid a name conflict.
|
|
|
|
|
2020-05-04 03:28:38 -05:00
|
|
|
.. _`absolute imports`: https://legacy.python.org/dev/peps/pep-0328/#rationale-for-absolute-imports
|
2013-08-21 00:04:21 +01:00
|
|
|
|
|
|
|
|
2012-12-20 12:35:27 -07:00
|
|
|
Code Churn
|
|
|
|
==========
|
|
|
|
|
|
|
|
Many pull requests have been submitted that only churn code in the name of
|
2019-12-30 14:31:59 -06:00
|
|
|
PEP 8. Code churn is a leading source of bugs and is **strongly discouraged**.
|
2012-12-20 12:35:27 -07:00
|
|
|
While style fixes are encouraged they should be isolated to a single file per
|
|
|
|
commit, and the changes should be legitimate, if there are any questions about
|
|
|
|
whether a style change is legitimate please reference this document and the
|
2020-05-04 03:28:38 -05:00
|
|
|
official PEP 8 (https://legacy.python.org/dev/peps/pep-0008/) document before
|
2013-04-05 15:28:24 -07:00
|
|
|
changing code. Many claims that a change is PEP 8 have been invalid, please
|
2015-11-19 12:01:18 -07:00
|
|
|
double check before committing fixes.
|
2019-12-30 14:31:59 -06:00
|
|
|
|
|
|
|
.. _`SEP 15`: https://github.com/saltstack/salt-enhancement-proposals/pull/21
|