Skip to content
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

tmux_cmd: Modernize to use text=True #560

Merged
merged 6 commits into from
Feb 2, 2025
Merged

tmux_cmd: Modernize to use text=True #560

merged 6 commits into from
Feb 2, 2025

Conversation

tony
Copy link
Member

@tony tony commented Feb 2, 2025

This is a legacy function dating back to Python 2.x and was derived from pip.

See: #558

Changes

tmux_cmd: Use text=True in subprocess.run

See also:

tmux_cmd: Remove usage of console_to_str()

This is a legacy function dating back to Python 2.x and was derived from pip. text=True makes this unneeded.

tmux_cmd: Remove usage of str_from_console()

Originally from #131. In combination with text=True used in tmux_cmd this is unneeded.

compat: Remove console_to_str() and str_from_console()

Unused.

Summary by Sourcery

Update the tmux_cmd function to use the text=True argument in subprocess.run, simplifying the function by removing the dependency on legacy encoding functions, and removing now-unused encoding functions.

Enhancements:

  • Simplify tmux_cmd by removing the dependency on the legacy console_to_str function and using built-in string conversion.

Tests:

  • Update tests to reflect the changes in tmux_cmd and ensure compatibility with Python 3.

Copy link

codecov bot commented Feb 2, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 88.65%. Comparing base (4bb3158) to head (2a4901d).
Report is 8 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #560      +/-   ##
==========================================
- Coverage   88.68%   88.65%   -0.04%     
==========================================
  Files          36       36              
  Lines        3924     3922       -2     
  Branches      362      362              
==========================================
- Hits         3480     3477       -3     
  Misses        307      307              
- Partials      137      138       +1     

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

Copy link

sourcery-ai bot commented Feb 2, 2025

Reviewer's Guide by Sourcery

This pull request refactors the tmux_cmd function to use the text=True argument in subprocess.run, removing the need for the legacy console_to_str and str_from_console functions. This simplifies the code and improves compatibility with Python 3.

Sequence diagram for simplified tmux_cmd execution flow

Loading
sequenceDiagram
    participant C as Client Code
    participant T as tmux_cmd
    participant S as subprocess.run

    C->>+T: Execute tmux command
    T->>T: Convert args to strings
    T->>+S: Run command with text=True
    S-->>-T: Return stdout/stderr as text
    T->>T: Process stdout/stderr
    T-->>-C: Return processed results

Class diagram showing tmux_cmd changes

Loading
classDiagram
    class TmuxCommandResponse {
        +list[str] stdout
        +list[str] stderr
        +int returncode
        +list[str] cmd
        +__init__(args)
    }
    note for TmuxCommandResponse "Removed console_to_str usage
Now uses text=True in subprocess.run"

File-Level Changes

Change Details Files
Use text=True in subprocess.run
  • Added text=True argument to subprocess.run call.
src/libtmux/common.py
Remove usage of console_to_str()
  • Removed calls to console_to_str when processing stdout and stderr.
src/libtmux/common.py
Remove usage of str_from_console()
  • Removed calls to str_from_console when constructing the command.
  • Cast command arguments to str instead.
src/libtmux/common.py
Remove console_to_str() and str_from_console()
  • Removed the console_to_str function.
  • Removed the str_from_console function.
src/libtmux/_compat.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!
  • Generate a plan of action for an issue: Comment @sourcery-ai plan on
    an issue to generate a plan of action for it.

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

@tony tony force-pushed the console_to_str-fix branch from 10b03d7 to 1a5e69a Compare February 2, 2025 11:22
tony added 3 commits February 2, 2025 05:23

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
With `text=True` being used we can now wrap in `str()` for bytes
to be handled as `str`.
@tony
Copy link
Member Author

tony commented Feb 2, 2025

@sourcery-ai review

@tony tony marked this pull request as ready for review February 2, 2025 11:39
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 - here's some feedback:

Overall Comments:

  • Consider removing the unused 'reraise' function as well since it's not called anywhere (as noted in the TODO comment). This would further help in cleaning up legacy code.
Here's what I looked at during the review
  • 🟢 General issues: all looks good
  • 🟢 Security: all looks good
  • 🟢 Testing: all looks good
  • 🟢 Complexity: 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 2 commits February 2, 2025 05:46
More resilience for `tmux` command across systems. Ability to debug because
escape sequences preserves the byte value for debugging.
@tony tony force-pushed the console_to_str-fix branch from 8ac9fe6 to 2a4901d Compare February 2, 2025 11:46
@tony tony merged commit 6630533 into master Feb 2, 2025
26 checks passed
@tony tony deleted the console_to_str-fix branch February 2, 2025 11:50
@tony tony changed the title tmux_cmd: Remove reliance on console_to_str tmux_cmd: Modernize to use text=True Feb 2, 2025
tony added a commit that referenced this pull request Feb 2, 2025
tony added a commit to tmux-python/tmuxp that referenced this pull request Feb 2, 2025
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.

None yet

1 participant