Skip to content

Commit

Permalink
Remove AsyncField.validate_{pre,post}_hydration
Browse files Browse the repository at this point in the history
There was no need for this premature abstraction. Instead, all validation should happen in the corresponding hydrate rule.
  • Loading branch information
Eric-Arellano committed Mar 12, 2020
1 parent 8f32955 commit 71800ba
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 31 deletions.
22 changes: 4 additions & 18 deletions src/python/pants/engine/target.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,11 @@ class SourcesResult:
@rule
def hydrate_sources(sources: Sources) -> SourcesResult:
sources.validate_pre_hydration()
# possibly validate `sources.raw_value`
...
result = await Get[Snapshot](PathGlobs(sources.raw_value))
sources.validate_post_hydration(result)
# possibly validate `result`
...
return SourcesResult(result)
Expand All @@ -74,22 +76,6 @@ def rules():
sources = await Get[SourcesResult](Sources, my_tgt.get(Sources))
"""

def validate_pre_hydration(self) -> None:
"""Any validation that can be done on the original `raw_value`.
It is cheaper to do any possible validation here, rather than in `validate_post_hydration`,
because we can short-circuit if an invariant is violated before incurring the expense of
hydration.
"""

def validate_post_hydration(self, result: Any) -> None:
"""Any validation that must be done after hydration by inspecting the result of that
hydration.
For example, a `PythonSources` field may validate that all hydrated source files end in
`.py`.
"""


_F = TypeVar("_F", bound=Field)

Expand Down
20 changes: 7 additions & 13 deletions src/python/pants/engine/target_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,6 @@ class HaskellSources(AsyncField):
alias: ClassVar = "sources"
raw_value: Optional[List[str]]

def validate_pre_hydration(self) -> None:
ensure_str_list(self.raw_value)

def validate_post_hydration(self, result: Snapshot) -> None:
non_haskell_sources = [fp for fp in result.files if PurePath(fp).suffix != ".hs"]
if non_haskell_sources:
raise ValueError(
f"Received non-Haskell sources in {self.alias}: {non_haskell_sources}."
)


@dataclass(frozen=True)
class HaskellSourcesResult:
Expand All @@ -60,9 +50,13 @@ class HaskellSourcesResult:

@rule
async def hydrate_haskell_sources(sources: HaskellSources) -> HaskellSourcesResult:
sources.validate_pre_hydration()
result = await Get[Snapshot](PathGlobs("*.hs"))
sources.validate_post_hydration(result)
# Validate before hydration
ensure_str_list(sources.raw_value)
result = await Get[Snapshot](PathGlobs(sources.raw_value))
# Validate after hydration
non_haskell_sources = [fp for fp in result.files if PurePath(fp).suffix != ".hs"]
if non_haskell_sources:
raise ValueError(f"Received non-Haskell sources in {sources.alias}: {non_haskell_sources}.")
return HaskellSourcesResult(result)


Expand Down

0 comments on commit 71800ba

Please sign in to comment.