-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add style checker and formatter (#3299)
- Loading branch information
Showing
10 changed files
with
185 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# TODO: port this to pyproject.toml when supported: https://gitlab.com/pycqa/flake8/merge_requests/245 | ||
|
||
[flake8] | ||
select = B,C,E,F,W,B001,B003,B006,B007,B301,B305,B306,B902 | ||
ignore = E203,E722,W503 | ||
exclude = .eggs,.tox,build,compat.py,__init__.py,datadog_checks/dev/tooling/templates/*,datadog_checks/*/vendor/* | ||
max-line-length = 120 |
2 changes: 2 additions & 0 deletions
2
...s_dev/datadog_checks/dev/plugin/plugin.py → ...s_dev/datadog_checks/dev/plugin/pytest.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
# (C) Datadog, Inc. 2019 | ||
# All rights reserved | ||
# Licensed under a 3-clause BSD style license (see LICENSE) | ||
from __future__ import absolute_import | ||
|
||
import tox | ||
import tox.config | ||
|
||
STYLE_CHECK_ENV_NAME = 'style' | ||
STYLE_FORMATTER_ENV_NAME = 'format_style' | ||
STYLE_FLAG = 'dd_check_style' | ||
|
||
|
||
@tox.hookimpl | ||
def tox_configure(config): | ||
""" | ||
For more info, see: https://tox.readthedocs.io/en/latest/plugins.html | ||
For an example, see: https://github.com/tox-dev/tox-travis | ||
""" | ||
sections = config._cfg.sections | ||
|
||
# Cache these | ||
make_envconfig = None | ||
reader = None | ||
|
||
# Default to false so: | ||
# 1. we don't affect other projects using tox | ||
# 2. check migrations can happen gradually | ||
if str(sections.get('testenv', {}).get(STYLE_FLAG, 'false')).lower() == 'true': | ||
# Disable flake8 since we already include that | ||
config.envlist[:] = [env for env in config.envlist if not env.endswith('flake8')] | ||
|
||
make_envconfig = get_make_envconfig(make_envconfig) | ||
reader = get_reader(reader, config) | ||
|
||
add_style_checker(config, sections, make_envconfig, reader) | ||
add_style_formatter(config, sections, make_envconfig, reader) | ||
|
||
|
||
def add_style_checker(config, sections, make_envconfig, reader): | ||
# testenv:style | ||
section = '{}{}'.format(tox.config.testenvprefix, STYLE_CHECK_ENV_NAME) | ||
sections[section] = { | ||
# These tools only support Python 3+ | ||
'basepython': 'python3', | ||
'skip_install': 'true', | ||
'deps': 'flake8\nflake8-bugbear\nblack\nisort[pyproject]>=4.3.15', | ||
'commands': 'flake8 --config=../.flake8 .\nblack --check --diff .\nisort --check-only --diff --recursive .', | ||
} | ||
|
||
# Always add the environment configurations | ||
config.envconfigs[STYLE_CHECK_ENV_NAME] = make_envconfig( | ||
config, STYLE_CHECK_ENV_NAME, section, reader._subs, config | ||
) | ||
|
||
# Intentionally add to envlist when seeing what is available | ||
if any('--listenvs' in arg for arg in config.args): | ||
config.envlist.append(STYLE_CHECK_ENV_NAME) | ||
|
||
|
||
def add_style_formatter(config, sections, make_envconfig, reader): | ||
# testenv:format_style | ||
section = '{}{}'.format(tox.config.testenvprefix, STYLE_FORMATTER_ENV_NAME) | ||
sections[section] = { | ||
# These tools only support Python 3+ | ||
'basepython': 'python3', | ||
'skip_install': 'true', | ||
'deps': 'black\nisort[pyproject]>=4.3.15', | ||
# Run formatter AFTER sorting imports | ||
'commands': 'isort --recursive .\nblack .', | ||
} | ||
|
||
# Always add the environment configurations | ||
config.envconfigs[STYLE_FORMATTER_ENV_NAME] = make_envconfig( | ||
config, STYLE_FORMATTER_ENV_NAME, section, reader._subs, config | ||
) | ||
|
||
# Intentionally add to envlist when seeing what is available | ||
if any('--listenvs' in arg for arg in config.args): | ||
config.envlist.append(STYLE_FORMATTER_ENV_NAME) | ||
|
||
|
||
def get_make_envconfig(make_envconfig): | ||
if make_envconfig is None: | ||
make_envconfig = tox.config.ParseIni.make_envconfig | ||
|
||
# Make this a non-bound method for Python 2 compatibility | ||
make_envconfig = getattr(make_envconfig, '__func__', make_envconfig) | ||
|
||
return make_envconfig | ||
|
||
|
||
def get_reader(reader, config): | ||
if reader is None: | ||
# This is just boilerplate necessary to create a valid reader | ||
reader = tox.config.SectionReader('tox', config._cfg) | ||
reader.addsubstitutions(toxinidir=config.toxinidir, homedir=config.homedir) | ||
reader.addsubstitutions(toxworkdir=config.toxworkdir) | ||
config.distdir = reader.getpath('distdir', '{toxworkdir}/dist') | ||
reader.addsubstitutions(distdir=config.distdir) | ||
config.distshare = reader.getpath('distshare', '{homedir}/.tox/distshare') | ||
reader.addsubstitutions(distshare=config.distshare) | ||
|
||
return reader |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 1 addition & 4 deletions
5
...g_checks/dev/tooling/templates/check/{check_name}/datadog_checks/{check_name}/__init__.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,4 @@ | ||
{license_header}from .__about__ import __version__ | ||
from .{check_name} import {check_class} | ||
|
||
__all__ = [ | ||
'__version__', | ||
'{check_class}' | ||
] | ||
__all__ = ['__version__', '{check_class}'] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,46 @@ | ||
# NOTE: You have to use single-quoted strings in TOML for regular expressions. | ||
# It's the equivalent of r-strings in Python. Multiline strings are treated as | ||
# verbose regular expressions by Black. Use [ ] to denote a significant space | ||
# character. | ||
|
||
[tool.black] | ||
exclude = ''' | ||
# Directories | ||
/( | ||
\.eggs | ||
| \.git | ||
| \.hg | ||
| \.mypy_cache | ||
| \.tox | ||
| \.venv | ||
| _build | ||
| buck-out | ||
| build | ||
| dist | ||
# New integration templates | ||
| datadog_checks/dev/tooling/templates | ||
# Vendored third party libraries | ||
| datadog_checks/[^/]+/vendor | ||
)/ | ||
| | ||
# Files | ||
( | ||
# TODO: remove when upstream addresses https://github.com/DataDog/integrations-core/blob/c71e6d7204192a8002109da92452003598df2d28/datadog_checks_dev/datadog_checks/dev/tooling/signing.py#L9-L14 | ||
datadog_checks/dev/tooling/signing\.py$ | ||
) | ||
''' | ||
include = '\.pyi?$' | ||
line-length = 120 | ||
py36 = false | ||
skip-string-normalization = true | ||
|
||
[tool.isort] | ||
default_section = 'THIRDPARTY' | ||
force_grid_wrap = 0 | ||
include_trailing_comma = true | ||
known_first_party = 'datadog_checks' | ||
line_length = 120 | ||
multi_line_output = 3 | ||
skip_glob = 'datadog_checks/dev/tooling/signing.py,datadog_checks/dev/tooling/templates/*,datadog_checks/*/vendor/*' | ||
use_parentheses = true |