Skip to content

Commit

Permalink
Simplify engine/internals/nodes.py (#10487)
Browse files Browse the repository at this point in the history
[ci skip-rust]
[ci skip-build-wheels]
  • Loading branch information
Eric-Arellano committed Jul 28, 2020
1 parent 01ba67e commit a93850b
Showing 1 changed file with 4 additions and 40 deletions.
44 changes: 4 additions & 40 deletions src/python/pants/engine/internals/nodes.py
@@ -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, ...] = ()

0 comments on commit a93850b

Please sign in to comment.