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
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions src/libtmux/_internal/types.py
Original file line number Diff line number Diff line change
@@ -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.


Notes
-----
:class:`StrPath` and :class:`StrOrBytesPath` is based on `typeshed's`_.

.. _typeshed's: https://github.com/python/typeshed/blob/5ff32f3/stdlib/_typeshed/__init__.pyi#L176-L179
""" # E501

from __future__ import annotations

import typing as t

if t.TYPE_CHECKING:
from os import PathLike

Comment on lines +12 to +16
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

from typing_extensions import TypeAlias

StrPath: TypeAlias = "str | PathLike[str]"
6 changes: 4 additions & 2 deletions src/libtmux/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import sys
import types

from libtmux._internal.types import StrPath
from libtmux.common import tmux_cmd

if sys.version_info >= (3, 11):
Expand Down Expand Up @@ -587,7 +588,7 @@ def new_window(
self,
window_name: str | None = None,
*,
start_directory: str | None = None,
start_directory: StrPath | None = None,
attach: bool = False,
window_index: str = "",
window_shell: str | None = None,
Expand Down Expand Up @@ -677,7 +678,8 @@ def new_window(

window_args += ("-P",)

if start_directory is not None:
# Catch empty string and default (`None`)
if start_directory and isinstance(start_directory, str):
# 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.

Expand Down