Skip to content

Add UsethisConfig.copy method to simplify set#1825

Merged
nathanjmcdougall merged 3 commits into
mainfrom
copilot/add-usethisconfig-copy-method
Apr 6, 2026
Merged

Add UsethisConfig.copy method to simplify set#1825
nathanjmcdougall merged 3 commits into
mainfrom
copilot/add-usethisconfig-copy-method

Conversation

Copilot AI commented Apr 6, 2026

Copy link
Copy Markdown
Contributor

UsethisConfig.set performed attribute-by-attribute save/restore across all 11 config fields, making it verbose and fragile when fields are added.

  • copy() method: Returns a shallow copy via dataclasses.replace, replacing 11 individual old_* = self.* assignments with old = self.copy()
  • _restore() method: Restores all fields from a snapshot via dataclasses.fields, replacing 11 individual self.* = old_* assignments with self._restore(old)
  • PLR0915 suppression removed: Statement count dropped enough to remove the noqa comment
  • Tests: TestCopy covers identity, value preservation across all fields, and independence from original
# Before: 11 saves + 11 restores
old_offline = self.offline
old_quiet = self.quiet
...
yield
self.offline = old_offline
self.quiet = old_quiet
...

# After
old = self.copy()
...
yield
self._restore(old)

Copilot AI and others added 2 commits April 6, 2026 19:18
- Add `copy()` method using `dataclasses.replace` to create a shallow copy
- Add private `_restore()` method to restore state from a copy
- Simplify `set()` to use `copy`/`_restore` instead of attribute-by-attribute
  save/restore, removing 20+ lines of repetitive code
- Remove now-unnecessary PLR0915 noqa suppression
- Add tests for the new `copy` method

Agent-Logs-Url: https://github.com/usethis-python/usethis-python/sessions/8e4db7cc-5876-49e5-9601-e64a0a24f831

Co-authored-by: nathanjmcdougall <18602289+nathanjmcdougall@users.noreply.github.com>
Agent-Logs-Url: https://github.com/usethis-python/usethis-python/sessions/8e4db7cc-5876-49e5-9601-e64a0a24f831

Co-authored-by: nathanjmcdougall <18602289+nathanjmcdougall@users.noreply.github.com>
Copilot AI changed the title [WIP] Add UsethisConfig.copy method to simplify UsethisConfig.set Add UsethisConfig.copy method to simplify set Apr 6, 2026
Copilot AI requested a review from nathanjmcdougall April 6, 2026 19:22
@nathanjmcdougall nathanjmcdougall marked this pull request as ready for review April 6, 2026 19:39
@codecov

codecov Bot commented Apr 6, 2026

Copy link
Copy Markdown

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!

@codspeed-hq

codspeed-hq Bot commented Apr 6, 2026

Copy link
Copy Markdown

Merging this PR will not alter performance

✅ 2 untouched benchmarks


Comparing copilot/add-usethisconfig-copy-method (5766e1f) with main (7c45f33)

Open in CodSpeed

@nathanjmcdougall nathanjmcdougall merged commit ea2fc02 into main Apr 6, 2026
20 checks passed
@nathanjmcdougall nathanjmcdougall deleted the copilot/add-usethisconfig-copy-method branch April 6, 2026 19:59
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.

Create a UsethisConfig.copy method to allow UsethisConfig.set to be simplified

2 participants