SwissDataScienceCenter / calamus

A JSON-LD Serialization Libary for Python
Apache License 2.0
29 stars 12 forks source link

Cannot inherit class fields #69

Closed artreven closed 2 years ago

artreven commented 2 years ago

Thanks for this great package!

I tried defining some hierarchy of class using metaclass=JsonLDAnnotation and noticed that fields are not inherited. Here is a minimal example to reproduce the problem:

import calamus.schema, calamus.fields

skos_ns = calamus.fields.Namespace('http://www.w3.org/2004/02/skos/core#')

class A(metaclass=calamus.schema.JsonLDAnnotation):
    def __init__(self, uri=None):
        self.uri = uri or calamus.fields.BlankNodeId()

    uri = calamus.fields.Id()

    class Meta:
        rdf_type = [skos_ns.Concept]

class B(A):
    def __init__(self, obj1, uri=None):
        self.obj1 = obj1
        super(B, self).__init__(uri=uri)

    obj1 = calamus.fields.String(skos_ns.prefLabel)

    class Meta(A.Meta):
        rdf_type = A.Meta.rdf_type

a_uri = 'http://example.com/A'
a = A(uri=a_uri)
assert a.dump()['@id'] == a_uri, f'Serialized URI = {a.dump()["@id"]}, but expected {a_uri}'

b_uri = 'http://example.com/B'
b = B(obj1='something', uri=b_uri)
assert b.dump()['@id'] == b_uri, f'Serialized URI = {b.dump()["@id"]}, but expected {b_uri}'
Panaetius commented 2 years ago

Hey there, sorry for the way too late reply, I had my notification settings for this repo set wrong and only saw your issue now :slightly_frowning_face: (This repo doesn't get that much traffic...)

It should work if you use separate schemas instead of the JsonLDAnnotation.

The issue is that when using JsonLDAnnotation we create *Schema classes on the fly, but in the case of inheritance, we weren't inheriting from parent schema classes. I've opened a PR that should fix this: https://github.com/SwissDataScienceCenter/calamus/pull/70

Can you test if that works for you?

artreven commented 2 years ago

thank you @Panaetius ! Sorry, did not have time to work on this topic lately.