Skip to content

Allow Scan to take no parameters #62

@evvaaaa

Description

@evvaaaa

Currently Scan is only validating correctly if initialised through the scan decorator:

if not len(self.parameters) == 1:
raise FastCSException("Scan method cannot have arguments")

This is because inspect.Signature.parameters will ignore self, but not in the wrapped case:

import inspect

class SomeClass:
    def __init__(self, fn, b):
        print(f"someclass fn {fn.__name__}", inspect.signature(fn).parameters)

def dec(b: str):
    def wrapper(fn):
        fn.wrapped_foo = SomeClass(fn, b)
    return wrapper

class X:
    def __init__(self):
        print("internal foo", inspect.signature(self.foo).parameters)
        print("internal bar", inspect.signature(self.bar).parameters)
        SomeClass(self.foo, "c")

    async def foo(self): ...
    async def bar(self, a: str): ...

    @dec("b")
    async def wrapped_foo(self): ...

x = X()

gives us

someclass fn wrapped_foo OrderedDict([('self', <Parameter "self">)])
internal foo OrderedDict()
internal bar OrderedDict([('a', <Parameter "a: str">)])
someclass fn foo OrderedDict()

I would like to be able to pass a poll period into my controller at init time

class PandaController(Controller):
    def __init__(self, hostname: str, poll_period: float) -> None:
        ...
        self.fastcs_method = Scan(self.update, poll_period)

but this leads to no parameters and the check above failing because the expected self argument isn't present.

Suggestion

Change

if not len(self.parameters) == 1:
raise FastCSException("Scan method cannot have arguments")

to

        if self.parameters and tuple(self.parameters.keys()) != ("self",):
            raise FastCSException("Scan method cannot have arguments")

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    Status

    Done

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions