Skip to content

Commit

Permalink
move validate from classmethod to instance method (#147)
Browse files Browse the repository at this point in the history
* move validate from classmethod to instance method

Co-authored-by: Preston Hartzell <preston.hartzell@gmail.com>
  • Loading branch information
ircwaves and pjhartzell authored Sep 16, 2024
1 parent eaca640 commit 6198c8b
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 13 deletions.
9 changes: 8 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

## [Unreleased]

### ⚠️ Breaking Change

- ([#147](https://github.com/stac-utils/stac-task/pull/147)) Moved `Task.validate` from class method to instance method, availing
implementers of other instance convenience methods (i.e. `self.parameters`).

## [0.5.1] - 2024-05-23

### Added
Expand Down Expand Up @@ -111,7 +118,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

Initial release.

<!-- [unreleased]: <https://github.com/stac-utils/stac-task/compare/v0.4.2...main> -->
[unreleased]: <https://github.com/stac-utils/stac-task/compare/v0.5.1...main>
[0.5.1]: <https://github.com/stac-utils/stac-task/compare/v0.5.0...v0.5.1>
[0.5.0]: <https://github.com/stac-utils/stac-task/compare/v0.4.2...v0.5.0>
[v0.4.2]: <https://github.com/stac-utils/stac-task/compare/v0.4.1...v0.4.2>
Expand Down
18 changes: 9 additions & 9 deletions stactask/task.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,10 @@ def __init__(
validate: bool = True,
):

self._payload = payload

if not skip_validation and validate:
if not self.validate(payload):
if not self.validate():
raise FailedValidation()

# set instance variables
Expand All @@ -87,7 +89,6 @@ def __init__(
self._upload = upload

self._skip_upload = not upload # deprecated
self._payload = payload

# create temporary work directory if workdir is None
if workdir is None:
Expand Down Expand Up @@ -172,13 +173,6 @@ def items(self) -> ItemCollection:
items_dict = {"type": "FeatureCollection", "features": self.items_as_dicts}
return ItemCollection.from_dict(items_dict, preserve_dict=True)

@classmethod
def validate(cls, payload: dict[str, Any]) -> bool:
"""Validates the payload and returns True if valid. If invalid, raises
``stactask.exceptions.FailedValidation`` or returns False."""
# put validation logic on input Items and process definition here
return True

@classmethod
def add_software_version(cls, items: list[dict[str, Any]]) -> list[dict[str, Any]]:
warnings.warn(
Expand Down Expand Up @@ -215,6 +209,12 @@ def add_software_version_to_item(cls, item: dict[str, Any]) -> dict[str, Any]:
item["properties"]["processing:software"] = {cls.name: cls.version}
return item

def validate(self) -> bool:
"""Validates `self._payload` and returns True if valid. If invalid, raises
``stactask.exceptions.FailedValidation`` or returns False."""
# put validation logic on input Items and process definition here
return True

def cleanup_workdir(self) -> None:
"""Remove work directory if configured not to save it"""
try:
Expand Down
5 changes: 2 additions & 3 deletions tests/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,8 @@ class FailValidateTask(Task):
name = "failvalidation-task"
description = "this task always fails validation"

@classmethod
def validate(self, payload: dict[str, Any]) -> bool:
if payload:
def validate(self) -> bool:
if self._payload:
raise FailedValidation("Extra context about what went wrong")
return True

Expand Down

0 comments on commit 6198c8b

Please sign in to comment.