Skip to content

Refactor to use pathlib#10

Merged
bjester merged 2 commits intomainfrom
pathlib-refactor
Feb 16, 2026
Merged

Refactor to use pathlib#10
bjester merged 2 commits intomainfrom
pathlib-refactor

Conversation

@bjester
Copy link
Owner

@bjester bjester commented Feb 16, 2026

Summary

  • Refactor use of os.* and os.path* to use more modern pathlib wherever possible

@github-actions
Copy link

github-actions bot commented Feb 16, 2026

🎉 Build Assets are ready!

File Checksum
gh_worktree-0.1.1-py3-none-any.whl ``
gh-worktree.pex ``

@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @bjester, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request modernizes the project's file system interactions by systematically replacing legacy os.path module calls with the pathlib module. This change enhances code clarity, improves type safety, and aligns the codebase with contemporary Python best practices for path handling.

Highlights

  • Pathlib Adoption: Refactored all instances of os.path and os.makedirs functions to use the more modern and object-oriented pathlib module for path manipulation and file system operations across the codebase.
  • Improved Readability and Maintainability: Leveraged pathlib.Path objects for constructing paths using the / operator, checking existence with .exists(), creating directories with .mkdir(), and opening files with .open(), leading to cleaner and more intuitive code.
  • Test Suite Updates: Updated all relevant unit tests to reflect the pathlib changes, including adjusting mock objects, test setup/teardown using tempfile.TemporaryDirectory, and assertion methods to correctly handle Path objects.
Changelog
  • src/gh_worktree/commands/init.py
    • Removed os import and replaced os.path.join, os.path.exists, os.makedirs, and open(os.path.join(...)) with pathlib equivalents.
  • src/gh_worktree/commands/remove.py
    • Removed os.path import and replaced os.path.exists(os.path.join(...)) with Path.exists().
  • src/gh_worktree/context.py
    • Imported Path and Union, updated cwd initialization to Path.cwd(), and converted all os.path calls to pathlib methods and operators.
    • Modified use context manager to accept Path objects and updated file existence checks and directory creation to use pathlib methods.
  • src/gh_worktree/hooks.py
    • Replaced os.path.join, os.path.exists, os.makedirs, open(), and os.chmod with pathlib methods and ensured compatibility with os.access by converting Path objects to strings where necessary.
  • src/gh_worktree/operator.py
    • Replaced os import with pathlib.Path, updated type hints for path iterators, and converted path joining and existence checks to pathlib methods.
  • src/gh_worktree/templates.py
    • Replaced os.path.join with path division operator and ensured Path objects are converted to strings when assigned to replacement_map values.
  • src/gh_worktree/utils.py
    • Replaced os import with pathlib.Path, updated type hints for path arguments, and converted os.path functions like realpath, join, isdir, exists, and basename to their pathlib equivalents.
  • tests/commands/test_checkout.py
    • Imported Path and updated StubContext and test setup to use pathlib.Path objects for directory paths.
  • tests/commands/test_create.py
    • Imported Path and updated StubContext and test setup to use pathlib.Path objects for directory paths.
  • tests/commands/test_init.py
    • Removed glob, os, shutil, pytest imports, added tempfile, and refactored test setup/teardown to use tempfile.TemporaryDirectory.
    • Updated StubContext to handle Path objects and converted assertions to use unittest.TestCase methods with Path objects.
  • tests/commands/test_remove.py
    • Removed pytest import, added tempfile and Path, and refactored tests to use unittest.TestCase and tempfile.TemporaryDirectory for setup/cleanup.
    • Updated StubContext to handle Path objects and replaced pytest.raises with self.assertRaisesRegex.
  • tests/test_gh.py
    • Imported Path and refactored tests to use unittest.TestCase and mock.patch.object, updating cwd to be a Path object.
  • tests/test_git.py
    • Imported Path and SimpleNamespace, updated context.cwd to be a Path object, and adjusted cwd arguments in mock calls to Path objects.
  • tests/test_hooks.py
    • Removed glob, shutil, pytest imports, added tempfile and Path, and refactored test setup/teardown to use tempfile.TemporaryDirectory.
    • Updated context.cwd to be a Path object, used Path.mkdir(), and ensured Path objects are converted to strings for os.access.
  • tests/test_runtime.py
    • Renamed RemoteTestCase to RuntimeTestCase for better clarity.
  • tests/test_templates.py
    • Updated StubContext to accept Path objects for directory arguments and refactored test setup/teardown to use tempfile.TemporaryDirectory.
  • tests/test_utils.py
    • Removed pytest import, added tempfile and Path, and refactored test setup/teardown to use tempfile.TemporaryDirectory.
    • Updated find_up calls and assertions to use Path objects and replaced pytest.raises with self.assertRaisesRegex.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request successfully refactors the codebase to use the more modern pathlib library, replacing os.path functions. The changes are applied consistently and improve the code's readability. I've added a couple of minor suggestions in src/gh_worktree/utils.py for a small optimization. Additionally, the test suite has been refactored from pytest to unittest. This is a substantial change alongside the pathlib migration. Both sets of changes look good.

@bjester bjester merged commit 90b895d into main Feb 16, 2026
10 checks passed
@bjester bjester deleted the pathlib-refactor branch February 16, 2026 16:41
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.

1 participant