Skip to content

types: Add StrPath typing, fix new_session #597

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 2 commits into from
May 26, 2025
Merged

types: Add StrPath typing, fix new_session #597

merged 2 commits into from
May 26, 2025

Conversation

tony
Copy link
Member

@tony tony commented May 26, 2025

Check if this works to fix https://github.com/tmux-python/libtmux/pull/596/files#r2106995968.

Summary by Sourcery

Add StrPath type alias for internal annotations and adjust new_window to properly handle optional string start_directory values.

New Features:

  • Introduce StrPath type alias for path-like annotations.

Bug Fixes:

  • Fix new_window to accept a string start_directory and use explicit None checks instead of truthiness.

Copy link

sourcery-ai bot commented May 26, 2025

Reviewer's Guide

Introduces a new StrPath alias for path-like strings and refines the start_directory typing and logic in new_window to use explicit None checking.

Class diagram for the new StrPath type alias

classDiagram
  namespace libtmux._internal.types {
    class StrPath {
      <<TypeAlias: str | os.PathLike~str~>>
    }
  }
Loading

Updated class diagram for the Session class

classDiagram
  namespace libtmux.session {
    class Session {
      +new_window(window_name: str | None = None, start_directory: str | None = None, attach: bool = False, window_index: str = "", window_shell: str | None = None)
    }
  }
Loading

File-Level Changes

Change Details Files
Introduce StrPath type alias for path-like strings
  • Create new _internal/types.py module
  • Import PathLike and TypeAlias under TYPE_CHECKING
  • Define `StrPath = "str
PathLike[str]"`
Refine start_directory parameter in new_window
  • Change annotation from None to `str
None</li><li>Replace truthiness check with is not None`

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

Attention: Patch coverage is 50.00000% with 1 line in your changes missing coverage. Please review.

Project coverage is 79.84%. Comparing base (14d72bd) to head (7f96abb).
Report is 4 commits behind head on master.

Files with missing lines Patch % Lines
src/libtmux/session.py 0.00% 0 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master     #597      +/-   ##
==========================================
+ Coverage   79.83%   79.84%   +0.01%     
==========================================
  Files          22       23       +1     
  Lines        1914     1915       +1     
  Branches      294      294              
==========================================
+ Hits         1528     1529       +1     
  Misses        266      266              
  Partials      120      120              

☔ 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: 5 issues found
  • 🟢 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.

@@ -587,7 +587,7 @@ def new_window(
self,
window_name: str | None = None,
*,
start_directory: None = None,
start_directory: str | None = None,
Copy link

Choose a reason for hiding this comment

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

suggestion: Restrictive type annotation: consider accepting PathLike

Annotate start_directory as str | PathLike[str] | None (or use StrPath) to allow both strings and path-like objects, matching what pathlib.Path and .expanduser() accept.

Suggested implementation:

        start_directory: str | os.PathLike[str] | None = None,
            start_directory = pathlib.Path(start_directory).expanduser()
import os

@@ -677,7 +677,7 @@

window_args += ("-P",)

if start_directory:
if start_directory is not None:
Copy link

Choose a reason for hiding this comment

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

question (bug_risk): Empty string now treated as a valid directory

Using is not None allows empty strings, which pathlib.Path interprets as the current directory. If this isn't intended, consider keeping the original truthiness check or explicitly rejecting empty strings.

@@ -677,7 +677,7 @@

window_args += ("-P",)

if start_directory:
if start_directory is not None:
# as of 2014-02-08 tmux 1.9-dev doesn't expand ~ in new-window -c.
start_directory = pathlib.Path(start_directory).expanduser()
window_args += (f"-c{start_directory}",)
Copy link

Choose a reason for hiding this comment

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

suggestion (bug_risk): Unquoted directory argument may break on spaces

Passing the directory as an unquoted string may fail if the path contains spaces. Use separate arguments for the flag and value, or ensure proper quoting.

@@ -0,0 +1,19 @@
"""Internal type annotations.
Copy link

Choose a reason for hiding this comment

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

issue: Doc mentions StrOrBytesPath but alias is not defined

Please define the StrOrBytesPath alias or remove its mention from the docstring to prevent confusion.

Comment on lines +12 to +16
import typing as t

if t.TYPE_CHECKING:
from os import PathLike

Copy link

Choose a reason for hiding this comment

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

suggestion: TypeAlias used outside TYPE_CHECKING import block

Since StrPath: TypeAlias is at the module level, ensure TypeAlias is available at runtime by importing it outside the TYPE_CHECKING block or move the alias declaration inside the block for clarity.

Suggested change
import typing as t
if t.TYPE_CHECKING:
from os import PathLike
import typing as t
from typing import TypeAlias
if t.TYPE_CHECKING:
from os import PathLike

@tony tony force-pushed the session-str-path branch from cda6c79 to 7f96abb Compare May 26, 2025 10:25
@tony tony merged commit e58766a into master May 26, 2025
28 checks passed
@tony tony deleted the session-str-path branch May 26, 2025 10:31
tony added a commit that referenced this pull request 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 | None` → `start_directory: StrPath | None`
- `Session.new_window`: `start_directory: str | None` → `start_directory: StrPath | None`
- `Pane.split` and `Pane.split_window`: `start_directory: str | None` → `start_directory: StrPath | None`
- `Window.split` and `Window.split_window`: `start_directory: str | None` → `start_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
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