Skip to content

Commit dc73777

Browse files
committedJan 30, 2024
chore(dev): replace black with ruff
Ruff has included (for a few months now) a formatter functionality. The formatting and linting are entirely distinct functionalities, though both provided by Ruff. My experience so far has been that Ruff's formatting is very fast, and mostly matches Black's formatting. Signed-off-by: JP-Ellis <josh@jpellis.me>
1 parent b3207c8 commit dc73777

20 files changed

+81
-70
lines changed
 

‎.cirrus.yml

+2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ TEST_TEMPLATE: &TEST_TEMPLATE
66
- python --version
77
# TODO: Fix lints before enabling
88
- echo hatch run lint
9+
- echo hatch run typecheck
10+
- echo hatch run format
911
- hatch run test
1012

1113
linux_arm64_task:

‎.github/workflows/test.yml

+6
Original file line numberDiff line numberDiff line change
@@ -134,3 +134,9 @@ jobs:
134134

135135
- name: Lint
136136
run: hatch run lint
137+
138+
- name: Typecheck
139+
run: hatch run typecheck
140+
141+
- name: Format
142+
run: hatch run format

‎.pre-commit-config.yaml

+1-9
Original file line numberDiff line numberDiff line change
@@ -45,16 +45,8 @@ repos:
4545
# files in pact/v3/** and tests/v3/**.
4646
exclude: ^(pact|tests)/(?!v3/).*\.py$
4747
args: [--fix, --exit-non-zero-on-fix]
48-
stages: [pre-push]
49-
50-
- repo: https://github.com/psf/black
51-
rev: 24.1.1
52-
hooks:
53-
- id: black
54-
# Exclude python files in pact/** and tests/**, except for the
55-
# files in pact/v3/** and tests/v3/**.
48+
- id: ruff-format
5649
exclude: ^(pact|tests)/(?!v3/).*\.py$
57-
stages: [pre-push]
5850

5951
- repo: https://github.com/commitizen-tools/commitizen
6052
rev: v3.13.0

‎CONTRIBUTING.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -109,11 +109,11 @@ You can also try using the new [github.dev](https://github.dev/pact-foundation/p
109109

110110
- **Most important: Look around.** Match the style you see used in the rest of the project. This includes formatting, naming files, naming things in code, naming things in documentation, etc.
111111
- "Attractive"
112-
- We do have Black (a formatter) and Ruff (a syntax linter) to catch most stylistic problems. If you are working locally, they should automatically fix some issues during git commits and push.
112+
- We do have Ruff to catch most stylistic problems (both linting and formatting). If you are working locally, they should automatically fix some issues during git commits and push.
113113

114114
Don't worry too much about styles in general—the maintainers will help you fix them as they review your code.
115115
116-
To help catch a lot of simple formatting or linting issues, you can run `hatch run lint` to run Black and Ruff. This process can also be automated by installing [`pre-commit`](https://pre-commit.com/) hooks:
116+
To help catch a lot of simple formatting or linting issues, you can run `hatch run lint` to run the linter and `hatch run format` to format your code. This process can also be automated by installing [`pre-commit`](https://pre-commit.com/) hooks:
117117
118118
```sh
119119
pre-commit install

‎Makefile

+2
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ example:
4949
.PHONY: lint
5050
lint:
5151
hatch run lint
52+
hatch run format
53+
hatch run typecheck
5254

5355

5456
.PHONY: ci

‎examples/tests/test_00_consumer.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@
2121

2222
import pytest
2323
import requests
24-
from pact import Consumer, Format, Like, Provider
2524
from yarl import URL
2625

2726
from examples.src.consumer import User, UserConsumer
27+
from pact import Consumer, Format, Like, Provider
2828

2929
if TYPE_CHECKING:
3030
from pathlib import Path

‎examples/tests/test_01_provider_fastapi.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,11 @@
3030

3131
import pytest
3232
import uvicorn
33-
from pact import Verifier
3433
from pydantic import BaseModel
3534
from yarl import URL
3635

3736
from examples.src.fastapi import app
37+
from pact import Verifier
3838

3939
PROVIDER_URL = URL("http://localhost:8080")
4040

‎examples/tests/test_01_provider_flask.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,10 @@
3030

3131
import pytest
3232
from flask import request
33-
from pact import Verifier
3433
from yarl import URL
3534

3635
from examples.src.flask import app
36+
from pact import Verifier
3737

3838
PROVIDER_URL = URL("http://localhost:8080")
3939

‎examples/tests/test_02_message_consumer.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,9 @@
3535
from unittest.mock import MagicMock
3636

3737
import pytest
38-
from pact import MessageConsumer, MessagePact, Provider
3938

4039
from examples.src.message import Handler
40+
from pact import MessageConsumer, MessagePact, Provider
4141

4242
if TYPE_CHECKING:
4343
from pathlib import Path

‎examples/tests/test_03_message_provider.py

+1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
from typing import TYPE_CHECKING, Dict
3030

3131
from flask import Flask
32+
3233
from pact import MessageProvider
3334

3435
if TYPE_CHECKING:

‎pact/v3/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
considered deprecated, and will be removed in a future release.
2121
"""
2222

23-
from .pact import Interaction, Pact
23+
from pact.v3.pact import Interaction, Pact
2424

2525
__all__ = [
2626
"Pact",

‎pact/v3/ffi.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@
8888
from enum import Enum
8989
from typing import TYPE_CHECKING, Any, List
9090

91-
from ._ffi import ffi, lib # type: ignore[import]
91+
from pact.v3._ffi import ffi, lib # type: ignore[import]
9292

9393
if TYPE_CHECKING:
9494
from pathlib import Path
@@ -5146,7 +5146,7 @@ def with_query_parameter_v2(
51465146
handle,
51475147
"version",
51485148
0,
5149-
json.dumps({ "value": ["2", "3"] })
5149+
json.dumps({"value": ["2", "3"]}),
51505150
)
51515151
```
51525152
@@ -5288,7 +5288,7 @@ def with_header_v2(
52885288
part,
52895289
"Accept-Version",
52905290
0,
5291-
json.dumps({ "value": ["2", "3"] })
5291+
json.dumps({"value": ["2", "3"]}),
52925292
)
52935293
```
52945294

‎pact/v3/pact.py

+16-15
Original file line numberDiff line numberDiff line change
@@ -144,8 +144,9 @@ def given(
144144
145145
```python
146146
(
147-
pact.upon_receiving("a request")
148-
.given("a user exists", name="id", value="123")
147+
pact.upon_receiving("a request").given(
148+
"a user exists", name="id", value="123"
149+
)
149150
)
150151
```
151152
@@ -156,11 +157,13 @@ def given(
156157
157158
```python
158159
(
159-
pact.upon_receiving("a request")
160-
.given("a user exists", parameters={
161-
"id": "123",
162-
"name": "John",
163-
})
160+
pact.upon_receiving("a request").given(
161+
"a user exists",
162+
parameters={
163+
"id": "123",
164+
"name": "John",
165+
},
166+
)
164167
)
165168
```
166169
@@ -200,8 +203,9 @@ def given(
200203
201204
```python
202205
(
203-
pact.upon_receiving("a request")
204-
.given("a user exists", name="value", value=parameters)
206+
pact.upon_receiving("a request").given(
207+
"a user exists", name="value", value=parameters
208+
)
205209
)
206210
```
207211
@@ -503,8 +507,7 @@ def with_header(
503507
504508
```python
505509
(
506-
pact.upon_receiving("a request")
507-
.with_header(
510+
pact.upon_receiving("a request").with_header(
508511
"Accept-Version",
509512
json.dumps({
510513
"value": "1.2.3",
@@ -693,8 +696,7 @@ def with_query_parameter(self, name: str, value: str) -> Self:
693696
694697
```python
695698
(
696-
pact.upon_receiving("a request")
697-
.with_query_parameter(
699+
pact.upon_receiving("a request").with_query_parameter(
698700
"name",
699701
json.dumps({
700702
"value": ["John", "Mary"],
@@ -709,8 +711,7 @@ def with_query_parameter(self, name: str, value: str) -> Self:
709711
710712
```python
711713
(
712-
pact.upon_receiving("a request")
713-
.with_query_parameter(
714+
pact.upon_receiving("a request").with_query_parameter(
714715
"version",
715716
json.dumps({
716717
"value": "1.2.3",

‎pyproject.toml

+36-34
Original file line numberDiff line numberDiff line change
@@ -76,12 +76,7 @@ test = [
7676
"pytest-cov ~= 4.0",
7777
"testcontainers ~= 3.0",
7878
]
79-
dev = [
80-
"pact-python[types]",
81-
"pact-python[test]",
82-
"black ==24.1.1",
83-
"ruff ==0.1.14",
84-
]
79+
dev = ["pact-python[types]", "pact-python[test]", "ruff==0.1.14"]
8580

8681
################################################################################
8782
## Hatch Build Configuration
@@ -127,10 +122,14 @@ extra-dependencies = [
127122
]
128123

129124
[tool.hatch.envs.default.scripts]
130-
lint = ["black --check --diff {args:.}", "ruff {args:.}", "mypy {args:.}"]
131-
test = "pytest {args:tests/}"
132-
example = "pytest examples/ {args}"
133-
all = ["lint", "test", "example"]
125+
lint = "ruff check --show-source --show-fixes {args}"
126+
typecheck = "mypy {args:.}"
127+
format = "ruff format --diff {args}"
128+
test = "pytest tests/ {args}"
129+
example = "pytest examples/ {args}"
130+
all = ["format", "lint", "typecheck", "test", "example"]
131+
docs = ["mkdocs serve {args}"]
132+
docs-build = ["mkdocs build {args}"]
134133

135134
# Test environment for running unit tests. This automatically tests against all
136135
# supported Python versions.
@@ -140,11 +139,6 @@ features = ["test"]
140139
[[tool.hatch.envs.test.matrix]]
141140
python = ["3.8", "3.9", "3.10", "3.11", "3.12"]
142141

143-
[tool.hatch.envs.test.scripts]
144-
test = "pytest {args:tests/}"
145-
example = "pytest examples/ {args}"
146-
all = ["test", "example"]
147-
148142
################################################################################
149143
## PyTest Configuration
150144
################################################################################
@@ -186,17 +180,6 @@ exclude_lines = [
186180

187181
[tool.ruff]
188182
target-version = "py38"
189-
select = ["ALL"]
190-
191-
ignore = [
192-
"D200", # Require single line docstrings to be on one line.
193-
"D203", # Require blank line before class docstring
194-
"D212", # Multi-line docstring summary must start at the first line
195-
"ANN101", # `self` must be typed
196-
"ANN102", # `cls` must be typed
197-
"FIX002", # Forbid TODO in comments
198-
"TD002", # Assign someone to 'TODO' comments
199-
]
200183

201184
# TODO: Remove the explicity extend-exclude once astral-sh/ruff#6262 is fixed.
202185
# https://github.com/pact-foundation/pact-python/issues/458
@@ -236,19 +219,38 @@ extend-exclude = [
236219
"tests/test_verify_wrapper.py",
237220
]
238221

239-
[tool.ruff.pyupgrade]
222+
[tool.ruff.lint]
223+
select = ["ALL"]
224+
225+
ignore = [
226+
"D200", # Require single line docstrings to be on one line.
227+
"D203", # Require blank line before class docstring
228+
"D212", # Multi-line docstring summary must start at the first line
229+
"ANN101", # `self` must be typed
230+
"ANN102", # `cls` must be typed
231+
"FIX002", # Forbid TODO in comments
232+
"TD002", # Assign someone to 'TODO' comments
233+
234+
# The following are disabled for compatibility with the formatter
235+
"COM812", # enforce trailing commas
236+
"ISC001", # require imports to be sorted
237+
]
238+
239+
[tool.ruff.lint.pyupgrade]
240240
keep-runtime-typing = true
241241

242-
[tool.ruff.pydocstyle]
242+
[tool.ruff.lint.pydocstyle]
243243
convention = "google"
244244

245-
################################################################################
246-
## Black Configuration
247-
################################################################################
245+
[tool.ruff.isort]
246+
known-first-party = ["pact"]
247+
248+
[tool.ruff.flake8-tidy-imports]
249+
ban-relative-imports = "all"
248250

249-
[tool.black]
250-
target-version = ["py38"]
251-
extend-exclude = '^/(pact|tests)/(?!v3).+\.py$'
251+
[tool.ruff.format]
252+
preview = true
253+
docstring-code-format = true
252254

253255
################################################################################
254256
## Mypy Configuration

‎tests/v3/compatiblity_suite/test_v1_consumer.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@
99

1010
import pytest
1111
import requests
12-
from pact.v3 import Pact
1312
from pytest_bdd import given, parsers, scenario, then, when
1413
from yarl import URL
1514

16-
from .util import ( # type: ignore[import-untyped]
15+
from pact.v3 import Pact
16+
from tests.v3.compatiblity_suite.util import (
1717
FIXTURES_ROOT,
1818
InteractionDefinition,
1919
string_to_int,

‎tests/v3/test_async_interaction.py

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import re
88

99
import pytest
10+
1011
from pact.v3 import Pact
1112

1213

‎tests/v3/test_ffi.py

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import re
1010

1111
import pytest
12+
1213
from pact.v3 import ffi
1314

1415

‎tests/v3/test_http_interaction.py

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
import aiohttp
1212
import pytest
13+
1314
from pact.v3 import Pact
1415

1516
if TYPE_CHECKING:

‎tests/v3/test_pact.py

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from typing import TYPE_CHECKING, Literal
99

1010
import pytest
11+
1112
from pact.v3 import Pact
1213

1314
if TYPE_CHECKING:

‎tests/v3/test_sync_interaction.py

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import re
88

99
import pytest
10+
1011
from pact.v3 import Pact
1112

1213

0 commit comments

Comments
 (0)
Please sign in to comment.