fix(community): use __post_init__ coercion, source_product store arg, add package-data for SQL files
Some checks are pending
CI / test (push) Waiting to run
Mirror / mirror (push) Waiting to run

This commit is contained in:
pyr0ball 2026-04-12 22:24:13 -07:00
parent 0598801aaa
commit 56fb6be4b1
3 changed files with 17 additions and 16 deletions

View file

@ -66,25 +66,22 @@ class CommunityPost:
protein_pct: float | None
moisture_pct: float | None
def __init__(self, **kwargs):
# 1. Coerce list fields to tuples
def __post_init__(self) -> None:
# Coerce list fields to tuples (frozen dataclass: use object.__setattr__)
for key in ("slots", "dietary_tags", "allergen_flags", "flavor_molecules"):
if key in kwargs and isinstance(kwargs[key], list):
kwargs[key] = tuple(kwargs[key])
val = getattr(self, key)
if isinstance(val, list):
object.__setattr__(self, key, tuple(val))
# 2. Validate BEFORE assignment
post_type = kwargs.get("post_type")
if post_type not in _VALID_POST_TYPES:
# Validate post_type
if self.post_type not in _VALID_POST_TYPES:
raise ValueError(
f"post_type must be one of {sorted(_VALID_POST_TYPES)}, got {post_type!r}"
f"post_type must be one of {sorted(_VALID_POST_TYPES)}, got {self.post_type!r}"
)
# Validate scores
for score_name in (
"seasoning_score", "richness_score", "brightness_score",
"depth_score", "aroma_score", "structure_score",
):
if score_name in kwargs:
_validate_score(score_name, kwargs[score_name])
# 3. Assign all fields
for f in self.__dataclass_fields__:
object.__setattr__(self, f, kwargs[f])
_validate_score(score_name, getattr(self, score_name))

View file

@ -66,8 +66,9 @@ class SharedStore:
All methods return new objects (immutable pattern). Never mutate rows in-place.
"""
def __init__(self, db: "CommunityDB") -> None:
def __init__(self, db: "CommunityDB", source_product: str = "kiwi") -> None:
self._db = db
self._source_product = source_product
# ------------------------------------------------------------------
# Reads
@ -176,7 +177,7 @@ class SharedStore:
json.dumps(list(post.allergen_flags)),
json.dumps(list(post.flavor_molecules)),
post.fat_pct, post.protein_pct, post.moisture_pct,
"kiwi",
self._source_product,
),
)
conn.commit()

View file

@ -84,6 +84,9 @@ cf-manage = "circuitforge_core.manage.cli:app"
where = ["."]
include = ["circuitforge_core*"]
[tool.setuptools.package-data]
"circuitforge_core.community.migrations" = ["*.sql"]
[tool.pytest.ini_options]
testpaths = ["tests"]
asyncio_mode = "auto"