Skip to content

types: Add StrPath typing, fix new_session, part 2 #598

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
May 26, 2025
Merged

Conversation

tony
Copy link
Member

@tony tony commented May 26, 2025

I'm making mistakes, but follow up to #597, #596

Summary

This PR adds uniform StrPath type support for start_directory parameters across all methods in libtmux, enabling PathLike objects (like pathlib.Path) to be used alongside strings.

Changes Made

Type Annotations Updated

  • Server.new_session: start_directory: str | Nonestart_directory: StrPath | None
  • Session.new_window: start_directory: str | Nonestart_directory: StrPath | None
  • Pane.split and Pane.split_window: start_directory: str | Nonestart_directory: StrPath | None
  • Window.split and Window.split_window: start_directory: str | Nonestart_directory: StrPath | None

Implementation Details

  • Added StrPath import to all affected modules
  • Updated docstrings to reflect "str or PathLike" support
  • Standardized logic patterns using if start_directory: (not if start_directory is not None:) to properly handle empty strings
  • Added path expansion logic with pathlib.Path(start_directory).expanduser() for proper tilde expansion

Testing

  • Added comprehensive parametrized tests for all affected methods
  • Test cases cover: None, empty string, absolute path string, and pathlib.Path objects
  • Added separate pathlib-specific tests using temporary directories
  • Tests verify operations complete successfully with all input types
  • Integrated tests into existing test files following project conventions

Files Modified

  • src/libtmux/server.py
  • src/libtmux/session.py
  • src/libtmux/pane.py
  • src/libtmux/window.py
  • tests/test_server.py
  • tests/test_session.py
  • tests/test_pane.py
  • tests/test_window.py

Benefits

  • Improved Developer Experience: Users can now pass pathlib.Path objects directly without converting to strings
  • Type Safety: Better type annotations that accurately reflect accepted input types
  • Consistency: Uniform pattern across all start_directory parameters in the codebase
  • Backward Compatibility: Existing string-based code continues to work unchanged

Testing Results

  • All linting (ruff), formatting, and type checking (mypy) passed
  • All 20 new start_directory tests passed (16 parametrized + 4 pathlib-specific)
  • Full test suite continues to pass

Fixes typing inconsistencies and enhances usability for path-related parameters throughout libtmux.

Summary by Sourcery

Update StrPath documentation and enhance Session.new_window to better handle start_directory values

Enhancements:

  • Simplify StrPath docstring by removing outdated StrOrBytesPath mention
  • Relax start_directory check in Session.new_window to apply expanduser for any truthy path-like value

Copy link

sourcery-ai bot commented May 26, 2025

Reviewer's Guide

This PR refines the internal type documentation by focusing solely on the new StrPath alias and simplifies the session startup logic by dropping the explicit string type check in favor of a truthiness check, allowing broader path-like inputs to be handled by pathlib.

Updated class diagram for the Session class

classDiagram
  class Session {
    +new_window(..., start_directory: StrPath | None, ...): Window
  }
Loading

File-Level Changes

Change Details Files
Updated type documentation to remove redundant reference and align with the new StrPath definition
  • Removed mention of StrOrBytesPath from the module docstring
  • Simplified doc comment to reference only StrPath based on typeshed
src/libtmux/_internal/types.py
Simplified start_directory handling in new_window to accept any truthy path-like object
  • Replaced if start_directory and isinstance(start_directory, str) with if start_directory
  • Delegated all input types to pathlib.Path.expanduser() for consistent path expansion
src/libtmux/session.py

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link

codecov bot commented May 26, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 80.30%. Comparing base (e58766a) to head (8483a5b).
Report is 13 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #598      +/-   ##
==========================================
+ Coverage   79.84%   80.30%   +0.45%     
==========================================
  Files          23       23              
  Lines        1915     1919       +4     
  Branches      294      294              
==========================================
+ Hits         1529     1541      +12     
+ Misses        266      261       -5     
+ Partials      120      117       -3     

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

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copy link

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

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

Hey @tony - I've reviewed your changes and they look great!

Here's what I looked at during the review
  • 🟢 General issues: all looks good
  • 🟢 Security: all looks good
  • 🟢 Testing: all looks good
  • 🟢 Documentation: all looks good

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

tony added 10 commits May 26, 2025 06:32
why: Standardize path handling to accept both str and PathLike objects

what:
- Import StrPath from libtmux._internal.types
- Update start_directory parameter type annotation to StrPath | None
- Update docstring to reflect str or PathLike support
- Maintain existing if start_directory: logic for proper command building
…rectory

why: Standardize path handling to accept both str and PathLike objects

what:
- Import StrPath from libtmux._internal.types
- Update start_directory parameter type annotations to StrPath | None
- Update docstrings to reflect str or PathLike support
- Fix logic pattern from 'is not None' to truthy check for proper command building
- Clean up import organization
…directory

why: Standardize path handling to accept both str and PathLike objects

what:
- Import StrPath from libtmux._internal.types
- Update start_directory parameter type annotations to StrPath | None
- Update docstring to reflect str or PathLike support
what:
- Introduced StartDirectoryTestFixture for parameter testing
- Added tests for various start_directory scenarios including None, empty string, absolute path, and pathlib.Path
- Updated test_pane_context_manager to verify pane count after context exit
- Added test for pathlib.Path acceptance in Pane.split
what:
- Added StartDirectoryTestFixture for comprehensive testing of start_directory scenarios
- Implemented tests for None, empty string, absolute path, and pathlib.Path inputs
- Verified correct command generation based on start_directory values
- Ensured compatibility with pathlib.Path in new_session method
what:
- Added comprehensive tests for `start_directory` parameter handling in `Session.new_window`
- Included scenarios for None, empty string, absolute path, and pathlib.Path
- Verified correct command generation and window creation based on `start_directory` values
- Ensured compatibility with pathlib.Path for start_directory input
what:
- Added tests for start_directory parameter handling in Window.split
- Included scenarios for None, empty string, absolute path, and pathlib.Path
- Verified correct pane creation and command generation based on start_directory values
- Ensured compatibility with pathlib.Path for start_directory input
what:
- Removed expected_in_cmd and expected_not_in_cmd from StartDirectoryTestFixture.
- Updated test cases to handle user_path and relative_path scenarios.
- Enhanced logic for formatting start_directory with actual paths in tests.
- Ensured compatibility with pathlib.Path for start_directory input across tests in test_pane.py, test_server.py, test_session.py, and test_window.py.
what:
- Added monkeypatching to change the current working directory to tmp_path in test cases.
- Simplified assertions for current path checks by removing unnecessary conditional statements.
- Ensured consistency in handling expected paths across test files: test_pane.py, test_server.py, test_session.py, and test_window.py.
@tony tony merged commit c579a5c into master May 26, 2025
28 checks passed
@tony tony deleted the session-str-path-2 branch May 26, 2025 19:35
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