Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace black with ruff format #74

Merged
merged 6 commits into from
Nov 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@
- **Python** development environment with:
- [Poetry](https://python-poetry.org/) for dependency management
- [Pytest](https://docs.pytest.org/) for testing
- [Black](https://black.readthedocs.io/) for code formatting
- [Ruff](https://beta.ruff.rs/) for static analysis
- [Ruff](https://docs.astral.sh/ruff/) for static analysis & code formatting
- [Mypy](https://mypy.readthedocs.io/) for type checking
- [Pre-commit](https://pre-commit.com/) for git hooks
- **Continuous Integration** of your choice:
Expand Down
6 changes: 3 additions & 3 deletions __tests__/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ describe("generator-sicarator:app", () => {
});

it("runs linter successfully", done => {
execInPyenvVenv("make ruff", (error, stdout, stderr) => {
execInPyenvVenv("make lint-check", (error, stdout, stderr) => {
if (stdout) {
console.log(`stdout:\n ${stdout}`);
}
Expand All @@ -342,7 +342,7 @@ describe("generator-sicarator:app", () => {
});

it("runs type checking successfully", done => {
execInPyenvVenv("make mypy", (error, stdout, stderr) => {
execInPyenvVenv("make type-check", (error, stdout, stderr) => {
if (stdout) {
console.log(`stdout:\n ${stdout}`);
}
Expand All @@ -360,7 +360,7 @@ describe("generator-sicarator:app", () => {
});

it("runs formatting check successfully", done => {
execInPyenvVenv("make black", (error, stdout, stderr) => {
execInPyenvVenv("make format-check", (error, stdout, stderr) => {
if (stdout) {
console.log(`stdout:\n ${stdout}`);
}
Expand Down
12 changes: 6 additions & 6 deletions generators/app/templates/ci/.azure-pipelines.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,14 @@ steps:
path: $(Agent.HomeDirectory)/../../.cache/pypoetry
displayName: "Cache poetry dependencies"

- script: make black
displayName: "Run black"
- script: make format-check
displayName: "Run formatter"

- script: make ruff
displayName: "Run ruff"
- script: make lint-check
displayName: "Run linter"

- script: make mypy
displayName: "Run mypy"
- script: make type-check
displayName: "Run type checker"

- script: make test
displayName: "Run tests"
Expand Down
24 changes: 12 additions & 12 deletions generators/app/templates/ci/.circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -60,29 +60,29 @@ jobs:
- store_artifacts:
path: htmlcov

black:
format-check:
executor: base-python
steps:
- get_code_and_cache
- run:
name: run black
command: make black
name: run formatter
command: make format-check

ruff:
lint-check:
executor: base-python
steps:
- get_code_and_cache
- run:
name: run ruff
command: make ruff
name: run linter
command: make lint-check

mypy:
type-check:
executor: base-python
steps:
- get_code_and_cache
- run:
name: run mypy
command: make mypy
name: run type checker
command: make type-check

workflows:
version: 2
Expand All @@ -92,12 +92,12 @@ workflows:
- test:
requires:
- install
- ruff:
- lint-check:
requires:
- install
- black:
- format-check:
requires:
- install
- mypy:
- type-check:
requires:
- install
6 changes: 3 additions & 3 deletions generators/app/templates/ci/.github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ jobs:
strategy:
matrix:
quality-command:
- black
- ruff
- mypy
- format-check
- lint-check
- type-check
steps:
- uses: actions/checkout@v3
- uses: ./.github/workflows/actions/install_dependencies
Expand Down
12 changes: 6 additions & 6 deletions generators/app/templates/ci/.gitlab-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,20 +37,20 @@ install:
- curl -sSL https://install.python-poetry.org | python - --version 1.5.1
- export PATH="/root/.local/bin:$PATH"

black:
format-check:
extends: .job_template
script:
- make black
- make format-check

ruff:
lint-check:
extends: .job_template
script:
- make ruff
- make lint-check

mypy:
type-check:
extends: .job_template
script:
- make mypy
- make type-check

test:
extends: .job_template
Expand Down
18 changes: 9 additions & 9 deletions generators/app/templates/common/.pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,27 +15,27 @@ repos:

- repo: local
hooks:
- id: black
name: Formatting (black)
entry: black
- id: format-fix
name: Formatting (ruff)
entry: ruff format
language: system
types: [python]
stages: [commit]
- id: ruff
name: Linter (ruff)
entry: ruff
- id: lint-check
name: Linting (ruff)
entry: ruff check
language: system
types: [python]
stages: [commit]
- id: mypy
- id: type-check
name: Type checking (mypy)
entry: make mypy
entry: make type-check
pass_filenames: false
language: system
types: [python]
stages: [commit]
- id: test
name: Unit tests (pytest)
name: Unit testing (pytest)
entry: make test
pass_filenames: false
language: system
Expand Down
13 changes: 8 additions & 5 deletions generators/app/templates/common/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,19 @@ install:
test:
poetry run pytest tests --cov src --cov-report term --cov-report=html --cov-report xml --junit-xml=tests-results.xml

black:
poetry run black . --check
format-check:
poetry run ruff format --check src tests
jack-mcivor marked this conversation as resolved.
Show resolved Hide resolved

ruff:
format-fix:
poetry run ruff format src tests

lint-check:
poetry run ruff check src tests

fix-ruff:
lint-fix:
poetry run ruff check src tests --fix

mypy:
type-check:
poetry run mypy src

<% if (includeApi) { -%>
Expand Down
16 changes: 8 additions & 8 deletions generators/app/templates/common/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -165,18 +165,18 @@ make test

## Formatting and static analysis

### Code formatting with `black`
### Code formatting with `ruff`

To check code formatting, run `black` with:
To check code formatting, run `ruff format` with:
```bash
black . --check
ruff format --check .
```
or
```bash
make black
make format-check
```

You can also [integrate it to your IDE](https://black.readthedocs.io/en/stable/integrations/editors.html) to reformat
You can also [integrate it to your IDE](https://docs.astral.sh/ruff/integrations/) to reformat
your code each time you save a file.

### Static analysis with `ruff`
Expand All @@ -187,12 +187,12 @@ ruff check src tests
```
or
```bash
make ruff
make lint-check
```

To run static analysis and to apply auto-fixes, run `ruff` with:
```bash
make fix-ruff
make lint-fix
```
### Type checking with `mypy`

Expand All @@ -202,7 +202,7 @@ mypy src --explicit-package-bases --namespace-packages
```
or
```bash
make mypy
make type-check
```

<% if (includeApi) { -%>
Expand Down
11 changes: 2 additions & 9 deletions generators/app/templates/common/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,14 @@ loguru = "^0.7.0"
<% } -%>

[tool.poetry.group.dev.dependencies]
black = "^22"
<% if (includeDvc) { -%>
dvc = {extras = ["<%= dvcRemoteType %>"], version = "^2.58.1"}
<% } -%>
mypy = "^1.2"
pre-commit = "^2"
pytest = "^7"
pytest-cov = "^3"
ruff = "^0"
ruff = "^0.1.5"
<% if (includeStreamlit) { -%>
streamlit = "^1.16.0"
<% } -%>
Expand All @@ -42,11 +41,6 @@ omit = ["src/constants.py"]
fail_under = 70.00
precision = 2

## black

[tool.black]
target-version = ['py<%= pythonMajorVersionShortcut %>']

## ruff
# Recommended ruff config for now, to be updated as we go along.
[tool.ruff]
Expand All @@ -64,7 +58,6 @@ select = [
"SIM", # flake8-simplify
"TCH", # flake8-type-checking
"TID", # flake8-tidy-imports
"Q", # flake8-quotes
jack-mcivor marked this conversation as resolved.
Show resolved Hide resolved
"UP", # pyupgrade
"PT", # flake8-pytest-style
"RUF", # Ruff-specific rules
Expand All @@ -73,7 +66,7 @@ select = [

ignore = [
"E501", # "Line too long"
# -> line length already regulated by black
# -> line length already regulated by the formatter
"PT011", # "pytest.raises() should specify expected exception"
# -> would imply to update tests every time you update exception message
"SIM102", # "Use a single `if` statement instead of nested `if` statements"
Expand Down