Skip to content

Commit

Permalink
Add signature to adapters so that help works as expected, part of #307
Browse files Browse the repository at this point in the history
Signed-off-by: Pascal Tomecek <pascal.tomecek@cubistsystematic.com>
  • Loading branch information
ptomecek authored and timkpaine committed Jul 16, 2024
1 parent 4114b90 commit 7c01d7b
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 0 deletions.
15 changes: 15 additions & 0 deletions csp/impl/wiring/adapters.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import inspect
from datetime import timedelta

from csp.impl.__cspimpl import _cspimpl
Expand Down Expand Up @@ -43,6 +44,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
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()

0 comments on commit 7c01d7b

Please sign in to comment.