Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add signature to adapters so that help works as expected #334

Merged
merged 1 commit into from
Jul 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 28 additions & 1 deletion csp/impl/wiring/adapters.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import inspect
from datetime import timedelta
from typing_extensions import override

from csp.impl.__cspimpl import _cspimpl
from csp.impl.mem_cache import csp_memoized_graph_object
Expand Down Expand Up @@ -43,6 +45,20 @@ def __call__(cls, *args, **kwargs):
def using(cls, name=None, **__forced_tvars):
return lambda *args, **kwargs: cls._instantiate(__forced_tvars, name, *args, **kwargs)

@property
def __signature__(cls):
# Implement so that `help` works properly on adapter definitions.
parameters = [
inspect.Parameter(
input_def.name,
inspect.Parameter.POSITIONAL_OR_KEYWORD,
annotation=input_def.typ,
default=cls._signature.defaults.get(input_def.name, inspect.Parameter.empty),
)
for input_def in cls._signature.inputs
]
return inspect.Signature(parameters)


# Every AdapterDef instance represents an instance of a wiring-time input or output adapter
class AdapterDef:
Expand Down Expand Up @@ -348,7 +364,18 @@ def impl(mgr, engine, scalars):
)


add_graph_output = output_adapter_def(
@override
def add_graph_output(
key: object,
input: tstype.ts["T"], # noqa: F821
tick_count: int = -1,
tick_history: timedelta = timedelta(),
):
# Stub for IDE auto-complete/static type checking
...


add_graph_output = output_adapter_def( # noqa: F811
"add_graph_output",
_cspimpl._graph_output_adapter,
key=object,
Expand Down
4 changes: 4 additions & 0 deletions csp/impl/wiring/signature.py
Original file line number Diff line number Diff line change
Expand Up @@ -273,3 +273,7 @@ def ts_inputs(self):
@property
def scalars(self):
return self._scalars

@property
def defaults(self):
return self._defaults
7 changes: 7 additions & 0 deletions csp/tests/impl/test_outputadapter.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""this test is derived from e_14_user_adapters_05 and e_14_user_adapters_06"""

import inspect
import random
import threading
import unittest
Expand Down Expand Up @@ -198,3 +199,9 @@ def test_with_manager(self):
self.assertIn("publication_data_1", entry)
elif "symbol=data_3" in entry:
self.assertIn("publication_data_3", entry)

def test_help(self):
# for `help` to work on output adapters, signature must be defined
sig = inspect.signature(MyBufferWriterAdapter)
self.assertEqual(sig.parameters["input"].annotation, ts["T"])
self.assertEqual(sig.parameters["output_buffer"].annotation, list)
10 changes: 10 additions & 0 deletions csp/tests/impl/test_pushadapter.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import inspect
import threading
import time
import unittest
Expand Down Expand Up @@ -240,6 +241,15 @@ def graph():
result = list(x[1] for x in result)
self.assertEqual(result, expected)

def test_help(self):
# for `help` to work on adapters, signature must be defined
sig = inspect.signature(test_adapter)
self.assertEqual(sig.parameters["typ"].annotation, "T")
self.assertEqual(sig.parameters["interval"].annotation, int)
self.assertEqual(sig.parameters["ticks_per_interval"].annotation, int)
self.assertEqual(sig.parameters["push_mode"].annotation, PushMode)
self.assertEqual(sig.parameters["push_group"].annotation, object)


if __name__ == "__main__":
unittest.main()
Loading