Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Simplify
engine/internals/nodes.py
(#10487)
[ci skip-rust] [ci skip-build-wheels]
- Loading branch information
1 parent
01ba67e
commit a93850b
Showing
1 changed file
with
4 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,57 +1,21 @@ | ||
# Copyright 2015 Pants project contributors (see CONTRIBUTORS.md). | ||
# Copyright 2020 Pants project contributors (see CONTRIBUTORS.md). | ||
# Licensed under the Apache License, Version 2.0 (see LICENSE). | ||
|
||
import logging | ||
from abc import ABC | ||
from dataclasses import dataclass | ||
from typing import Any, Optional, Tuple | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
def _satisfied_by(t, o): | ||
"""Pickleable type check function.""" | ||
return t.satisfied_by(o) | ||
|
||
|
||
class State(ABC): | ||
@classmethod | ||
def raise_unrecognized(cls, state): | ||
raise ValueError("Unrecognized Node State: {}".format(state)) | ||
|
||
@staticmethod | ||
def from_components(components): | ||
"""Given the components of a State, construct the State.""" | ||
cls, remainder = components[0], components[1:] | ||
return cls._from_components(remainder) | ||
|
||
def to_components(self): | ||
"""Return a flat tuple containing individual pickleable components of the State. | ||
TODO: Consider https://docs.python.org/2.7/library/pickle.html#pickling-and-unpickling-external-objects | ||
for this usecase? | ||
""" | ||
return (type(self),) + self._to_components() | ||
|
||
|
||
@dataclass(frozen=True) | ||
class Return(State): | ||
class Return: | ||
"""Indicates that a Node successfully returned a value.""" | ||
|
||
value: Any | ||
|
||
@classmethod | ||
def _from_components(cls, components): | ||
return cls(components[0]) | ||
|
||
def _to_components(self): | ||
return (self.value,) | ||
|
||
|
||
@dataclass(frozen=True) | ||
class Throw(State): | ||
class Throw: | ||
"""Indicates that a Node should have been able to return a value, but failed.""" | ||
|
||
exc: Exception | ||
python_traceback: Optional[str] = None | ||
engine_traceback: Tuple[str, ...] = tuple() | ||
engine_traceback: Tuple[str, ...] = () |