Skip to content

Commit

Permalink
Only use keyword arguments for HBARecord constructor
Browse files Browse the repository at this point in the history
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)
  • Loading branch information
pgiraud committed Dec 5, 2024
1 parent 55e970e commit 3246453
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 14 deletions.
20 changes: 6 additions & 14 deletions pgtoolkit/hba.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@
import re
import sys
import warnings
from collections.abc import Callable, Iterable, Iterator, Sequence
from collections.abc import Callable, Iterable, Iterator
from pathlib import Path
from typing import IO, Any

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

conntype: str | None
database: str
user: str

def __init__(
self,
values: Iterable[tuple[str, str]] | dict[str, Any] | None = None,
comment: str | None = None,
**kw_values: str | Sequence[str],
) -> None:
def __init__(self, **kw_values: Any) -> None:
"""
:param values: A dict of fields.
:param kw_values: Fields passed as keyword.
:param comment: Comment at the end of the line.
"""
dict_values: dict[str, Any] = dict(values or {}, **kw_values)
self.__dict__.update(dict_values)
self.fields = [k for k, _ in dict_values.items()]
self.comment = comment
self.__dict__.update(kw_values)
self.comment = kw_values.pop("comment", None)
self.fields = [k for k, _ in kw_values.items()]

def __repr__(self) -> str:
return "<{} {}{}>".format(
Expand Down
21 changes: 21 additions & 0 deletions tests/test_hba.py
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,27 @@ def test_as_dict():
}


def test_hbarecord_constructor():
from pgtoolkit.hba import HBARecord

HBARecord(
conntype="local",
database="all",
user="all",
method="trust",
comment="This is comment!",
)
HBARecord(
**dict(
conntype="local",
database="all",
user="all",
method="trust",
comment="This is comment!",
)
)


def test_hbarecord_equality():
from pgtoolkit.hba import HBARecord

Expand Down

0 comments on commit 3246453

Please sign in to comment.