Skip to content

Commit cdb443b

Browse files
committed
Migrating to ruff for linting and sorting imports
1 parent 8afbe04 commit cdb443b

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+198
-226
lines changed
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,5 @@ jobs:
1313
uses: psf/black@stable
1414
with:
1515
jupyter: true
16-
- name: Run isort
17-
uses: isort/isort-action@v1
16+
- name: Run ruff
17+
uses: chartboost/ruff-action@v1

.poetry-sync-db.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
11
{
2-
"nbqa": {
3-
"repo": "https://github.com/nbQA-dev/nbQA",
4-
"rev": "${rev}"
5-
},
62
"nbstripout": {
73
"repo": "https://github.com/kynan/nbstripout",
84
"rev": "${rev}"
@@ -14,5 +10,9 @@
1410
"pre-commit-hooks": {
1511
"repo": "https://github.com/pre-commit/pre-commit-hooks",
1612
"rev": "v${rev}"
13+
},
14+
"ruff": {
15+
"repo": "https://github.com/astral-sh/ruff-pre-commit",
16+
"rev": "v${rev}"
1717
}
1818
}

.pre-commit-config.yaml

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,13 @@ repos:
1818
hooks:
1919
- id: black
2020
- id: black-jupyter
21-
- repo: https://github.com/pycqa/isort
22-
rev: 5.12.0
21+
- repo: https://github.com/astral-sh/ruff-pre-commit
22+
rev: v0.1.0
2323
hooks:
24-
- id: isort
24+
- id: ruff
25+
args: [--fix, --exit-non-zero-on-fix]
26+
types_or: [python, pyi, jupyter]
2527
- repo: https://github.com/kynan/nbstripout
2628
rev: 0.6.1
2729
hooks:
2830
- id: nbstripout
29-
- repo: https://github.com/nbQA-dev/nbQA
30-
rev: 1.7.0
31-
hooks:
32-
- id: nbqa-isort

auxiliary/col_comparisons.ipynb

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,13 @@
66
"metadata": {},
77
"outputs": [],
88
"source": [
9-
"%load_ext autoreload\n",
10-
"%autoreload 2"
9+
"import gravis as gv\n",
10+
"import matplotlib.pyplot as plt\n",
11+
"import pandas as pd\n",
12+
"\n",
13+
"from nhssynth.modules.dataloader.metadata import MetaData\n",
14+
"from nhssynth.modules.dataloader.metatransformer import MetaTransformer\n",
15+
"from nhssynth.modules.model.models import DPVAE"
1116
]
1217
},
1318
{
@@ -23,12 +28,8 @@
2328
"metadata": {},
2429
"outputs": [],
2530
"source": [
26-
"import matplotlib.pyplot as plt\n",
27-
"import pandas as pd\n",
28-
"\n",
29-
"from nhssynth.modules.dataloader.metadata import MetaData\n",
30-
"from nhssynth.modules.dataloader.metatransformer import MetaTransformer\n",
31-
"from nhssynth.modules.model.models import DPVAE, VAE"
31+
"%load_ext autoreload\n",
32+
"%autoreload 2"
3233
]
3334
},
3435
{
@@ -72,8 +73,6 @@
7273
"metadata": {},
7374
"outputs": [],
7475
"source": [
75-
"import gravis as gv\n",
76-
"\n",
7776
"gv.d3(mt.metadata.constraints.minimal_graph)\n",
7877
"# or gv.d3(mt.metadata.constraints.graph)"
7978
]
@@ -175,7 +174,7 @@
175174
"name": "python",
176175
"nbconvert_exporter": "python",
177176
"pygments_lexer": "ipython3",
178-
"version": "3.11.3"
177+
"version": "3.11.6"
179178
}
180179
},
181180
"nbformat": 4,

auxiliary/mwe.ipynb

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,28 +6,29 @@
66
"metadata": {},
77
"outputs": [],
88
"source": [
9-
"%load_ext autoreload\n",
10-
"%autoreload 2"
9+
"import gravis as gv\n",
10+
"import pandas as pd\n",
11+
"\n",
12+
"from nhssynth.modules.dataloader.metadata import MetaData\n",
13+
"from nhssynth.modules.dataloader.metatransformer import MetaTransformer\n",
14+
"from nhssynth.modules.model.models import DPVAE"
1115
]
1216
},
1317
{
14-
"cell_type": "markdown",
18+
"cell_type": "code",
19+
"execution_count": null,
1520
"metadata": {},
21+
"outputs": [],
1622
"source": [
17-
"We first load in the required parts of the `NHSSynth` package and other packages that we will use in this notebook."
23+
"%load_ext autoreload\n",
24+
"%autoreload 2"
1825
]
1926
},
2027
{
21-
"cell_type": "code",
22-
"execution_count": null,
28+
"cell_type": "markdown",
2329
"metadata": {},
24-
"outputs": [],
2530
"source": [
26-
"import pandas as pd\n",
27-
"\n",
28-
"from nhssynth.modules.dataloader.metadata import MetaData\n",
29-
"from nhssynth.modules.dataloader.metatransformer import MetaTransformer\n",
30-
"from nhssynth.modules.model.models import DPVAE"
31+
"We first load in the required parts of the `NHSSynth` package and other packages that we will use in this notebook."
3132
]
3233
},
3334
{
@@ -71,8 +72,6 @@
7172
"metadata": {},
7273
"outputs": [],
7374
"source": [
74-
"import gravis as gv\n",
75-
"\n",
7675
"gv.d3(mt.metadata.constraints.minimal_graph)\n",
7776
"# or gv.d3(mt.metadata.constraints.graph)"
7877
]
@@ -150,7 +149,7 @@
150149
"name": "python",
151150
"nbconvert_exporter": "python",
152151
"pygments_lexer": "ipython3",
153-
"version": "3.11.3"
152+
"version": "3.11.6"
154153
}
155154
},
156155
"nbformat": 4,

docs/development_guide.md

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -62,25 +62,31 @@ python setup.py install
6262

6363
### Style
6464

65-
We use [`black`](https://black.readthedocs.io/en/stable/) for code formatting. This is a fairly opinionated formatter, but it is widely used and has a good reputation. We also use [`isort`](https://pycqa.github.io/isort/) to manage imports. Both of these tools are run automatically via a [`pre-commit`](https://pre-commit.com) hook. Ensure you have installed the package with the `dev` group of dependencies and then run the following command to install the hooks:
65+
We use [`black`](https://black.readthedocs.io/en/stable/) for code formatting. This is a fairly opinionated formatter, but it is widely used and has a good reputation. We also use [`ruff`](https://docs.astral.sh/ruff/) to manage imports and lint the code. Both of these tools are run automatically via [`pre-commit`](https://pre-commit.com) hooks. Ensure you have installed the package with the `dev` group of dependencies and then run the following command to install the hooks:
6666

6767
```bash
6868
pre-commit install
6969
```
7070

7171
*Note that you may need to pre-pend this command with `poetry run` if you are not using your own virtual environment.*
7272

73-
This will ensure that your code conforms to the two formatters' requirements each time you commit to a branch. `black` and `isort` are also run as part of the CI workflow discussed below, such that even without these hooks, the code will be checked and raise an error on GitHub if it is not formatted consistently.
73+
This will ensure that your code conforms to the two formatters' / linters' requirements each time you commit to a branch. `black` and `ruff` are also run as part of the CI workflow discussed below, such that even without these hooks, the code will be checked and raise an error on GitHub if it is not formatted consistently.
7474

7575
Configuration for both packages can be found in the [`pyproject.toml`](https://github.com/nhsengland/NHSSynth/blob/main/pyproject.toml), this configuration should be picked up automatically by both the pre-commit hooks and your IDE / running them manually in the command line. The main configuration is as follows:
7676

7777
```toml
7878
[tool.black]
7979
line-length = 120
8080
81-
[tool.isort]
82-
profile = "black"
83-
known_first_party = "nhssynth"
81+
[tool.ruff]
82+
include = ["*.py", "*.pyi", "**/pyproject.toml", "*.ipynb"]
83+
select = ["E4", "E7", "E9", "F", "C90", "I"]
84+
85+
[tool.ruff.per-file-ignores]
86+
"src/nhssynth/common/constants.py" = ["F403", "F405"]
87+
88+
[tool.ruff.isort]
89+
known-first-party = ["nhssynth"]
8490
```
8591
8692
This ensure that absolute imports from `NHSSynth` are sorted separately from the rest of the imports in a file.
@@ -196,7 +202,7 @@ This will prompt for PyPI credentials, and then publish the package. *Note that
196202
197203
### Continuous Integration
198204
199-
We use GitHub Actions for continuous integration. The different workflows comprising this can be found in the [`.github/workflows`](https://github.com/nhsengland/NHSSynth/blob/main/.github/workflows/) folder. In general, the CI workflow is triggered on every push to the `main` or a feature branch - as appropriate - and runs tests against all supported versions of Python. It also runs `black` and `isort` to check that the code is formatted correctly, and builds the documentation site.
205+
We use GitHub Actions for continuous integration. The different workflows comprising this can be found in the [`.github/workflows`](https://github.com/nhsengland/NHSSynth/blob/main/.github/workflows/) folder. In general, the CI workflow is triggered on every push to the `main` or a feature branch - as appropriate - and runs tests against all supported versions of Python. It also runs `black` and `ruff` to check that the code is formatted correctly, and builds the documentation site.
200206
201207
There are also scripts to update the [dynamic badges](https://github.com/marketplace/actions/dynamic-badges) in the [`README`](https://github.com/nhsengland/NHSSynth/blob/main/README.md). These work via a gist associated with the repository. It is not easy to transfer ownership of this process, so if they break please feel free to [contact me](mailto:h.wilde@ucl.ac.uk).
202208

poetry.lock

Lines changed: 27 additions & 64 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,10 @@ pytest-cov = "^4.0.0"
4545
tox = "^4.0.0"
4646
mock = "^5.1.0"
4747
black = {extras = ["jupyter"], version = "^23.9.1"}
48-
isort = "^5.12.0"
4948
pre-commit = "^3.5.0"
5049
nbstripout = "^0.6.1"
5150
pre-commit-hooks = "^4.5.0"
52-
nbqa = "^1.7.0"
51+
ruff = "^0.1.0"
5352

5453
[tool.poetry.group.aux]
5554
optional = true
@@ -79,9 +78,15 @@ urllib3 = "^2.0.7"
7978
[tool.black]
8079
line-length = 120
8180

82-
[tool.isort]
83-
profile = "black"
84-
known_first_party = "nhssynth"
81+
[tool.ruff]
82+
include = ["*.py", "*.pyi", "**/pyproject.toml", "*.ipynb"]
83+
select = ["E4", "E7", "E9", "F", "C90", "I"]
84+
85+
[tool.ruff.per-file-ignores]
86+
"src/nhssynth/common/constants.py" = ["F403", "F405"]
87+
88+
[tool.ruff.isort]
89+
known-first-party = ["nhssynth"]
8590

8691
[tool.pytest.ini_options]
8792
testpaths = "tests"

src/nhssynth/cli/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
from .run import run
1+
from .run import run as run

src/nhssynth/cli/common_arguments.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import argparse
77
from typing import Final
88

9-
from nhssynth.common.constants import *
9+
from nhssynth.common.constants import TIME
1010

1111

1212
def get_core_parser(overrides=False) -> argparse.ArgumentParser:
@@ -35,7 +35,7 @@ def get_core_parser(overrides=False) -> argparse.ArgumentParser:
3535
"--experiment-name",
3636
type=str,
3737
default=TIME,
38-
help=f"name the experiment run to affect logging, config, and default-behaviour i/o",
38+
help="name the experiment run to affect logging, config, and default-behaviour i/o",
3939
)
4040
core_grp.add_argument(
4141
"--save-config",

0 commit comments

Comments
 (0)