Ticket #91 (reopened enhancement)
emit no anchors and aliases
| Reported by: | Andrey | Owned by: | xi |
|---|---|---|---|
| Priority: | normal | Component: | pyyaml |
| Severity: | normal | Keywords: | |
| Cc: | w@… |
Description
Hi, is it possible to avoid using anchors and aliases? So to output
[1, 1]
as
- 1 - 1
and not as
- &id1 1 - *id1
Attachments
Change History
comment:2 Changed 3 years ago by iand@…
- Status changed from closed to reopened
- Resolution worksforme deleted
I can reproduce this with the following input, regardless of flow style. Having the option to disable anchors & aliases would be extremely handy for me.
import yaml print yaml.dump([1L, 1L])
comment:3 Changed 22 months ago by anonymous
+1 - yaml loses a lot of usefulness when it isn't human readable, and having hundreds of aliases in a document with names like id001 is hard for me to parse.
comment:4 Changed 17 months ago by w@…
- Cc w@… added
This is actually really easy, it's just not documented:
class CustomDumper?(SafeDumper?):
A custom YAML dumper that never emits aliases"
def ignore_aliases(self, _data):
return True
Can this please be documented?
comment:5 Changed 12 months ago by anonymous
Here is a quick script to remove anchors and aliases:
#!/usr/bin/python
"""Parse and re-emit YAML without aliases.
"""
import argparse
import sys
import yaml
def main():
parser = argparse.ArgumentParser()
parser.add_argument(
'file', nargs='?', default='-', type=argparse.FileType('r'))
args = parser.parse_args()
Loader = yaml.SafeLoader
Dumper = yaml.SafeDumper
Dumper.ignore_aliases = lambda self, data: True
yaml.dump(yaml.load(args.file, Loader=Loader), sys.stdout, Dumper=Dumper)
if __name__ == '__main__':
sys.exit(main())
Save as ~/bin/yaml-rewrite
I use it as a git diff filter. Register with
git config --global diff.cleanyaml.textconv yaml-rewrite
and in .gitattributes:
*.yaml diff=cleanyaml

It works for me:
Could you post a code snippet that produces the output with aliases?