ZnTrack icon indicating copy to clipboard operation
ZnTrack copied to clipboard

Fix YAML anchor references in params.yaml by implementing NoAnchorDumper

Open Copilot opened this issue 6 months ago • 3 comments

This PR fixes an issue where params.yaml files sometimes contained unwanted YAML anchor references like &id001 when the same object appeared multiple times in the data structure.

Problem

PyYAML automatically generates anchor references when the same object is referenced multiple times to avoid duplication. For example:

repeat: &id001
- 1
- 1
- 1
another_repeat: *id001

While this is valid YAML, it creates confusing and unreadable configuration files that are harder to understand and maintain.

Solution

Implemented a custom _NoAnchorDumper class that inherits from SafeDumper and overrides the ignore_aliases method to always return True, preventing automatic anchor generation.

class _NoAnchorDumper(SafeDumper):
    """Custom YAML dumper that disables anchor/alias generation."""
    def ignore_aliases(self, data):
        return True

Changes

  • zntrack/project.py: Added _NoAnchorDumper and updated params.yaml and dvc.yaml generation
  • zntrack/plugins/base.py: Added _NoAnchorDumper and updated experiment info YAML generation
  • tests/unit_tests/test_yaml_no_anchors.py: Added comprehensive tests to verify the fix

Result

The same data now generates clean, readable YAML:

repeat:
- 1
- 1
- 1
another_repeat:
- 1
- 1
- 1

The fix maintains full semantic equivalence and backward compatibility while producing cleaner, more maintainable configuration files.

Fixes #940.


💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.

Copilot avatar Sep 02 '25 08:09 Copilot

@PythonFZ 👋 This repository doesn't have Copilot instructions. With Copilot instructions, I can understand the repository better, work faster and produce higher quality PRs.

I can generate a .github/copilot-instructions.md file for you automatically. Click here to open a pre-filled issue and assign it to me. I'll write the instructions, and then tag you for review.

Copilot avatar Sep 02 '25 08:09 Copilot

Codecov Report

:white_check_mark: All modified and coverable lines are covered by tests. :white_check_mark: Project coverage is 88.77%. Comparing base (a46d048) to head (403cdbf). :white_check_mark: All tests successful. No failed tests found.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #941      +/-   ##
==========================================
+ Coverage   88.74%   88.77%   +0.03%     
==========================================
  Files          40       40              
  Lines        2434     2442       +8     
==========================================
+ Hits         2160     2168       +8     
  Misses        274      274              

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.

codecov[bot] avatar Sep 02 '25 09:09 codecov[bot]

Benchmark

Write: Varying number of nodes

Write: Varying number of edges

github-actions[bot] avatar Sep 02 '25 10:09 github-actions[bot]