Open tropxy opened 3 years ago
I'm unsure how to fix this on this library. Or even issue a warning to the user.
Consider the following code:
def other(f):
def inner(*args, **kwargs):
return f(args, kwargs)
return inner
class ChargePoint:
@on(Action.Heartbeat)
@other
def on_heartbeat(self):
pass
Note that decorators are executed from bottom to top. Also note that decorators are syntactic sugar. The above could be rewritten as: on(other)(on_heartbeat)
As you can see on()
receives other()
. And in it's current implementation on()
will register other
in a global list of routables. Ans it sets the attributes _on_action
and _skip_schema_validation
on this method instead of setting them on on_heartbeat
.
create_route_map()
iterates over that global lists of routables. It verifies if an instance of ChargePoint
has an attribute that matches the routables
. So in our case something like getattr(ChargePoint(), 'other')
is done. That look up will fail.
OCPP Version: 0.8.1 Python Version: 3.X
If we have an handler for a BootNotification like this:
Things work as expected. But if we have a simple decorator like this one:
and we apply it to the BootNotification handler:
The handler is not registered in the routables and we get the error:
This happens because the code in here: https://github.com/mobilityhouse/ocpp/blob/48092b453a5829ff96bba11eb18a132babe0b390/ocpp/routing.py#L37
uses the function name to establish a route, but the say_hello decorator changes the function name to 'inner', in this case. A quick fix on the client side is to use the functools and decorate the inner method with it, like:
But ideally, the lib should be shielded against other decorators actions