Skip to content

Commit

Permalink
chore: enable lints fully
Browse files Browse the repository at this point in the history
As there is a significant existing codebase which will eventually be
deprecated, there is little benefit to applying formatting and linting
rules to the files which are to be deprecated.

This commit configures Ruff, Mypy and Black to exclude files in the v2
codebase so that `hatch run lint` can function correctly. As a result,
the linting in the CI workflow has also been enabled.

An equivalent was already implemented for the pre-commit hooks.

Signed-off-by: JP-Ellis <[email protected]>
  • Loading branch information
JP-Ellis committed Oct 23, 2023
1 parent c7c00b7 commit 8aeec66
Show file tree
Hide file tree
Showing 15 changed files with 66 additions and 29 deletions.
27 changes: 13 additions & 14 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -103,22 +103,21 @@ jobs:
run: >
hatch run example --broker-url=http://pactbroker:pactbroker@localhost:9292
# TODO: Fix lints before enabling this
# lint:
# name: Lint
lint:
name: Lint

# runs-on: ubuntu-latest
runs-on: ubuntu-latest

# steps:
# - uses: actions/checkout@v4
steps:
- uses: actions/checkout@v4

# - name: Set up Python
# uses: actions/setup-python@v4
# with:
# python-version: ${{ env.STABLE_PYTHON_VERSION }}
- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: ${{ env.STABLE_PYTHON_VERSION }}

# - name: Install Hatch
# run: pip install --upgrade hatch
- name: Install Hatch
run: pip install --upgrade hatch

# - name: Lint
# run: hatch run lint
- name: Lint
run: hatch run lint
1 change: 1 addition & 0 deletions examples/.ruff.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ extend = "../pyproject.toml"
ignore = [
"S101", # Forbid assert statements
"D103", # Require docstring in public function
"D104", # Require docstring in public package
]

[per-file-ignores]
Expand Down
Empty file added examples/__init__.py
Empty file.
3 changes: 2 additions & 1 deletion examples/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,14 @@
Pact files will be stored. You are encouraged to have a look at these files
after the examples have been run.
"""

from __future__ import annotations

from pathlib import Path
from typing import Any, Generator, Union

import pytest
from testcontainers.compose import DockerCompose
from testcontainers.compose import DockerCompose # type: ignore[import-untyped]
from yarl import URL

EXAMPLE_DIR = Path(__file__).parent.resolve()
Expand Down
2 changes: 1 addition & 1 deletion examples/src/fastapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@


@app.get("/users/{uid}")
async def get_user_by_id(uid: int) -> Dict[str, Any]:
async def get_user_by_id(uid: int) -> JSONResponse:
"""
Fetch a user by their ID.
Expand Down
3 changes: 2 additions & 1 deletion examples/src/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
handler is solely responsible for processing the message, and does not
necessarily need to send a response.
"""

from __future__ import annotations

from pathlib import Path
Expand Down Expand Up @@ -59,7 +60,7 @@ def process(self, event: Dict[str, Any]) -> Union[str, None]:
self.validate_event(event)

if event["action"] == "WRITE":
return self.fs.write(event["path"], event.get("contents", ""))
self.fs.write(event["path"], event.get("contents", ""))
if event["action"] == "READ":
return self.fs.read(event["path"])

Expand Down
Empty file added examples/tests/__init__.py
Empty file.
4 changes: 3 additions & 1 deletion examples/tests/test_00_consumer.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,11 @@

import pytest
import requests
from examples.src.consumer import User, UserConsumer
from pact import Consumer, Format, Like, Provider
from yarl import URL

from examples.src.consumer import User, UserConsumer

if TYPE_CHECKING:
from pathlib import Path

Expand Down Expand Up @@ -138,5 +139,6 @@ def test_get_unknown_user(pact: Pact, user_consumer: UserConsumer) -> None:
with pact:
with pytest.raises(requests.HTTPError) as excinfo:
user_consumer.get_user(123)
assert excinfo.value.response is not None
assert excinfo.value.response.status_code == HTTPStatus.NOT_FOUND
pact.verify()
7 changes: 5 additions & 2 deletions examples/tests/test_01_provider_fastapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,12 @@

import pytest
import uvicorn
from examples.src.fastapi import app
from pact import Verifier
from pydantic import BaseModel
from yarl import URL

from examples.src.fastapi import app

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


Expand Down Expand Up @@ -78,7 +79,9 @@ def run_server() -> None:
lambda cannot be used as the target of a `multiprocessing.Process` as it
cannot be pickled.
"""
uvicorn.run(app, host=PROVIDER_URL.host, port=PROVIDER_URL.port)
host = PROVIDER_URL.host if PROVIDER_URL.host else "localhost"
port = PROVIDER_URL.port if PROVIDER_URL.port else 8080
uvicorn.run(app, host=host, port=port)


@pytest.fixture(scope="module")
Expand Down
6 changes: 3 additions & 3 deletions examples/tests/test_01_provider_flask.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,19 @@
section of the Pact documentation.
"""


from __future__ import annotations

from multiprocessing import Process
from typing import Any, Dict, Generator, Union
from unittest.mock import MagicMock

import pytest
from examples.src.flask import app
from flask import request
from pact import Verifier
from yarl import URL

from examples.src.flask import app

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


Expand All @@ -58,7 +58,7 @@ async def mock_pact_provider_states() -> Dict[str, Union[str, None]]:
"user 123 doesn't exist": mock_user_123_doesnt_exist,
"user 123 exists": mock_user_123_exists,
}
return {"result": mapping[request.json["state"]]()}
return {"result": mapping[request.json["state"]]()} # type: ignore[index]


def run_server() -> None:
Expand Down
10 changes: 7 additions & 3 deletions examples/tests/test_02_message_consumer.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,10 @@
from unittest.mock import MagicMock

import pytest
from examples.src.message import Handler
from pact import MessageConsumer, MessagePact, Provider

from examples.src.message import Handler

if TYPE_CHECKING:
from pathlib import Path

Expand Down Expand Up @@ -116,7 +117,10 @@ def test_write_file(pact: MessagePact, handler: Handler) -> None:
)

result = handler.process(msg)
handler.fs.write.assert_called_once_with("test.txt", "Hello world!")
handler.fs.write.assert_called_once_with( # type: ignore[attr-defined]
"test.txt",
"Hello world!",
)
assert result is None


Expand All @@ -130,5 +134,5 @@ def test_read_file(pact: MessagePact, handler: Handler) -> None:
)

result = handler.process(msg)
handler.fs.read.assert_called_once_with("test.txt")
handler.fs.read.assert_called_once_with("test.txt") # type: ignore[attr-defined]
assert result == "Hello world!"
20 changes: 20 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -176,8 +176,28 @@ ignore = [
"ANN102", # `cls` must be typed
]

extend-exclude = [
"tests/*.py",
"pact/*.py",
]

[tool.ruff.pyupgrade]
keep-runtime-typing = true

[tool.ruff.pydocstyle]
convention = "google"

################################################################################
## Black Configuration
################################################################################

[tool.black]
target-version = ["py38"]
extend-exclude = '^/(pact|tests)/(?!v3).+\.py$'

################################################################################
## Mypy Configuration
################################################################################

[tool.mypy]
exclude = '^(pact|tests)/(?!v3).+\.py$'
Empty file added tests/__init__.py
Empty file.
Empty file added tests/v3/__init__.py
Empty file.
12 changes: 9 additions & 3 deletions tests/v3/test_http_interaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -407,7 +407,11 @@ def test_with_body_invalid(pact: Pact) -> None:
pact.upon_receiving("")
.with_request("GET", "/")
.will_respond_with(200)
.with_body(json.dumps({"request": True}), "application/json", "Invalid")
.with_body(
json.dumps({"request": True}),
"application/json",
"Invalid", # type: ignore[arg-type]
)
)


Expand Down Expand Up @@ -504,11 +508,13 @@ async def test_multipart_file_request(pact: Pact, temp_dir: Path) -> None:
with pact.serve() as srv, aiohttp.MultipartWriter() as mpwriter:
mpwriter.append(
fpy.open("rb"),
{"Content-Type": "text/x-python"},
# TODO: Remove type ignore once aio-libs/aiohttp#7741 is resolved
{"Content-Type": "text/x-python"}, # type: ignore[arg-type]
)
mpwriter.append(
fpng.open("rb"),
{"Content-Type": "image/png"},
# TODO: Remove type ignore once aio-libs/aiohttp#7741 is resolved
{"Content-Type": "image/png"}, # type: ignore[arg-type]
)

async with aiohttp.ClientSession(srv.url) as session, session.post(
Expand Down

0 comments on commit 8aeec66

Please sign in to comment.