[spyne] _no_self=False broken ? (#371)

Burak Arslan burak.arslan at arskom.com.tr
Tue Jun 24 20:36:11 UTC 2014

Hi Thomas,

On 06/23/14 22:43, Thomas Recouvreux wrote:
> I try to do something similar to:
> class A(ServiceBase):
>     @rpc(String, _returns=String, _no_cls=False)
>     def hello(cls, ctx, name):
>         print(name, cls)
>         return cls._hello(name)
>     @classmethod
>     def _hello(cls, name):
>         return 'Hello, %s' % name
> class B(A):
>     @classmethod
>     def _hello(cls, name):
>         return 'Bello, %s' % name
> app = Application([B], name='Service', tns='my.ns.roxx', ..)
> # client
> client.service.hello('tom')
> # >>> Bello, tom
> I'd like to ba able to do some inheritance and overriding between my
> services.
> I have a working implementation on my repo
> https://github.com/trecouvr/spyne/tree/trecouvr/service_inheritance
> but I was wondering if this is not already implemented.

I wish you'd asked me before going forward and writing all that code.

Service class is already accessible from ctx.descriptor.service_class,
so  deca3d399ab1c9732f127af2fa9e695c6820526c and in turn
a695324cd193f10891cd8d574afb7b3526197cb4 are totally redundant.

ServiceBase subclasses are designed to be composed, not inherited. The
self.public_records = {} line in ServiceBaseMeta initializer is totally
intentional. Sorry but I will not accept any patch that alters this
behaviour. The rationale is that the code in ServiceBase subclasses
defines the public interface of your application. So it has to be as
clear and explicit as possible..

I'd rewrite your code as:

def _hello(name):
    return 'Hello, %s' % name

def _bello(name):
    return 'Bello, %s' % name

def TSomeService(hellobello):
    class SomeService(ServiceBase):
        @rpc(String, _returns=String, _no_cls=False)
        def hello(cls, ctx, name):
            print(name, cls)
            return hellobello(name)
    return SomeService

A = TSomeService(_hello)
B = TSomeService(_bello)

I know, this is a bit weird at first, but trust me that is pattern is
way more useful than the classic polymorphic approach once you get the
hang of it.

Best regards,

More information about the people mailing list