Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
What does this PR do?
This adds the ability to easily test for style in our CI and format code locally at will.
Motivation
Consistent code formatting across https://github.com/DataDog/integrations-core and https://github.com/DataDog/integrations-extras.
Implementation
We use a tox plugin that, when
dd_check_style
is set to true intox.ini
, will dynamically expose 2 new environments:style
- This will check the formatting using a variety of tools. Using the-s/--style
flag ofddev test
will execute this environment rather than standaloneflake8
.format_style
- This will format the code for you, often without the need for any additional manual edits. You can run the formatter by using the new-fs/--format-style
flag ofddev test
.New checks will have this enabled upon creation.
Style
We now use all of the main style checkers the Python community has coalesced on.
__future__
, stdlib, third party, first party, and local.datadog_checks
is configured as a first party namespace.flake8
plugin for finding likely bugs and design problems in programs. We use:except:
, it also catches unexpected events like memory errors, interrupts, system exit, and so on. Preferexcept Exception:
."os.environ
doesn't clear the environment. Subprocesses are going to see outdated variables, in disagreement with the current process. Useos.environ.clear()
or theenv=
argument to Popen.".iter*
methods on dictionaries. The default behavior is to return iterables. Simply remove theiter
prefix from the method. For Python 2 compatibility, also prefer the Python 3 equivalent if you expect that the size of the dict to be small and bounded. The performance regression on Python 2 will be negligible and the code is going to be the clearest. Alternatively, usesix.iter*
."BaseException.message
has been deprecated as of Python 2.6 and is removed in Python 3. Usestr(e)
to access the user-readable message. Usee.args
to access arguments passed to the exception."self
for instance methods, andcls
for class methods."Config files
black
andisort
configuration resides in the now-standardizedpyproject.toml
at the root.flake8
will follow suit when support lands, but for now resides in its usual.flake8
.Once this is merged we'll start syncing these with https://github.com/DataDog/integrations-extras.
Future
Add the
black
readme badge for each repo when finishedAlso, we may want to eventually:
flake8
environmentNotes
There are only 2 things to look out for when formatting: