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 jsanitize when recursive_msonable=True #727

Conversation

Andrew-S-Rosen
Copy link
Contributor

@Andrew-S-Rosen Andrew-S-Rosen commented Nov 30, 2024

Summary

Closes #726 by fixing the jsanitize(recursive_msonable=True) behavior.

Checklist

  • Google format doc strings added. Check with ruff.
  • Type annotations included. Check with mypy.
  • Tests added for new features/fixes.
  • If applicable, new classes/functions/modules have duecredit @due.dcite decorators to reference relevant papers by DOI (example)

Tip: Install pre-commit hooks to auto-check types and linting before every commit:

pip install -U pre-commit
pre-commit install

Summary by CodeRabbit

  • New Features

    • Improved sanitization process for MSONable objects, ensuring consistent handling of nested structures.
  • Tests

    • Updated import for version information in the test suite.
    • Introduced a new test case to validate the jsanitize function with nested MSONable objects.

Copy link

coderabbitai bot commented Nov 30, 2024

Walkthrough

The changes in this pull request focus on the jsanitize function within the monty/json.py file, specifically enhancing its handling of MSONable objects when the recursive_msonable parameter is set to True. The implementation now ensures that MSONable instances are recursively sanitized by wrapping the call to as_dict() with a call to jsanitize. Additionally, modifications in the tests/test_json.py file include a new test scenario for validating the updated functionality, alongside an adjustment in the import statement for versioning.

Changes

File Change Summary
monty/json.py Modified jsanitize to recursively sanitize MSONable objects when recursive_msonable=True.
tests/test_json.py Changed import of tests_version to from monty, added DoubleGoodMSONClass for testing jsanitize.

Assessment against linked issues

Objective Addressed Explanation
The jsanitize function does not behave as expected with recursive_msonable=True (#726)

Possibly related PRs

🐇 In the code, I hop and play,
Sanitizing all the way!
MSONables now clean and bright,
Recursive joy, what a sight!
With tests to ensure it's right,
Hooray for changes, pure delight! 🌟


📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between a3d35a6 and 91e3b7e.

📒 Files selected for processing (2)
  • monty/json.py (1 hunks)
  • tests/test_json.py (2 hunks)
🔇 Additional comments (3)
monty/json.py (1)

718-724: LGTM! The implementation correctly handles nested MSONable objects.

The changes ensure that when recursive_msonable=True, nested MSONable objects are properly sanitized by recursively calling jsanitize on the result of as_dict(). This fixes the bug reported in issue #726.

Let's verify the fix handles deeply nested MSONable objects correctly:

✅ Verification successful

Based on the test file content, I can now provide a definitive response. The test file contains comprehensive test cases that verify the handling of nested MSONable objects, particularly in test_nested_to_from_dict and test_jsanitize methods.

The implementation correctly handles nested MSONable objects.

The test cases confirm that:

  • Deeply nested MSONable objects are properly sanitized when recursive_msonable=True
  • The test test_jsanitize specifically verifies this with DoubleGoodMSONClass containing nested MSONable objects
  • The implementation maintains object structure and values through serialization/deserialization
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Search for test cases that verify nested MSONable objects
# Test: Look for test cases that create nested MSONable objects
rg -A 5 "MSONable.*=.*MSONable" tests/

Length of output: 40


Script:

#!/bin/bash
# Let's try a different approach to find test cases related to nested MSONable objects
# First, let's look for test files that might test jsanitize functionality
rg -l "jsanitize" tests/

# Then, let's look for test classes or functions that might test MSONable objects
rg -l "MSONable" tests/

# Finally, let's examine the content of test_json.py if it exists
fd "test_json.py" tests/ --exec cat {}

Length of output: 28170

tests/test_json.py (2)

18-18: LGTM! Good practice using the package version.

The change to import version from the package instead of locally is a good practice.


579-590: LGTM! Comprehensive test coverage for nested MSONable objects.

The test case thoroughly verifies that:

  1. Nested MSONable objects are properly sanitized
  2. All attributes at each nesting level are preserved
  3. The fix works as intended when recursive_msonable=True

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>, please review it.
    • 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 gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @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 using 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.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

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.

@Andrew-S-Rosen
Copy link
Contributor Author

Closed in favor of #726.

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.

[Bug]: The jsanitize function does not behave as expected with recursive_msonable=True
1 participant