[spyne] Complex data types and inheritance

Burak Arslan burak.arslan at arskom.com.tr
Mon Jun 9 22:52:30 UTC 2014


On 06/06/14 13:13, Alex wrote:
> Hi,
>
>
> I have a hierarchy of data types that I built with the intention to
> avoid duplication of code and effort. For example, there is a basic
> data type defined as such:
>
> class RequestSigned(ComplexModel):
>    identifier = Uuid
>    signature = Unicode
>
>
> And a few others that contain their own data, besides the `identifier`
> and `signature`, ex:
>
> class FooBar(RequestSigned):
>    foo = Unicode
>    bar = Uuid
>
>
> The data are exchanged via JSON strings, on top of HTTP. When a
> spynified function of the server returns an object of the FooBar
> class, I do something like this
>
> result = FooBar()
> result.identifier = xxxxxxxxx
> result.signature = yyyyyyyyyyy
> result.foo = zzzzzz
> result.identifier = qqqqqqqqqqq
> return result
>
>
> The assignments go without an error, but the client only gets `foo`
> and `bar` in the returned object, not the other attributes, which I
> assumed would be there.

Hi Alex,

This is supposed to work.

I added your case to Spyne test suite, both 2.10 and 2.11. 2.10:
https://github.com/arskom/spyne/blob/121516e8d6d22eb7a057814fd0a3999286423e82/spyne/test/protocol/_test_dictdoc.py#L1068
and they are both green.

Could you provide a self-contained example (e.g. a modified
examples/hello_world as a github gist) so that I can work on it?

>
> So it seems that one of my basic assumptions was wrong - such form of
> inheritance does not work; perhaps I was fooled by the fact that no
> errors were thrown when I sent a test request to the server (and did
> not bother to examine the result carefully).
>
>
> What is the recommended method of reusing data types? I have a large
> hierarchy of types and it would be a pity if I had to explicitly
> declare everything in each class. Perhaps I am missing something?

I hope you do :)

Besides inheritance there are also __mixin__classes. While it's
essentially the same as interiting from a non-mixin class, what really
happens is that fields from parent's type info are merged to child's
type info. __mixin__ classes don't set __extends__ thus don't appear in
the xml schema as parents of their child classes.

You can use them as a workaround to Xml Schema's single inheritance
restriction.

Best regards,
Burak



More information about the people mailing list