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

Fix zpath when passing pathlib.Path #704

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

janosh
Copy link
Contributor

@janosh janosh commented Jul 29, 2024

unblocks materialsproject/atomate2#854 which is failing with

filename = PosixPath('/home/runner/work/atomate2/atomate2/tests/test_data/lobster/lobsteroutputs/mp-2534/lobsterout')

    def zpath(filename: str) -> str:
        """
        Returns an existing (zipped or unzipped) file path given the unzipped
        version. If no path exists, returns the filename unmodified.
    
        Args:
            filename: filename without zip extension
    
        Returns:
            filename with a zip extension (unless an unzipped version
            exists). If filename is not found, the same filename is returned
            unchanged.
        """
        exts = ("", ".gz", ".GZ", ".bz2", ".BZ2", ".z", ".Z")
        for ext in exts:
>           filename = filename.removesuffix(ext)
E           AttributeError: 'PosixPath' object has no attribute 'removesuffix'

Summary by CodeRabbit

  • New Features

    • The zpath function now accepts both string and Path object inputs for greater flexibility.
  • Bug Fixes

    • Enhanced the output consistency of the zpath function by ensuring all return values are strings, regardless of input type.
  • Tests

    • Added comprehensive test cases for the zpath function to cover various scenarios, including handling of different file extensions and non-existent files.

Copy link

coderabbitai bot commented Jul 29, 2024

Walkthrough

The recent changes enhance the zpath function to accept both string and Path object inputs, improving its flexibility. Alongside this modification, the testing suite has been expanded with new cases to ensure thorough coverage of different scenarios, such as handling various file extensions and non-existent files. These updates collectively improve the usability, reliability, and clarity of the zpath functionality.

Changes

Files Change Summary
src/monty/os/path.py Updated zpath function to accept Path objects in addition to strings and ensured consistent string return type.
tests/test_os.py Expanded testing suite with new cases for zpath, including checks for Path input, multiple extensions, and non-existent files. Renamed existing test for clarity.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant zpath
    participant FileSystem

    User->>zpath: Calls zpath("file.txt")
    zpath->>FileSystem: Check if file exists
    alt File exists
        zpath->>FileSystem: Return file path without extension
    else File does not exist
        zpath->>FileSystem: Return original path
    end
Loading

🐰✨
In a world of paths, both new and old,
I hop and I skip, as the stories unfold.
A Path or a string, it matters not,
My little zpath finds treasures, a lot!
With tests galore, I've danced through the night,
Celebrating changes that feel just right!
🌟🌼


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share
Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (invoked as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 069ee2c and 8163a11.

Files selected for processing (2)
  • src/monty/os/path.py (3 hunks)
  • tests/test_os.py (1 hunks)
Additional comments not posted (5)
tests/test_os.py (4)

15-28: Good test coverage for string inputs.

The function test_zpath_str correctly tests the zpath function with string inputs, ensuring the return type and output are as expected.


29-36: Good test coverage for Path inputs.

The function test_zpath_path correctly tests the zpath function with Path inputs, ensuring the return type and output are as expected.


37-52: Good test coverage for multiple extensions.

The function test_zpath_multiple_extensions correctly tests the zpath function with various file extensions, ensuring the output is as expected in different scenarios.


53-59: Good test coverage for non-existent files.

The function test_zpath_nonexistent_file correctly tests the zpath function with non-existent files, ensuring the output is as expected.

src/monty/os/path.py (1)

Line range hint 18-30:
Good enhancement for handling Path inputs.

The function zpath has been correctly modified to handle both string and Path inputs, improving its flexibility. The input is converted to a string before processing, ensuring consistency.

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