Skip to content

subclassing schema #115

@SoundsSerious

Description

@SoundsSerious

Hey Again!

Found a bug (of my own making!), release 0.7.0 has this code:

This was the modifed code previously:

@classmethod
def schema(cls,only:typing.List[str]=None):
    '''schema caches Marshmellow Schemas on this class to preserve memory'''
    if not hasattr(cls,'_cls_schema'):
        objects_dict = cls.get_objects_dict()
        cls._cls_schema = type(
            cls.__name__ + "Schema", (ObjectSchema,), objects_dict
        )

I'm for now hacking this in my own application code with the use of hidden class based variables, with dynamic lookup

@classmethod
def columns(cls) -> typing.List[str]:
    
    f = lambda : "__{}_col_cache".format(cls.__name__)
    print(cls.__name__)
    if not hasattr(cls, f() ):
        cls._cls_schema = None
        del cls._cls_schema
        schema = cls.schema()
        out = set(schema.fields.keys()).union(set(["pik"]))
        if "_key" in out:
            out.remove("_key")
        setattr(cls,f(),list(out)) 
    return getattr(cls,f())

I can hopefully create a PR for this change soon.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions