mkdocs-macros-plugin icon indicating copy to clipboard operation
mkdocs-macros-plugin copied to clipboard

supercollections prevents dumping back as yaml

Open kratsg opened this issue 10 months ago • 2 comments

a simple usecase might be to call yaml.safe_dump back on any variables loaded with this plugin, but the lack of any encoders causes a crash. A dump has the custom objects built in:

(Pdb) yaml.dump(contribution["notes"])
'!!python/object/new:super_collections.SuperList\nlistitems:\n- MET/sqrt(Ht) signal shape\n- !!python/object/new:super_collections.SuperList\n  listitems:\n  - why are smaller mass splittings harder? Is this due to chargino decay products?\n  - Check how MET and Ht differ between signals\n'

as opposed to the crash with safe_dump

(Pdb) yaml.safe_dump(contribution["notes"])
*** yaml.representer.RepresenterError: ('cannot represent an object', ['MET/sqrt(Ht) signal shape', ['why are smaller mass splittings harder? Is this due to chargino decay products?', 'Check how MET and Ht differ between signals']])

It would be really nice if the mkdocs-macros didn't just give SuperDict/SuperLists which makes things a bit harder, and somewhat unexpected when one is just providing yaml metadata in the frontmatter. The easiest solution I find is this

import yaml
import super_collections

def custom_representer(dumper, data):
    """Convert custom objects into YAML serializable format."""
    if isinstance(data, super_collections.SuperList):
        return dumper.represent_list(data)
    elif isinstance(data, super_collections.SuperDict):
        return dumper.represent_dict(data)
    else:
        return dumper.represent_str(str(data))

# Register the custom representer for unknown objects
yaml.add_multi_representer(object, custom_representer)

kratsg avatar Feb 21 '25 15:02 kratsg

Welcome to this project and thank you!

github-actions[bot] avatar Feb 21 '25 15:02 github-actions[bot]

In principle a SuperDict should be have exactly as a dictionary. If it doesn't then it might be an issue with it.

Cross referencing: fralau/super-collections/issues/7

fralau avatar Apr 20 '25 06:04 fralau

A similar issue has just been fixed by fixing a CustomEncoder in SuperCollections with JSON, so I am now a little more familiar.

@kratsg I would need to examples where this happens, so that I can think with it. Could you give me more information?

fralau avatar Sep 12 '25 18:09 fralau

I wish I could tell you, as I migrated to using inline-data on the markdown pages instead. I'm not fully sure I recall anymore. I think I had some data in a yaml file that I was trying to load and generate the markdown content on the fly.

kratsg avatar Sep 12 '25 22:09 kratsg

This should be solved with SuperCollections v0.5.7.

See: https://github.com/fralau/super-collections/issues/7

fralau avatar Sep 21 '25 17:09 fralau

@kratsg Could you please test if that happens again?

fralau avatar Sep 21 '25 17:09 fralau

Fixed for me for all of the projects I've tested so far.

kratsg avatar Sep 23 '25 02:09 kratsg