Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -37,21 +37,21 @@ repos:
- id: rst-inline-touching-normal

- repo: https://github.com/rbubley/mirrors-prettier
rev: "v3.9.4"
rev: "v3.9.9"
hooks:
- id: prettier
types_or: [yaml, markdown, html, css, scss, javascript, json]
args: [--prose-wrap=always]

- repo: https://github.com/astral-sh/ruff-pre-commit
rev: "v0.15.20"
rev: "v0.16.10"
hooks:
- id: ruff
- id: ruff-check
args: ["--fix", "--show-fixes"]
- id: ruff-format

- repo: https://github.com/pre-commit/mirrors-mypy
rev: "v2.1.0"
rev: "v2.4.0"
hooks:
- id: mypy
files: src|tests
Expand All @@ -61,12 +61,12 @@ repos:
- pytest

- repo: https://github.com/codespell-project/codespell
rev: "v2.4.2"
rev: "v2.4.3"
hooks:
- id: codespell

- repo: https://github.com/shellcheck-py/shellcheck-py
rev: "v0.11.0.1"
rev: "v0.11.0.1-1"
hooks:
- id: shellcheck

Expand All @@ -79,13 +79,13 @@ repos:
exclude: .pre-commit-config.yaml

- repo: https://github.com/abravalheri/validate-pyproject
rev: "v0.25"
rev: "0.26"
hooks:
- id: validate-pyproject
additional_dependencies: ["validate-pyproject-schema-store[all]"]

- repo: https://github.com/python-jsonschema/check-jsonschema
rev: "0.37.4"
rev: "0.38.2"
hooks:
- id: check-dependabot
- id: check-github-workflows
Expand Down
2 changes: 0 additions & 2 deletions src/dbetto/_version.pyi
Original file line number Diff line number Diff line change
@@ -1,4 +1,2 @@
from __future__ import annotations

version: str
version_tuple: tuple[int, int, int] | tuple[int, int, int, str, str]
19 changes: 12 additions & 7 deletions src/dbetto/attrsdict.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@

import logging
from collections.abc import Hashable
from typing import Any
from typing import TYPE_CHECKING, Any

if TYPE_CHECKING:
from typing_extensions import Self

log = logging.getLogger(__name__)

Expand All @@ -36,7 +39,7 @@ class AttrsDict(dict):
1
"""

def __new__(cls, *_, **__) -> AttrsDict:
def __new__(cls, *_, **__) -> Self:
"""Create a new instance of AttrsDict."""
instance = super().__new__(cls)
super(AttrsDict, instance).__setattr__("__readonly__", False)
Expand Down Expand Up @@ -70,7 +73,7 @@ def __init__(self, value: dict | None = None, readonly: bool = False) -> None:
# attribute that holds cached remappings -- see map()
self.__readonly__ = readonly

def __setitem__(self, key: str | int | float, value: Any) -> Any:
def __setitem__(self, key: str | float, value: Any) -> Any:
if self.__readonly__:
msg = "this AttrsDict is read-only"
raise TypeError(msg)
Expand Down Expand Up @@ -208,10 +211,10 @@ def map(self, label: str, unique: bool = True) -> AttrsDict:

if not isinstance(newid, Hashable):
msg = f"'{label}' values are not all hashable"
raise RuntimeError(msg)
raise TypeError(msg)

if newid in newmap:
newkey = sorted(newmap[newid].keys())[-1] + 1
newkey = max(newmap[newid].keys()) + 1
newmap[newid].update({newkey: v})
unique_tracker = False
else:
Expand Down Expand Up @@ -279,11 +282,13 @@ def group(self, label: str) -> AttrsDict:
return self.map(label, unique=False)

# d |= other_d should still produce a valid AttrsDict
def __ior__(self, other: dict | AttrsDict) -> AttrsDict:
def __ior__(self, other: dict | AttrsDict) -> Self:
if self.__readonly__:
msg = "this AttrsDict is read-only"
raise TypeError(msg)
return AttrsDict(super().__ior__(other))
for key, value in other.items():
self[key] = value
return self

# d1 | d2 should still produce a valid AttrsDict
def __or__(self, other: dict | AttrsDict) -> AttrsDict:
Expand Down
6 changes: 4 additions & 2 deletions src/dbetto/catalog.py
Original file line number Diff line number Diff line change
Expand Up @@ -265,8 +265,10 @@ def write_to(self, file_name: str | Path) -> None:
if ext == ".jsonl":
with Path(file_name).open("w", encoding="utf-8") as file:
for category, entries in self.entries.items():
for entry in entries:
file.write(json.dumps(entry.save_format(category)) + "\n")
file.writelines(
json.dumps(entry.save_format(category)) + "\n"
for entry in entries
)
else:
utils.write_dict(self.get_dict_format(), file_name)

Expand Down
9 changes: 7 additions & 2 deletions tests/test_textdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -297,11 +297,16 @@ def test_modification():

def test_merging():
d = AttrsDict({"a": 1})
d |= {"b": 2}
assert d == {"a": 1, "b": 2}
d_alias = d
d |= {"b": 2, "e": {"f": 5}}
assert d is d_alias
assert d == {"a": 1, "b": 2, "e": {"f": 5}}
assert isinstance(d, AttrsDict)
assert hasattr(d, "a")
assert hasattr(d, "b")
assert isinstance(d.e, AttrsDict)
assert d.e.f == 5
del d["e"]

d2 = d | {"c": 3}
assert isinstance(d2, AttrsDict)
Expand Down
Loading