Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Automatically use LibYAML bindings if they are available (or easily enable globally) #437

Open
psphicas opened this issue Sep 10, 2020 · 2 comments

Comments

@psphicas
Copy link
Contributor

It would be nice to have fast LibYAML bindings automatically enabled if they are available, or, to have an easy way to turn them on globally, without modifying existing code that already uses PyYAML, and that can work even when importing other packages that themselves use PyYAML.

The suggestion in the documentation

from yaml import load, dump
try:
    from yaml import CLoader as Loader, CDumper as Dumper
except ImportError:
    from yaml import Loader, Dumper
# ...
data = load(stream, Loader=Loader)
# ...
output = dump(data, Dumper=Dumper)

only works for code that is fully controlled. Also, it is inconvenient to retrofit large projects that may import yaml in more than one place, use load, dump, add_representer, etc. without explicitly specifying a Loader or Dumper, or use the "sugar" methods.

This seems work, but maybe there is a better way?
#436

@bryant1410
Copy link

What about having a yaml.auto import that automatically selects the Loader, Dumper, etc? Like tqdm.auto API. Which could eventually become the default in a major version change (v7?).

@nitzmahone
Copy link
Member

Yeah, this has been a long-standing need, but is somewhat complicated in the current codebase by the way the various Loaders and Dumpers are technically unrelated concrete classes composed entirely of mixins. I have a working sample of something similar in #700 - there's still some bikeshedding on the name, but the overall concept seems workable (since the new API lets you explicitly opt in to the features/tags you want independent of the underlying Loader/Dumper impl).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants