Skip to content

Commit

Permalink
Implement black and isort-based style checks
Browse files Browse the repository at this point in the history
  • Loading branch information
mdickinson committed Mar 22, 2023
1 parent f8ad947 commit 66aec4e
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 13 deletions.
3 changes: 2 additions & 1 deletion .flake8
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
[flake8]
max-line-length = 88
exclude = examples,build
ignore = E266, W503
ignore = E203, E266, W503
per-file-ignores = */api.py:F401
20 changes: 20 additions & 0 deletions .github/workflows/check-style.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
name: Run style checks

on: [pull_request, workflow_dispatch]

jobs:
style:
runs-on: 'ubuntu-latest'

steps:
- uses: actions/checkout@v3
- uses: actions/setup-python@v4
with:
python-version: '3.11'
cache: 'pip'
cache-dependency-path: '.github/workflows/style-requirements.txt'
- run: python -m pip install -r .github/workflows/style-requirements.txt
- run: |
python -m black --check --diff .
python -m isort --check --diff .
python -m flake8 .
4 changes: 4 additions & 0 deletions .github/workflows/style-requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
black ~= 23.0
flake8
flake8-ets
isort
3 changes: 0 additions & 3 deletions .github/workflows/test-with-edm.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,6 @@ jobs:
run: python -m pip install -r .github/workflows/bootstrap-requirements.txt
- name: Install test environment
run: python etstool.py install --runtime=${{ matrix.runtime }} --toolkit=${{ matrix.toolkit }}
- name: Flake8
run: python etstool.py flake8 --runtime=${{ matrix.runtime }} --toolkit=${{ matrix.toolkit }}
if: runner.os == 'Linux' && matrix.toolkit == 'null'
- name: Run tests (Ubuntu)
if: matrix.os == 'ubuntu-latest'
run: xvfb-run -a python etstool.py test --runtime=${{ matrix.runtime }} --toolkit=${{ matrix.toolkit }}
Expand Down
54 changes: 47 additions & 7 deletions etstool.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,6 @@
"apptools",
"coverage",
"enthought_sphinx_theme",
"flake8",
"flake8_ets",
"pyface",
"sphinx",
"sphinx_copybutton",
Expand All @@ -116,7 +114,14 @@
}

# Dependencies we install from PyPI
pypi_dependencies = {}
pypi_dependencies = {
# style packages; install from PyPI to make sure that we're compatible
# with the style checks used in the check-style.yml workflow
"black~=23.0",
"flake8",
"flake8-ets",
"isort",
}

# Dependencies we install from source for cron tests
# Order from packages with the most dependencies to one with the least
Expand Down Expand Up @@ -187,6 +192,16 @@ def cli():
pass


# Subgroup for checking and fixing style


@cli.group()
def style():
"""
Commands for checking style and applying style fixes.
"""


@cli.command()
@edm_option
@runtime_option
Expand Down Expand Up @@ -275,17 +290,42 @@ def shell(edm, runtime, toolkit, environment):
execute(commands, parameters)


@cli.command()
@style.command(name="check")
@edm_option
@runtime_option
@toolkit_option
@environment_option
def flake8(edm, runtime, toolkit, environment):
""" Run a flake8 check in a given environment.
def style_check(edm, runtime, toolkit, environment):
"""
Run style checks.
"""
parameters = get_parameters(edm, runtime, toolkit, environment)
commands = [
"{edm} run -e {environment} -- python -m black --check --diff .",
"{edm} run -e {environment} -- python -m isort --check --diff .",
"{edm} run -e {environment} -- python -m flake8 .",
]
execute(commands, parameters)


@style.command(name="fix")
@edm_option
@runtime_option
@toolkit_option
@environment_option
def style_fix(edm, runtime, toolkit, environment):
"""
Run style fixers.
This automatically fixes any black or isort failures, but it won't
automatically fix all flake8 errors.
"""
parameters = get_parameters(edm, runtime, toolkit, environment)
commands = ["{edm} run -e {environment} -- python -m flake8"]
commands = [
"{edm} run -e {environment} -- python -m black .",
"{edm} run -e {environment} -- python -m isort .",
"{edm} run -e {environment} -- python -m flake8 .",
]
execute(commands, parameters)


Expand Down
4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,11 @@ repository = 'https://github.com/enthought/envisage'
issues = 'https://github.com/enthought/envisage/issues'

[tool.black]
line-length = 79
line-length = 88

[tool.isort]
profile = 'black'
line_length = 79
line_length = 88
order_by_type = false
sections = ['FUTURE', 'STDLIB', 'THIRDPARTY', 'ENTHOUGHT', 'FIRSTPARTY', 'LOCALFOLDER']
known_enthought = ['apptools', 'pyface', 'traits', 'traitsui']
Expand Down

0 comments on commit 66aec4e

Please sign in to comment.