From 58bb217a749aa9e8890e7f46ba78294330e64b07 Mon Sep 17 00:00:00 2001 From: Oz Tiram Date: Sun, 10 Dec 2023 15:16:27 +0100 Subject: [PATCH] Inherit Pipefile class from BaseModel --- src/plette/pipfiles.py | 23 ++++++++--------------- 1 file changed, 8 insertions(+), 15 deletions(-) diff --git a/src/plette/pipfiles.py b/src/plette/pipfiles.py index 8354c41..1f44e64 100644 --- a/src/plette/pipfiles.py +++ b/src/plette/pipfiles.py @@ -9,10 +9,12 @@ from .models import ( + BaseModel, Hash, Requires, PipfileSection, Pipenv, PackageCollection, ScriptCollection, SourceCollection, ) + def remove_empty_values(d): # Iterate over a copy of the dictionary for key, value in list(d.items()): @@ -26,6 +28,7 @@ def remove_empty_values(d): elif value is None or value == '': del d[key] + PIPFILE_SECTIONS = { "sources": SourceCollection, "packages": PackageCollection, @@ -43,8 +46,9 @@ def remove_empty_values(d): verify_ssl = true """ + @dataclass -class Pipfile: +class Pipfile(BaseModel): """Representation of a Pipfile.""" sources: SourceCollection packages: Optional[PackageCollection] = None @@ -55,19 +59,6 @@ class Pipfile: pipfile: Optional[PipfileSection] = None pipenv: Optional[Pipenv] = None - def __post_init__(self): - """Run validation methods if declared. - The validation method can be a simple check - that raises ValueError or a transformation to - the field value. - The validation is performed by calling a function named: - `validate_(self, value, field) -> field.type` - """ - - for name, field in self.__dataclass_fields__.items(): - if (method := getattr(self, f"validate_{name}", None)): - setattr(self, name, method(getattr(self, name), field=field)) - def validate_sources(self, value, field): if isinstance(value, list): return SourceCollection(value) @@ -76,6 +67,7 @@ def validate_sources(self, value, field): def validate_pipenv(self, value, field): if value is not None: return Pipenv(**value) + return value def validate_packages(self, value, field): PackageCollection(value) @@ -91,7 +83,8 @@ def to_dict(self): } data["_meta"].update(asdict(getattr(self, "sources", {}))) for category, values in self.__dict__.items(): - if category in PIPFILE_SECTIONS or category in ("default", "develop", "pipenv"): + if category in PIPFILE_SECTIONS or category in ( + "default", "develop", "pipenv"): continue data[category] = values remove_empty_values(data)