Skip to content

Commit

Permalink
Supporting subclassed pg.Functor.
Browse files Browse the repository at this point in the history
Usage:

```python
import pyglove as pg

class Foo(pg.Functor):
  x: int
  y: int
  def _call(self) -> int:
    return self.x + self.y

Foo(1, 2)()    # Early bound.
Foo()(1, 2)    # Late bound.
Foo(1)(y=2)    # Partial bound.
Foo(1, 2)(y=3, override_args=True)   # Partial bound with override.
...
```

This CL also renames `pg.Functor.signature` to `pg.Functor.__signature__`.

PiperOrigin-RevId: 563788916
  • Loading branch information
daiyip authored and pyglove authors committed Sep 8, 2023
1 parent 05a42c9 commit 11f5df1
Show file tree
Hide file tree
Showing 7 changed files with 278 additions and 120 deletions.
7 changes: 4 additions & 3 deletions pyglove/core/patching/rule_based.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,8 @@ def validate(self, x: symbolic.Symbolic) -> None:
def __call__(
self,
x: symbolic.Symbolic
) -> Union[Dict[str, Any], Tuple[Dict[str, Any], Callable[[], None]]]:
) -> Union[Dict[str, Any],
Tuple[Dict[str, Any], Callable[[Any], None]]]:
"""Override __call__ to get rebind dict."""
return super().__call__(x, override_args=True)

Expand Down Expand Up @@ -218,7 +219,7 @@ def _decorator(fn):
cls = functor_decorator(fn)
_PATCHER_REGISTRY.register(name or fn.__name__,
typing.cast(Type[Patcher], cls))
arg_specs = cls.signature.args
arg_specs = cls.__signature__.args
if len(arg_specs) < 1:
raise TypeError(
'Patcher function should have at least 1 argument '
Expand Down Expand Up @@ -337,7 +338,7 @@ def from_uri(uri: str) -> Patcher:
"""Create a Patcher object from a URI-like string."""
name, args, kwargs = parse_uri(uri)
patcher_cls = typing.cast(Type[Any], _PATCHER_REGISTRY.get(name))
args, kwargs = parse_args(patcher_cls.signature, args, kwargs)
args, kwargs = parse_args(patcher_cls.__signature__, args, kwargs)
return patcher_cls(object_utils.MISSING_VALUE, *args, **kwargs)


Expand Down
Loading

0 comments on commit 11f5df1

Please sign in to comment.