Title: Lesson: prefer context managers over paired backup/restore helpers · Issue #1786 · usethis-python/usethis-python · GitHub
Open Graph Title: Lesson: prefer context managers over paired backup/restore helpers · Issue #1786 · usethis-python/usethis-python
X Title: Lesson: prefer context managers over paired backup/restore helpers · Issue #1786 · usethis-python/usethis-python
Description: When implementing backup-and-restore logic around a block of code (e.g. backing up poetry.lock before a subprocess and restoring it afterwards), always use a context manager instead of separate _backup and _restore helper functions with ...
Open Graph Description: When implementing backup-and-restore logic around a block of code (e.g. backing up poetry.lock before a subprocess and restoring it afterwards), always use a context manager instead of separate _ba...
X Description: When implementing backup-and-restore logic around a block of code (e.g. backing up poetry.lock before a subprocess and restoring it afterwards), always use a context manager instead of separate _ba...
Opengraph URL: https://github.com/usethis-python/usethis-python/issues/1786
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Lesson: prefer context managers over paired backup/restore helpers","articleBody":"When implementing backup-and-restore logic around a block of code (e.g. backing up `poetry.lock` before a subprocess and restoring it afterwards), always use a context manager instead of separate `_backup` and `_restore` helper functions with a `try/finally` block.\n\n**Why it matters:** A context manager encapsulates the setup/teardown lifecycle into a single construct, making the calling code cleaner and eliminating the risk of forgetting the `finally` block or mismatching the backup/restore calls. It also makes the intent clearer at the call site (`with _frozen_poetry_lock(path):` vs. manual backup + try/finally + restore).\n\n**Where observed:** `src/usethis/_backend/poetry/call.py` — the initial implementation used `_backup_poetry_lock` and `_restore_poetry_lock` as separate helpers, which was refactored into a `_frozen_poetry_lock` context manager in PR #1774.\n\n**Lesson for agents:** When writing code that needs to preserve and restore state around a block of operations, reach for `@contextmanager` as the first-choice pattern.","author":{"url":"https://github.com/nathanjmcdougall","@type":"Person","name":"nathanjmcdougall"},"datePublished":"2026-04-03T21:05:30.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":0},"url":"https://github.com/1786/usethis-python/issues/1786"}
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:30e6c7d1-ecc5-56c5-5036-d1e344f43eb8 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | C13A:236C7E:1DB95BF:2A0AAC1:6A4CD050 |
| html-safe-nonce | 47596856829d5e1357b2f870912e49e915d04c783e7042fff66228f0ee2a8833 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDMTNBOjIzNkM3RToxREI5NUJGOjJBMEFBQzE6NkE0Q0QwNTAiLCJ2aXNpdG9yX2lkIjoiMjg2NjAwNTA1MzkwMTk1MTA1NyIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 3a5faac5f25720f16fe459347a72ddc9b8390265ed760f9c2d1ac0856f4226eb |
| hovercard-subject-tag | issue:4202198197 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/usethis-python/usethis-python/1786/issue_layout |
| twitter:image | https://opengraph.githubassets.com/c095924370115d09557c8799bf6bf378573c5603d6bc87ce7656929162b0e2b0/usethis-python/usethis-python/issues/1786 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/c095924370115d09557c8799bf6bf378573c5603d6bc87ce7656929162b0e2b0/usethis-python/usethis-python/issues/1786 |
| og:image:alt | When implementing backup-and-restore logic around a block of code (e.g. backing up poetry.lock before a subprocess and restoring it afterwards), always use a context manager instead of separate _ba... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | nathanjmcdougall |
| hostname | github.com |
| expected-hostname | github.com |
| None | ee8a9d18044a05fe2d57b9ae797da38c8be34effb03f015f7c42e94770d28a1b |
| turbo-cache-control | no-preview |
| go-import | github.com/usethis-python/usethis-python git https://github.com/usethis-python/usethis-python.git |
| octolytics-dimension-user_id | 216362695 |
| octolytics-dimension-user_login | usethis-python |
| octolytics-dimension-repository_id | 842189705 |
| octolytics-dimension-repository_nwo | usethis-python/usethis-python |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 842189705 |
| octolytics-dimension-repository_network_root_nwo | usethis-python/usethis-python |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | f84814acc6f036583fd43463f5847a846243d2a4 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width