refactor(shared): FilePath VO uses __post_init__ instead of custom __init__

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.
This commit is contained in:
2026-05-20 23:47:03 +02:00
parent f0aaf50c97
commit cfa9f54d9f
+10 -18
View File
@@ -43,29 +43,21 @@ class ImdbId:
@dataclass(frozen=True) @dataclass(frozen=True)
class FilePath: 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 value: Path
def __init__(self, path: str | Path): def __post_init__(self) -> None:
""" if isinstance(self.value, Path):
Initialize FilePath. return
if isinstance(self.value, str):
Args: object.__setattr__(self, "value", Path(self.value))
path: String or Path object representing the file path return
""" raise ValidationError(f"Path must be str or Path, got {type(self.value)}")
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 __str__(self) -> str: def __str__(self) -> str:
return str(self.value) return str(self.value)