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

Close file handles if close_on_exit even if safer.writer() fails; use future type annotations #28

Merged
merged 3 commits into from
Nov 5, 2024

Conversation

rec
Copy link
Owner

@rec rec commented Nov 2, 2024

Summary by Sourcery

Close file handles when 'close_on_exit' is set, even if 'safer.writer()' fails, and update type annotations to use future syntax for better readability.

Bug Fixes:

  • Ensure file handles are closed if 'close_on_exit' is set, even when 'safer.writer()' fails.

Enhancements:

  • Adopt future type annotations for improved code readability and maintainability.

Copy link

sourcery-ai bot commented Nov 2, 2024

Reviewer's Guide by Sourcery

This PR makes two main improvements to the safer library: it ensures proper file handle cleanup when close_on_exit is True even if safer.writer() fails, and modernizes type annotations using Python's newer union operator syntax (|). The changes are implemented by wrapping the main writer logic in a try-except block and updating type hints throughout the codebase.

Updated class diagram for the safer module

classDiagram
    class Safer {
        +writer(stream: Callable | None | IO | Path | str, is_binary: bool | None, close_on_exit: bool, temp_file: bool, chunk_size: int, delete_failures: bool, dry_run: bool | Callable, enabled: bool) Callabe | IO
        +open(name: Path | str, mode: str, buffering: int, encoding: str | None, errors: str | None, newline: str | None, closefd: bool, opener: Callable | None, make_parents: bool, delete_failures: bool, temp_file: bool, dry_run: bool | Callable, enabled: bool) IO
        +closer(stream: IO, is_binary: bool | None, close_on_exit: bool, **kwds) Callable | IO
        +dump(obj, stream: Callable | None | IO | Path | str, dump: Any, **kwargs) Any
        +printer(name: Path | str, mode: str, *args, **kwargs) Iterator[Callable]
    }
    note for Safer "Updated type annotations using Python's newer union operator syntax (|)"
Loading

File-Level Changes

Change Details Files
Added error handling to ensure proper file handle cleanup
  • Wrapped main writer logic in try-except block
  • Added cleanup code to close file handles in exception handler if close_on_exit is True
  • Moved close_on_exit validation check before main logic
safer/__init__.py
Updated type annotations to use modern Python syntax
  • Replaced t.Union[] with
operator for union types
  • Updated return type hints to use
  • Improved code organization and error handling flow
    • Moved dry_run handling to prevent unnecessary operations
    • Restructured conditional blocks for better readability
    • Consolidated stream validation logic inside try block
    safer/__init__.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.
    • Generate a pull request title: Write @sourcery-ai anywhere in the pull
      request title to generate a 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. You can also use
      this command to specify where the summary should be inserted.

    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

    @rec rec mentioned this pull request Nov 2, 2024
    6 tasks
    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 @rec - I've reviewed your changes and they look great!

    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.


    return closer.fp
    except Exception:
    if close_on_exit:
    Copy link

    Choose a reason for hiding this comment

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

    issue (bug_risk): The error handler could mask the original exception if close() fails

    Consider using contextlib.suppress() or logging any cleanup errors while preserving the original exception.

    Copy link
    Owner Author

    Choose a reason for hiding this comment

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

    The exception is reraised in all code paths!

    Copy link

    Choose a reason for hiding this comment

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

    Thank you for your feedback, we will generate fewer comments like this in the future according to the following instructions:

    - Avoid comments that suggest changes without clear justification or evidence of a problem.
    - Ensure comments identify specific issues with potential impact, such as bugs or performance concerns.
    - Provide actionable suggestions that are directly related to the identified issue.
    

    raise ValueError('Stream mode "%s" is not a write mode' % mode)
    if write and mode:
    if not set('w+a').intersection(mode):
    raise ValueError('Stream mode "%s" is not a write mode' % mode)
    Copy link

    Choose a reason for hiding this comment

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

    issue (code-quality): We've found these issues:

    Copy link
    Owner Author

    Choose a reason for hiding this comment

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

    1. The first comment is good and I got rid of all string interpolation.
    2. The second comment is not good, there is no way to simplify the condition.

    Copy link

    Choose a reason for hiding this comment

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

    Thank you for your feedback, we will generate fewer comments like this in the future according to the following instructions:

    - Avoid including comments that list multiple issues without specific examples or context from the code.
    - Do not include comments that rely solely on external links for explanation without providing a summary or rationale within the comment itself.
    - Ensure comments provide actionable feedback that is directly applicable to the code being reviewed.
    

    @rec rec requested a review from Chiemezuo November 3, 2024 17:13
    @rec
    Copy link
    Owner Author

    rec commented Nov 3, 2024

    @Chiemezuo: these are three tiny commits but I thought each would be educational for you.

    @Chiemezuo
    Copy link
    Collaborator

    Thanks!

    @rec rec merged commit 8e15b99 into main Nov 5, 2024
    2 checks passed
    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.

    2 participants