From cfa9f54d9fffe454b99c015c0d12e8aa239da8cc Mon Sep 17 00:00:00 2001 From: Francwa Date: Wed, 20 May 2026 23:47:03 +0200 Subject: [PATCH] refactor(shared): FilePath VO uses __post_init__ instead of custom __init__ MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Custom __init__ on a @dataclass(frozen=True) is a code smell — it bypasses the generated dataclass __init__ and re-implements the str/Path coercion + frozen-aware setattr by hand. Replaced with a single __post_init__ that performs the same normalization. Same public API (FilePath(str) and FilePath(Path) both work), same behavior, no callers touched. --- alfred/domain/shared/value_objects.py | 28 ++++++++++----------------- 1 file changed, 10 insertions(+), 18 deletions(-) diff --git a/alfred/domain/shared/value_objects.py b/alfred/domain/shared/value_objects.py index 74f8d11..30c74ee 100644 --- a/alfred/domain/shared/value_objects.py +++ b/alfred/domain/shared/value_objects.py @@ -43,29 +43,21 @@ class ImdbId: @dataclass(frozen=True) class FilePath: """ - Value object representing a file path with validation. + Value object representing a file path. - Ensures the path is valid and optionally checks existence. + Accepts either ``str`` or :class:`pathlib.Path` at construction; + the value is normalized to ``Path`` in ``__post_init__``. """ value: Path - def __init__(self, path: str | Path): - """ - Initialize FilePath. - - Args: - path: String or Path object representing the file path - """ - if isinstance(path, str): - path_obj = Path(path) - elif isinstance(path, Path): - path_obj = path - else: - raise ValidationError(f"Path must be str or Path, got {type(path)}") - - # Use object.__setattr__ because dataclass is frozen - object.__setattr__(self, "value", path_obj) + def __post_init__(self) -> None: + if isinstance(self.value, Path): + return + if isinstance(self.value, str): + object.__setattr__(self, "value", Path(self.value)) + return + raise ValidationError(f"Path must be str or Path, got {type(self.value)}") def __str__(self) -> str: return str(self.value)