Skip to content

Commit f0dc05a

Browse files
authored
Merge pull request #1431 from dbfixtures/issue-1397
Enable PT ruff rulesets - closes #1397
2 parents c529ae8 + 89bb53b commit f0dc05a

16 files changed

Lines changed: 86 additions & 97 deletions

newsfragments/1397.misc.1.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
``postgresql_proc`` now tears down through a fixture ``yield`` instead of ``request.addfinalizer``.
2+
Consuming it as a pytest fixture is unaffected.

newsfragments/1397.misc.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Enabled Ruff's `PT <https://docs.astral.sh/ruff/rules/#flake8-pytest-style-pt>`_ rules
2+
to keep pytest usage consistent across the plugin and its test suite.

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ select = [
8181
"I", # isort
8282
"D", # pydocstyle
8383
"FBT", # flake8-boolean-trap
84+
"PT", # flake8-pytest-style
8485
]
8586

8687
[tool.pyproject-fmt]

pytest_postgresql/config.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
from pathlib import Path
55
from typing import Any
66

7+
import pytest
78
from _pytest._py.path import LocalPath
8-
from pytest import FixtureRequest
99

1010

1111
@dataclass(frozen=True)
@@ -29,7 +29,7 @@ class PostgreSQLConfig:
2929
drop_test_database: bool
3030

3131

32-
def get_config(request: FixtureRequest) -> PostgreSQLConfig:
32+
def get_config(request: pytest.FixtureRequest) -> PostgreSQLConfig:
3333
"""Return a PostgreSQLConfig instance with configuration options."""
3434

3535
def get_postgresql_option(option: str) -> Any:

pytest_postgresql/factories/client.py

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
import psycopg
2323
import pytest
2424
from psycopg import AsyncConnection, Connection
25-
from pytest import FixtureRequest
2625

2726
from pytest_postgresql._asyncio_compat import mark_postgresql_async_fixture, supports_loop_factories
2827
from pytest_postgresql.config import get_config
@@ -37,11 +36,11 @@
3736
pytest_asyncio = _pytest_asyncio
3837

3938

40-
def _postgresql_async_unavailable_stub() -> Callable[[FixtureRequest], AsyncIterator[AsyncConnection]]:
39+
def _postgresql_async_unavailable_stub() -> Callable[[pytest.FixtureRequest], AsyncIterator[AsyncConnection]]:
4140
"""Return a sync fixture stub that raises when pytest-asyncio is missing or too old."""
4241

4342
@pytest.fixture
44-
def postgresql_async_stub(request: FixtureRequest) -> None:
43+
def postgresql_async_stub(request: pytest.FixtureRequest) -> None:
4544
"""Sync stub that raises ImportError when pytest-asyncio is absent or too old."""
4645
raise ImportError(
4746
"pytest-asyncio >= 1.4 is required for async fixtures. "
@@ -50,7 +49,7 @@ def postgresql_async_stub(request: FixtureRequest) -> None:
5049

5150
mark_postgresql_async_fixture(postgresql_async_stub)
5251
return cast(
53-
Callable[[FixtureRequest], AsyncIterator[AsyncConnection]],
52+
Callable[[pytest.FixtureRequest], AsyncIterator[AsyncConnection]],
5453
postgresql_async_stub,
5554
)
5655

@@ -59,7 +58,7 @@ def postgresql(
5958
process_fixture_name: str,
6059
dbname: str | None = None,
6160
isolation_level: "psycopg.IsolationLevel | None" = None,
62-
) -> Callable[[FixtureRequest], Iterator[Connection]]:
61+
) -> Callable[[pytest.FixtureRequest], Iterator[Connection]]:
6362
"""Return connection fixture factory for PostgreSQL.
6463
6564
:param process_fixture_name: name of the process fixture
@@ -70,7 +69,7 @@ def postgresql(
7069
"""
7170

7271
@pytest.fixture
73-
def postgresql_factory(request: FixtureRequest) -> Iterator[Connection]:
72+
def postgresql_factory(request: pytest.FixtureRequest) -> Iterator[Connection]:
7473
"""Fixture connection factory for PostgreSQL.
7574
7675
:param request: fixture request object
@@ -120,7 +119,7 @@ def postgresql_async(
120119
process_fixture_name: str,
121120
dbname: str | None = None,
122121
isolation_level: "psycopg.IsolationLevel | None" = None,
123-
) -> Callable[[FixtureRequest], AsyncIterator[AsyncConnection]]:
122+
) -> Callable[[pytest.FixtureRequest], AsyncIterator[AsyncConnection]]:
124123
"""Return async connection fixture factory for PostgreSQL.
125124
126125
Requires ``pytest-asyncio`` >= 1.4 (install via ``pip install pytest-postgresql[async]``).
@@ -135,7 +134,7 @@ def postgresql_async(
135134
return _postgresql_async_unavailable_stub()
136135

137136
@pytest_asyncio.fixture
138-
async def postgresql_async_factory(request: FixtureRequest) -> AsyncIterator[AsyncConnection]:
137+
async def postgresql_async_factory(request: pytest.FixtureRequest) -> AsyncIterator[AsyncConnection]:
139138
"""Async connection fixture factory for PostgreSQL.
140139
141140
:param request: fixture request object
@@ -180,6 +179,6 @@ async def postgresql_async_factory(request: FixtureRequest) -> AsyncIterator[Asy
180179

181180
mark_postgresql_async_fixture(postgresql_async_factory)
182181
return cast(
183-
Callable[[FixtureRequest], AsyncIterator[AsyncConnection]],
182+
Callable[[pytest.FixtureRequest], AsyncIterator[AsyncConnection]],
184183
postgresql_async_factory,
185184
)

pytest_postgresql/factories/noprocess.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
from typing import Callable, Iterator
2323

2424
import pytest
25-
from pytest import FixtureRequest
2625

2726
from pytest_postgresql.config import get_config
2827
from pytest_postgresql.executors import NoopExecutor
@@ -49,7 +48,7 @@ def postgresql_noproc(
4948
load: list[Callable | str | Path] | None = None,
5049
load_autocommit: bool | None = None,
5150
depends_on: str | None = None,
52-
) -> Callable[[FixtureRequest], Iterator[NoopExecutor]]:
51+
) -> Callable[[pytest.FixtureRequest], Iterator[NoopExecutor]]:
5352
"""Postgresql noprocess factory.
5453
5554
:param host: hostname
@@ -72,7 +71,7 @@ def postgresql_noproc(
7271
"""
7372

7473
@pytest.fixture(scope="session")
75-
def postgresql_noproc_fixture(request: FixtureRequest) -> Iterator[NoopExecutor]:
74+
def postgresql_noproc_fixture(request: pytest.FixtureRequest) -> Iterator[NoopExecutor]:
7675
"""Noop Process fixture for PostgreSQL.
7776
7877
:param request: fixture request object

pytest_postgresql/factories/process.py

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,11 @@
2323
import platform
2424
import tempfile
2525
from pathlib import Path
26-
from typing import Callable, Iterable
26+
from typing import Callable, Iterable, Iterator
2727

2828
import port_for
2929
import pytest
3030
from port_for import PortForException, get_port
31-
from pytest import FixtureRequest, TempPathFactory
3231

3332
from pytest_postgresql.config import PostgreSQLConfig, get_config
3433
from pytest_postgresql.executors import PostgreSQLExecutor
@@ -81,7 +80,7 @@ def postgresql_proc(
8180
postgres_options: str | None = None,
8281
load: list[Callable | str | Path] | None = None,
8382
load_autocommit: bool | None = None,
84-
) -> Callable[[FixtureRequest, TempPathFactory], PostgreSQLExecutor]:
83+
) -> Callable[[pytest.FixtureRequest, pytest.TempPathFactory], Iterator[PostgreSQLExecutor]]:
8584
"""Postgresql process factory.
8685
8786
:param executable: path to postgresql_ctl
@@ -108,7 +107,10 @@ def postgresql_proc(
108107
"""
109108

110109
@pytest.fixture(scope="session")
111-
def postgresql_proc_fixture(request: FixtureRequest, tmp_path_factory: TempPathFactory) -> PostgreSQLExecutor:
110+
def postgresql_proc_fixture(
111+
request: pytest.FixtureRequest,
112+
tmp_path_factory: pytest.TempPathFactory,
113+
) -> Iterator[PostgreSQLExecutor]:
112114
"""Process fixture for PostgreSQL.
113115
114116
:param request: fixture request object
@@ -221,16 +223,9 @@ def _cleanup_executor_resources() -> None:
221223
for load_element in pg_load:
222224
janitor.load(load_element)
223225

224-
def cleanup() -> None:
225-
try:
226-
janitor.drop()
227-
finally:
228-
_cleanup_executor_resources()
229-
230-
request.addfinalizer(cleanup)
231-
return postgresql_executor
232-
except Exception:
226+
yield postgresql_executor
227+
janitor.drop()
228+
finally:
233229
_cleanup_executor_resources()
234-
raise
235230

236231
return postgresql_proc_fixture

tests/examples/test_assert_load_autocommit_is_true.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@
33
That shows that it is not the default (False), and that we parsed it as a bool.
44
"""
55

6-
from pytest import FixtureRequest
6+
import pytest
77

88
from pytest_postgresql.config import get_config
99

1010

11-
def test_assert_load_autocommit_is_true(request: FixtureRequest) -> None:
11+
def test_assert_load_autocommit_is_true(request: pytest.FixtureRequest) -> None:
1212
"""Asserts that load_autocommit is True."""
1313
config = get_config(request)
1414
assert config.load_autocommit is True

tests/examples/test_assert_port_search_count_is_ten.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@
33
That shows that it is not the default (5), and that we parsed it as an integer.
44
"""
55

6-
from pytest import FixtureRequest
6+
import pytest
77

88
from pytest_postgresql.config import get_config
99

1010

11-
def test_assert_port_search_count_is_ten(request: FixtureRequest) -> None:
11+
def test_assert_port_search_count_is_ten(request: pytest.FixtureRequest) -> None:
1212
"""Asserts that port_search_count is 10."""
1313
config = get_config(request)
1414
assert config.port_search_count == 10

tests/test_config.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@
99

1010

1111
@pytest.mark.parametrize(
12-
"path, want",
13-
(
12+
("path", "want"),
13+
[
1414
("test.sql", Path("test.sql")),
1515
("load.function", "load.function"),
1616
(LocalPath("test.sql"), Path("test.sql").absolute()), # type: ignore[no-untyped-call]
17-
),
17+
],
1818
)
1919
def test_detect_paths(path: str | LocalPath, want: Path | str) -> None:
2020
"""Check the correctness of detect_paths function."""

0 commit comments

Comments
 (0)