Skip to content

Commit 3246453

Browse files
committed
Only use keyword arguments for HBARecord constructor
The values argument wasn't really useful. It also makes Mypy happy with the following syntax: HBARecord(**a_mapping) The previous required syntax was: HBARecord(values=a_mapping)
1 parent 55e970e commit 3246453

File tree

2 files changed

+27
-14
lines changed

2 files changed

+27
-14
lines changed

pgtoolkit/hba.py

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@
7373
import re
7474
import sys
7575
import warnings
76-
from collections.abc import Callable, Iterable, Iterator, Sequence
76+
from collections.abc import Callable, Iterable, Iterator
7777
from pathlib import Path
7878
from typing import IO, Any
7979

@@ -164,27 +164,19 @@ def parse(cls, line: str) -> HBARecord:
164164
# Remove extra outer double quotes for auth options values if any
165165
auth_options = [(o[0], re.sub(r"^\"|\"$", "", o[1])) for o in auth_options]
166166
options = base_options + auth_options
167-
return cls(options, comment=comment)
167+
return cls(**{k: v for k, v in options}, comment=comment)
168168

169169
conntype: str | None
170170
database: str
171171
user: str
172172

173-
def __init__(
174-
self,
175-
values: Iterable[tuple[str, str]] | dict[str, Any] | None = None,
176-
comment: str | None = None,
177-
**kw_values: str | Sequence[str],
178-
) -> None:
173+
def __init__(self, **kw_values: Any) -> None:
179174
"""
180-
:param values: A dict of fields.
181175
:param kw_values: Fields passed as keyword.
182-
:param comment: Comment at the end of the line.
183176
"""
184-
dict_values: dict[str, Any] = dict(values or {}, **kw_values)
185-
self.__dict__.update(dict_values)
186-
self.fields = [k for k, _ in dict_values.items()]
187-
self.comment = comment
177+
self.__dict__.update(kw_values)
178+
self.comment = kw_values.pop("comment", None)
179+
self.fields = [k for k, _ in kw_values.items()]
188180

189181
def __repr__(self) -> str:
190182
return "<{} {}{}>".format(

tests/test_hba.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,27 @@ def test_as_dict():
378378
}
379379

380380

381+
def test_hbarecord_constructor():
382+
from pgtoolkit.hba import HBARecord
383+
384+
HBARecord(
385+
conntype="local",
386+
database="all",
387+
user="all",
388+
method="trust",
389+
comment="This is comment!",
390+
)
391+
HBARecord(
392+
**dict(
393+
conntype="local",
394+
database="all",
395+
user="all",
396+
method="trust",
397+
comment="This is comment!",
398+
)
399+
)
400+
401+
381402
def test_hbarecord_equality():
382403
from pgtoolkit.hba import HBARecord
383404

0 commit comments

Comments
 (0)