Skip to content

Commit 09e89c5

Browse files
committed
Convert config kept in TypedDict into a dataclass - closes #1225
1 parent f421006 commit 09e89c5

File tree

7 files changed

+47
-43
lines changed

7 files changed

+47
-43
lines changed

newsfragments/1225.feature.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Convert Config kept in TypedDict into a dataclass

pytest_postgresql/config.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
"""Plugin's configuration."""
22

3+
from dataclasses import dataclass
34
from pathlib import Path
4-
from typing import Any, TypedDict
5+
from typing import Any
56

67
from _pytest._py.path import LocalPath
78
from pytest import FixtureRequest
89

910

10-
class PostgresqlConfigDict(TypedDict):
11-
"""Typed Config dictionary."""
11+
@dataclass
12+
class PostgreSQLConfig:
13+
"""PostgreSQL Config."""
1214

1315
exec: str
1416
host: str
@@ -25,16 +27,16 @@ class PostgresqlConfigDict(TypedDict):
2527
drop_test_database: bool
2628

2729

28-
def get_config(request: FixtureRequest) -> PostgresqlConfigDict:
30+
def get_config(request: FixtureRequest) -> PostgreSQLConfig:
2931
"""Return a dictionary with config options."""
3032

3133
def get_postgresql_option(option: str) -> Any:
3234
name = "postgresql_" + option
3335
return request.config.getoption(name) or request.config.getini(name)
3436

35-
load_paths = detect_paths(get_postgresql_option("load"))
37+
load_paths: list[Path | str] = detect_paths(get_postgresql_option("load"))
3638

37-
return PostgresqlConfigDict(
39+
cfg = PostgreSQLConfig(
3840
exec=get_postgresql_option("exec"),
3941
host=get_postgresql_option("host"),
4042
port=get_postgresql_option("port"),
@@ -50,6 +52,7 @@ def get_postgresql_option(option: str) -> Any:
5052
postgres_options=get_postgresql_option("postgres_options"),
5153
drop_test_database=request.config.getoption("postgresql_drop_test_database"),
5254
)
55+
return cfg
5356

5457

5558
def detect_paths(load_paths: list[LocalPath | str]) -> list[Path | str]:

pytest_postgresql/factories/client.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ def postgresql_factory(request: FixtureRequest) -> Iterator[Connection]:
7070
password=pg_password,
7171
isolation_level=isolation_level,
7272
)
73-
if config["drop_test_database"]:
73+
if config.drop_test_database:
7474
janitor.drop()
7575
with janitor:
7676
db_connection: Connection = psycopg.connect(

pytest_postgresql/factories/noprocess.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -66,14 +66,14 @@ def postgresql_noproc_fixture(request: FixtureRequest) -> Iterator[NoopExecutor]
6666
:returns: tcp executor-like object
6767
"""
6868
config = get_config(request)
69-
pg_host = host or config["host"]
70-
pg_port = port or config["port"] or 5432
71-
pg_user = user or config["user"]
72-
pg_password = password or config["password"]
73-
pg_dbname = xdistify_dbname(dbname or config["dbname"])
74-
pg_options = options or config["options"]
75-
pg_load = load or config["load"]
76-
drop_test_database = config["drop_test_database"]
69+
pg_host = host or config.host
70+
pg_port = port or config.port or 5432
71+
pg_user = user or config.user
72+
pg_password = password or config.password
73+
pg_dbname = xdistify_dbname(dbname or config.dbname)
74+
pg_options = options or config.options
75+
pg_load = load or config.load
76+
drop_test_database = config.drop_test_database
7777

7878
noop_exec = NoopExecutor(
7979
host=pg_host,

pytest_postgresql/factories/process.py

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -28,17 +28,17 @@
2828
from port_for import PortForException, get_port
2929
from pytest import FixtureRequest, TempPathFactory
3030

31-
from pytest_postgresql.config import PostgresqlConfigDict, get_config
31+
from pytest_postgresql.config import PostgreSQLConfig, get_config
3232
from pytest_postgresql.exceptions import ExecutableMissingException
3333
from pytest_postgresql.executor import PostgreSQLExecutor
3434
from pytest_postgresql.janitor import DatabaseJanitor
3535

3636
PortType = port_for.PortType # mypy requires explicit export
3737

3838

39-
def _pg_exe(executable: str | None, config: PostgresqlConfigDict) -> str:
39+
def _pg_exe(executable: str | None, config: PostgreSQLConfig) -> str:
4040
"""If executable is set, use it. Otherwise best effort to find the executable."""
41-
postgresql_ctl = executable or config["exec"]
41+
postgresql_ctl = executable or config.exec
4242
# check if that executable exists, as it's no on systems' PATH
4343
# only replace it if executable isn't passed manually
4444
if not os.path.exists(postgresql_ctl) and executable is None:
@@ -50,9 +50,9 @@ def _pg_exe(executable: str | None, config: PostgresqlConfigDict) -> str:
5050
return postgresql_ctl
5151

5252

53-
def _pg_port(port: PortType | None, config: PostgresqlConfigDict, excluded_ports: Iterable[int]) -> int:
53+
def _pg_port(port: PortType | None, config: PostgreSQLConfig, excluded_ports: Iterable[int]) -> int:
5454
"""User specified port, otherwise find an unused port from config."""
55-
pg_port = get_port(port, excluded_ports) or get_port(config["port"], excluded_ports)
55+
pg_port = get_port(port, excluded_ports) or get_port(config.port, excluded_ports)
5656
assert pg_port is not None
5757
return pg_port
5858

@@ -115,8 +115,8 @@ def postgresql_proc_fixture(
115115
:returns: tcp executor
116116
"""
117117
config = get_config(request)
118-
pg_dbname = dbname or config["dbname"]
119-
pg_load = load or config["load"]
118+
pg_dbname = dbname or config.dbname
119+
pg_load = load or config.load
120120
postgresql_ctl = _pg_exe(executable, config)
121121
port_path = tmp_path_factory.getbasetemp()
122122
if hasattr(request.config, "workerinput"):
@@ -138,7 +138,7 @@ def postgresql_proc_fixture(
138138
port_file.write(f"pg_port {pg_port}\n")
139139
break
140140
except FileExistsError:
141-
if n >= config["port_search_count"]:
141+
if n >= config.port_search_count:
142142
raise PortForException(
143143
f"Attempted {n} times to select ports. "
144144
f"All attempted ports: {', '.join(map(str, used_ports))} are already "
@@ -151,17 +151,17 @@ def postgresql_proc_fixture(
151151

152152
postgresql_executor = PostgreSQLExecutor(
153153
executable=postgresql_ctl,
154-
host=host or config["host"],
154+
host=host or config.host,
155155
port=pg_port,
156-
user=user or config["user"],
157-
password=password or config["password"],
156+
user=user or config.user,
157+
password=password or config.password,
158158
dbname=pg_dbname,
159-
options=options or config["options"],
159+
options=options or config.options,
160160
datadir=str(datadir),
161-
unixsocketdir=unixsocketdir or config["unixsocketdir"],
161+
unixsocketdir=unixsocketdir or config.unixsocketdir,
162162
logfile=str(logfile_path),
163-
startparams=startparams or config["startparams"],
164-
postgres_options=postgres_options or config["postgres_options"],
163+
startparams=startparams or config.startparams,
164+
postgres_options=postgres_options or config.postgres_options,
165165
)
166166
# start server
167167
with postgresql_executor:
@@ -174,7 +174,7 @@ def postgresql_proc_fixture(
174174
version=postgresql_executor.version,
175175
password=postgresql_executor.password,
176176
)
177-
if config["drop_test_database"]:
177+
if config.drop_test_database:
178178
janitor.drop()
179179
with janitor:
180180
for load_element in pg_load:

tests/examples/test_assert_port_search_count_is_ten.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,4 @@
1111
def test_assert_port_search_count_is_ten(request: FixtureRequest) -> None:
1212
"""Asserts that port_search_count is 10."""
1313
config = get_config(request)
14-
assert config["port_search_count"] == 10
14+
assert config.port_search_count == 10

tests/test_executor.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -51,16 +51,16 @@ def version(self) -> Any:
5151
def test_unsupported_version(request: FixtureRequest) -> None:
5252
"""Check that the error gets raised on unsupported postgres version."""
5353
config = get_config(request)
54-
port = get_port(config["port"])
54+
port = get_port(config.port)
5555
assert port is not None
5656
executor = PatchedPostgreSQLExecutor(
57-
executable=config["exec"],
58-
host=config["host"],
57+
executable=config.exec,
58+
host=config.host,
5959
port=port,
6060
datadir="/tmp/error",
61-
unixsocketdir=config["unixsocketdir"],
61+
unixsocketdir=config.unixsocketdir,
6262
logfile="/tmp/version.error.log",
63-
startparams=config["startparams"],
63+
startparams=config.startparams,
6464
dbname="random_name",
6565
)
6666

@@ -84,12 +84,12 @@ def test_executor_init_with_password(
8484
datadir, logfile_path = process._prepare_dir(tmpdir, port)
8585
executor = PostgreSQLExecutor(
8686
executable=pg_exe,
87-
host=config["host"],
87+
host=config.host,
8888
port=port,
8989
datadir=str(datadir),
90-
unixsocketdir=config["unixsocketdir"],
90+
unixsocketdir=config.unixsocketdir,
9191
logfile=str(logfile_path),
92-
startparams=config["startparams"],
92+
startparams=config.startparams,
9393
password="somepassword",
9494
dbname="somedatabase",
9595
)
@@ -109,12 +109,12 @@ def test_executor_init_bad_tmp_path(
109109
datadir, logfile_path = process._prepare_dir(tmpdir, port)
110110
executor = PostgreSQLExecutor(
111111
executable=pg_exe,
112-
host=config["host"],
112+
host=config.host,
113113
port=port,
114114
datadir=str(datadir),
115-
unixsocketdir=config["unixsocketdir"],
115+
unixsocketdir=config.unixsocketdir,
116116
logfile=str(logfile_path),
117-
startparams=config["startparams"],
117+
startparams=config.startparams,
118118
password="some password",
119119
dbname="some database",
120120
)

0 commit comments

Comments
 (0)