Skip to content

Commit ed53ae3

Browse files
committed
chore: clear remaining ruff lint failures
Adds tests/* per-file ruff ignores for rules that legitimately differ between test and production code (PLC0415 lazy imports, PLR2004 magic status codes, ARG001/ARG002 fixture parameters, TC002/TC003 deferred typing imports, ANN401/ANN202, EM101/TRY003 short literal exceptions in test routes, N802 for stdlib-style camelCase methods on test fakes that mirror logging.Handler). Applies the user-authorised stylistic fixes to production source so ``ruff check src/ tests/`` passes cleanly: * src/ledgerbase/__init__.py: switch ``os.path`` calls to ``pathlib`` (PTH100/PTH118/PTH120/PTH112), assign the ValueError message to a variable (EM101/TRY003), and drop the commented-out SECRET_KEY line (ERA001). * src/ledgerbase/config.py: drop the ``#!/usr/bin/env python`` shebang on this importable, non-executable module (EXE001). Linter-applied autofixes also tidied a handful of test-file imports (unused-import, unsorted-imports, manual-from-import, etc.) and ran ``ruff format`` over the test/src trees. Verified locally: ``ruff check src/ tests/`` is green, ``ruff format --check src/ tests/`` is green, ``pytest tests/`` passes 72/72 with 95 % line / 87 % branch coverage on the targeted modules.
1 parent 17784a9 commit ed53ae3

8 files changed

Lines changed: 49 additions & 18 deletions

File tree

pyproject.toml

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,34 @@ exclude = [
187187
# file‑specific overrides
188188
[tool.ruff.lint.per-file-ignores]
189189
"annotation_spec.md" = ["E501"]
190+
"tests/*" = [
191+
# Tests legitimately use literal status codes / sizes (200, 422, 7…) so
192+
# mandating named constants would just obscure intent.
193+
"PLR2004",
194+
# Test fixtures often defer imports to inside test bodies (to avoid
195+
# collection-time side-effects, to enable monkeypatching, or to keep
196+
# test isolation clean). Module-level-only imports don't fit that need.
197+
"PLC0415",
198+
# Pytest fixtures appear as unused arguments to the test function but
199+
# are required for the dependency-injection wiring -- they're "used"
200+
# by being requested.
201+
"ARG001",
202+
"ARG002",
203+
# Tests do not need a strict typing-only import block.
204+
"TC002",
205+
"TC003",
206+
# ``pytest.MonkeyPatch`` parameter etc. -- ANN401 is excessive in tests.
207+
"ANN401",
208+
"ANN202",
209+
# Marshmallow ValidationError is constructed in test routes with a
210+
# short literal message.
211+
"EM101",
212+
"TRY003",
213+
# Test fakes must mirror the third-party interface they substitute
214+
# (e.g. ``logging.Handler.setLevel``) and so cannot be renamed to
215+
# snake_case without breaking duck-typing.
216+
"N802",
217+
]
190218

191219

192220
[tool.ruff.lint.isort]

src/ledgerbase/__init__.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import os
2+
from pathlib import Path
23

34
from dotenv import load_dotenv
45
from flask_sqlalchemy import SQLAlchemy
@@ -35,23 +36,24 @@
3536

3637
def create_app() -> Flask:
3738
"""Application factory function."""
38-
project_root = os.path.abspath(os.path.join(os.path.dirname(__file__), ".."))
39-
template_dir = os.path.join(project_root, "templates")
39+
project_root = Path(__file__).resolve().parent.parent
40+
template_dir = project_root / "templates"
4041

41-
if not os.path.isdir(template_dir):
42+
if not template_dir.is_dir():
4243
print(f"Warning: Template directory not found at {template_dir}")
4344
app = Flask(__name__)
4445
else:
45-
app = Flask(__name__, template_folder=template_dir)
46+
app = Flask(__name__, template_folder=str(template_dir))
4647

4748
app.config["SQLALCHEMY_DATABASE_URI"] = os.getenv(
48-
"DATABASE_URL", "sqlite:///default.db"
49+
"DATABASE_URL",
50+
"sqlite:///default.db",
4951
)
5052
if not app.config["SQLALCHEMY_DATABASE_URI"]:
51-
raise ValueError("DATABASE_URL environment variable is not set.")
53+
msg = "DATABASE_URL environment variable is not set."
54+
raise ValueError(msg)
5255

5356
app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False
54-
# app.config["SECRET_KEY"] = os.getenv("SECRET_KEY", "default-secret-key")
5557

5658
# Initialize core services and middleware
5759
db.init_app(app)

src/ledgerbase/config.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
#!/usr/bin/env python
21
"""---
32
# Front-Matter for Python Module
43

tests/app_factory_test.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,8 @@ def test_sentry_dsn_absent_branch_prints_notice(
111111
) -> None:
112112
"""Reproduce the ``SENTRY_DSN not found`` notice path without mutating the
113113
real ``ledgerbase`` package (which would corrupt SQLAlchemy registry state
114-
for other tests)."""
114+
for other tests).
115+
"""
115116
monkeypatch.delenv("SENTRY_DSN", raising=False)
116117

117118
# Mirror the inline branch in ``ledgerbase/__init__.py`` so we cover the

tests/conftest.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,17 @@
22

33
from __future__ import annotations
44

5+
from collections.abc import Iterator
56
from pathlib import Path
6-
from typing import TYPE_CHECKING, Iterator
7+
from typing import TYPE_CHECKING
78

89
import pytest
910

1011
if TYPE_CHECKING:
11-
from flask import Flask
1212
from flask.testing import FlaskClient
1313

14+
from flask import Flask
15+
1416

1517
PROJECT_ROOT = Path(__file__).resolve().parent.parent
1618
TEMPLATES_DIR = PROJECT_ROOT / "templates"
@@ -29,11 +31,10 @@ def app(monkeypatch: pytest.MonkeyPatch) -> Iterator[Flask]:
2931

3032
monkeypatch.setattr(_ledger_pkg, "configure_logging", lambda _app: None)
3133

32-
from ledgerbase import create_app, db
33-
3434
# Ensure ``ExampleModel`` is registered against the current ``db.metadata``
3535
# so ``db.create_all()`` actually creates its table.
3636
import ledgerbase.models # noqa: F401
37+
from ledgerbase import create_app, db
3738

3839
flask_app = create_app()
3940
flask_app.config.update(TESTING=True)

tests/error_handlers_full_test.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
from typing import TYPE_CHECKING
66

7-
import pytest
87
from marshmallow import ValidationError
98
from werkzeug.exceptions import InternalServerError, NotFound
109

tests/plaid_service_test.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
import pytest
1919
import requests
2020

21-
import services.plaid_service as plaid_service
21+
from services import plaid_service
2222
from services.plaid_service import (
2323
PLAID_BASE_URLS,
2424
create_link_token,
@@ -105,7 +105,8 @@ def test_plaid_request_returns_none_on_http_error(
105105
) -> None:
106106
"""An HTTP error (e.g. token expiry 401) returns None and prints diagnostics."""
107107
bad_response = _make_response(
108-
{"error_code": "INVALID_ACCESS_TOKEN"}, status_code=401
108+
{"error_code": "INVALID_ACCESS_TOKEN"},
109+
status_code=401,
109110
)
110111
with patch.object(plaid_service.requests, "post", return_value=bad_response):
111112
result = plaid_request("/endpoint", {"access_token": "expired"})

tests/security_full_test.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
from __future__ import annotations
44

55
import logging
6-
from logging.handlers import RotatingFileHandler
76
from pathlib import Path
87
from typing import TYPE_CHECKING
98

@@ -102,7 +101,8 @@ def test_configure_logging_skips_setup_in_testing_mode() -> None:
102101

103102

104103
def test_configure_logging_attaches_file_handler_in_production(
105-
monkeypatch: pytest.MonkeyPatch, tmp_path: Path
104+
monkeypatch: pytest.MonkeyPatch,
105+
tmp_path: Path,
106106
) -> None:
107107
"""Non-debug, non-test apps get a RotatingFileHandler installed."""
108108
import ledgerbase.security as security_mod

0 commit comments

Comments
 (0)