Skip to content

Add a read-only context manager for YAML files to prevent round-trip …#1047

Merged
nathanjmcdougall merged 1 commit intomainfrom
1044-dont-modify-yaml-for-read-only-ops
Oct 26, 2025
Merged

Add a read-only context manager for YAML files to prevent round-trip …#1047
nathanjmcdougall merged 1 commit intomainfrom
1044-dont-modify-yaml-for-read-only-ops

Conversation

@nathanjmcdougall
Copy link
Copy Markdown
Collaborator

…failures

@nathanjmcdougall nathanjmcdougall linked an issue Oct 26, 2025 that may be closed by this pull request
@codspeed-hq
Copy link
Copy Markdown

codspeed-hq bot commented Oct 26, 2025

CodSpeed Performance Report

Merging #1047 will improve performances by 13.47%

Comparing 1044-dont-modify-yaml-for-read-only-ops (d444e2d) with main (040cdab)

Summary

⚡ 1 improvement
✅ 1 untouched

Benchmarks breakdown

Mode Benchmark BASE HEAD Change
Simulation test_several_tools_add_and_remove 1.2 s 1 s +13.47%

@codecov
Copy link
Copy Markdown

codecov bot commented Oct 26, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ All tests successful. No failed tests found.

📢 Thoughts on this report? Let us know!

@nathanjmcdougall nathanjmcdougall marked this pull request as ready for review October 26, 2025 21:21
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR introduces read-only context managers for YAML configuration files to prevent unintended modifications during read operations. The key motivation is to avoid round-trip failures where simply reading a YAML file could inadvertently alter its formatting (particularly quote styles).

Key Changes:

  • Added read_yaml() context manager that reads YAML files without writing changes back
  • Refactored edit_yaml() to use read_yaml() internally, maintaining backward compatibility
  • Created read-only wrappers read_pre_commit_config_yaml() and read_bitbucket_pipelines_yaml() for specific config files

Reviewed Changes

Copilot reviewed 8 out of 8 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
src/usethis/integrations/file/yaml/io.py Extracted read logic into new read_yaml() context manager; edit_yaml() now delegates to it
src/usethis/integrations/pre_commit/io.py Added read_pre_commit_config_yaml() context manager for read-only access
src/usethis/integrations/ci/bitbucket/io.py Added read_bitbucket_pipelines_yaml() context manager for read-only access
src/usethis/_integrations/pre_commit/hooks.py Updated get_hook_ids() to use read-only context manager instead of edit mode
src/usethis/_integrations/ci/bitbucket/steps.py Updated get_steps_in_default() to use read-only context manager instead of edit mode
src/usethis/_integrations/ci/bitbucket/cache.py Updated get_cache_by_name() to use read-only context manager instead of edit mode
tests/usethis/integrations/pre_commit/test_pre_commit_io.py Added test verifying quote style preservation with read-only context manager
tests/usethis/integrations/ci/bitbucket/test_bitbucket_io.py Added test verifying quote style preservation with read-only context manager

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@nathanjmcdougall nathanjmcdougall merged commit d8db50c into main Oct 26, 2025
20 checks passed
@nathanjmcdougall nathanjmcdougall deleted the 1044-dont-modify-yaml-for-read-only-ops branch October 26, 2025 21:53
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.

Don't modify YAML for read-only ops

2 participants