Skip to content

Update subprocess types and improve error handling in run.py #485

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 3 commits into from
Feb 22, 2025

Conversation

tony
Copy link
Member

@tony tony commented Feb 17, 2025

Changes

  • Added proper type annotations for stdout/stderr line handling
  • Improved error handling for subprocess file descriptors
  • Added constant for error messages to improve maintainability
  • Fixed type conversion for command arguments in error reporting

Technical Details

  1. Type System Improvements:

    • Added explicit type annotations for stdout_lines and stderr_lines as list[bytes]
    • Fixed cmd argument type in CommandError by ensuring string conversion
  2. Error Handling:

    • Added explicit None checks for stdout/stderr file descriptors
    • Introduced PIPE_NOT_SET_ERR constant for consistent error messaging
    • Improved error messages to follow style guidelines
  3. Code Quality:

    • Replaced filter(None, ...) with list comprehension for better readability
    • Fixed list handling in all_output to maintain consistent types
    • Improved string joining operations for better type safety

Testing

  • All mypy type checks pass successfully
  • Code formatting follows project standards (verified with ruff)
  • Existing functionality remains unchanged

Impact

These changes improve code reliability and maintainability without changing the underlying behavior. The updates make the code more robust by:

  • Catching potential file descriptor issues earlier
  • Making type handling more explicit and safer
  • Improving error message consistency

Summary by Sourcery

Improve error handling and type annotations in the run function. Standardize error messages using constants and ensure consistent type handling for outputs. No user-facing behavior changes are included.

Bug Fixes:

  • Fixed potential RuntimeError caused by unchecked file descriptors in the run function.
  • Fixed type conversion for command arguments in error reporting to ensure string representation.
  • Corrected list handling in all_output to maintain consistent types across different code paths

Chores:

  • Improved type annotations across the run module using more concise imports from the typing module.
  • Introduced a constant PIPE_NOT_SET_ERR for consistent error messaging related to file descriptors.
  • Replaced filter(None, ...) with list comprehension for improved readability.
  • Improved string joining operations for better type safety

Summary by Sourcery

Improves error handling and type annotations in the run function. Standardizes error messages using constants and ensures consistent type handling for outputs.

Bug Fixes:

  • Fixes a potential error caused by unchecked file descriptors.
  • Fixes type conversion for command arguments in error reporting to ensure string representation.
  • Corrects list handling in all_output to maintain consistent types across different code paths.

Enhancements:

  • Improves type annotations across the run module using more concise imports from the typing module.
  • Replaces filter(None, ...) with list comprehension for improved readability.
  • Improves string joining operations for better type safety.

Chores:

  • Introduces a constant PIPE_NOT_SET_ERR for consistent error messaging related to file descriptors.

Copy link

sourcery-ai bot commented Feb 17, 2025

Reviewer's Guide by Sourcery

This pull request improves the reliability and maintainability of the run function by adding type annotations, improving error handling, and enhancing code quality. It addresses potential file descriptor issues, makes type handling more explicit, and improves error message consistency.

Sequence diagram for run function with improved error handling

sequenceDiagram
  participant User
  participant run
  participant subprocess.Popen
  participant proc.stdout
  participant proc.stderr
  participant CommandError

  User->>run: run(args, ...)
  run->>subprocess.Popen: Popen(args, stdout=PIPE, stderr=PIPE, ...)
  subprocess.Popen-->>run: proc
  loop Read stdout lines
    run->>proc.stdout: readlines()
    proc.stdout-->>run: line
    run->>run: Filter and join stdout lines
  end
  alt code != 0 and check_returncode
    loop Read stderr lines
      run->>proc.stderr: readlines()
      proc.stderr-->>run: line
      run->>run: Filter and join stderr lines
    end
    run->>CommandError: raise CommandError(output, returncode, cmd)
    CommandError-->>User: Exception
  else code == 0 or not check_returncode
    run-->>User: output
  end
Loading

Updated class diagram for CommandError

classDiagram
  class CommandError {
    +output: str
    +returncode: int
    +cmd: str | list[str]
    +__init__(output: str, returncode: int, cmd: str | list[str])
  }
  note for CommandError "cmd is now explicitly converted to string"
Loading

File-Level Changes

Change Details Files
Improved type annotations for stdout/stderr line handling and command arguments.
  • Added explicit type annotations for stdout_lines and stderr_lines as list[bytes].
  • Fixed cmd argument type in CommandError by ensuring string conversion.
src/libvcs/_internal/run.py
Enhanced error handling for subprocess file descriptors.
  • Added explicit None checks for stdout/stderr file descriptors.
  • Introduced PIPE_NOT_SET_ERR constant for consistent error messaging.
src/libvcs/_internal/run.py
Improved code quality and readability.
  • Replaced filter(None, ...) with list comprehension for better readability.
  • Fixed list handling in all_output to maintain consistent types.
  • Improved string joining operations for better type safety.
src/libvcs/_internal/run.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

Copy link

codecov bot commented Feb 17, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 54.09%. Comparing base (9979bd2) to head (d4015ff).
Report is 12 commits behind head on master.

Additional details and impacted files
@@           Coverage Diff           @@
##           master     #485   +/-   ##
=======================================
  Coverage   54.08%   54.09%           
=======================================
  Files          40       40           
  Lines        3635     3627    -8     
  Branches      793      793           
=======================================
- Hits         1966     1962    -4     
+ Misses       1318     1314    -4     
  Partials      351      351           

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

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 using io.TextIOWrapper to handle encoding and decoding directly when reading from stdout/stderr.
Here's what I looked at during the review
  • 🟢 General issues: all looks good
  • 🟢 Security: all looks good
  • 🟢 Testing: all looks good
  • 🟡 Complexity: 1 issue found
  • 🟢 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 tony force-pushed the subprocess-run-mypy branch from 3ba540f to 4dcd9db Compare February 17, 2025 16:04
@tony
Copy link
Member Author

tony commented Feb 17, 2025

@sourcery-ai review

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 using io.TextIOWrapper to avoid manual encoding/decoding.
  • The error messages could be improved by including the command that failed.
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 tony force-pushed the subprocess-run-mypy branch 4 times, most recently from a933a65 to f47cfd2 Compare February 22, 2025 15:37
@tony
Copy link
Member Author

tony commented Feb 22, 2025

@sourcery-ai review

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 using subprocess.run directly instead of wrapping it, as it provides similar functionality with a more standard interface.
  • The type annotation changes look good, but ensure they align with the actual types returned by the subprocess methods to avoid runtime errors.
Here's what I looked at during the review
  • 🟡 General issues: 1 issue found
  • 🟢 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 tony force-pushed the subprocess-run-mypy branch from 49750a7 to aa03d5f Compare February 22, 2025 15:44
tony added 2 commits February 22, 2025 09:45
uv run ruff check --select ALL . --fix --unsafe-fixes --preview --show-fixes; uv run ruff format .

Fixed 6 errors:
- src/libvcs/_internal/subprocess.py:
    3 × PYI061 (redundant-none-literal)
    3 × UP045 (non-pep604-annotation-optional)
@tony tony force-pushed the subprocess-run-mypy branch from 9653f76 to d4015ff Compare February 22, 2025 15:45
@tony
Copy link
Member Author

tony commented Feb 22, 2025

@sourcery-ai review

@tony tony merged commit a948f7d into master Feb 22, 2025
7 checks passed
@tony tony deleted the subprocess-run-mypy branch February 22, 2025 15:50
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