Skip to content

Conversation

Copy link

Copilot AI commented Sep 2, 2025

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.

Copy link
Author

Copilot AI commented Sep 2, 2025

@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.

Co-authored-by: PythonFZ <46721498+PythonFZ@users.noreply.github.com>
Copilot AI changed the title [WIP] params.yaml sometimes contains odd &id001 refs Fix YAML anchor references in params.yaml by implementing NoAnchorDumper Sep 2, 2025
Copilot AI requested a review from PythonFZ September 2, 2025 08:36
Comment on lines +190 to +191
config.PARAMS_FILE_PATH.write_text(yaml.dump(params_dict, Dumper=_NoAnchorDumper))
config.DVC_FILE_PATH.write_text(yaml.dump(dvc_dict, Dumper=_NoAnchorDumper))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would be better to use json.loads(json.dumps(...)) because it adds less code and with a comment refering to yaml/pyyaml#535 this would be much simpler.

@codecov
Copy link

codecov bot commented Sep 2, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 88.77%. Comparing base (a46d048) to head (403cdbf).
✅ 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              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@github-actions
Copy link

github-actions bot commented Sep 2, 2025

Benchmark

Write: Varying number of nodes

Write: Varying number of edges

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

Successfully merging this pull request may close these issues.

params.yaml sometimes contains odd &id001 refs

2 participants