Skip to content

Conversation

caic99
Copy link
Member

@caic99 caic99 commented Aug 1, 2025

Currently dpdata throws a warning if an existed data type is registered again, even if with the same definition. This PR adds a check to skip if the registered data types are identicle.

Summary by CodeRabbit

  • New Features

    • Improved object comparison and display for data types, allowing equality checks and clearer string representations.
  • Bug Fixes

    • Reduced unnecessary warnings when registering identical data types; warnings are now only shown if duplicate registrations differ.
  • Tests

    • Added tests for data type comparison, representation, and registration behavior.
    • Removed outdated test for duplicate data type registration.

Copy link

codspeed-hq bot commented Aug 1, 2025

CodSpeed Performance Report

Merging #856 will not alter performance

Comparing caic99:fix-dup-datatype (2a8ac2e) with devel (5aade0d)

Summary

✅ 2 untouched benchmarks

Copy link

coderabbitai bot commented Aug 1, 2025

📝 Walkthrough

Walkthrough

The changes introduce __eq__ and __repr__ methods to the DataType class, enabling comparison and improved string representation. The System.register_data_type method now only warns on duplicate data type registration if the definitions differ. Corresponding tests were added for these behaviors, and a redundant test was removed.

Changes

Cohort / File(s) Change Summary
DataType Enhancements
dpdata/data_type.py
Added __eq__ and __repr__ methods to the DataType class for equality comparison and string representation.
System Registration Logic
dpdata/system.py
Modified System.register_data_type to emit a warning only when registering a data type with the same name but a different definition. The warning message was updated accordingly.
DataType Tests and Cleanup
tests/test_custom_data_type.py
Added TestDataType class with tests for DataType equality, representation, and registration warning logic. Removed the now-redundant test_duplicated_data_type from DeepmdLoadDumpCompTest.

Sequence Diagram(s)

sequenceDiagram
    participant Tester
    participant DataType
    participant System

    Tester->>DataType: Create instance (dt1)
    Tester->>DataType: Create instance (dt2)
    Tester->>DataType: Compare dt1 == dt2 (__eq__)
    Tester->>DataType: Print representation (__repr__)

    Tester->>System: register_data_type(dt1)
    Tester->>System: register_data_type(dt2)
    alt dt1 and dt2 are identical
        System-->>Tester: No warning
    else dt1 and dt2 differ
        System-->>Tester: Emit UserWarning
    end
Loading

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~8 minutes

Note

⚡️ Unit Test Generation is now available in beta!

Learn more here, or try it out under "Finishing Touches" below.

✨ Finishing Touches
  • 📝 Generate Docstrings
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ 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.
    • Explain this complex logic.
    • 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 explain this code block.
    • @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 explain its main purpose.
    • @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.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

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 generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai generate unit tests to generate unit tests for this 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.

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: 1

🧹 Nitpick comments (1)
dpdata/system.py (1)

1106-1110: Add stacklevel parameter to warnings.warn call.

The static analysis tool correctly identifies that the warnings.warn call should include a stacklevel parameter to point to the correct location in the call stack where the warning originates.

-                if dt != dtypes_dict[dt.name]:
-                    warnings.warn(
-                        f"Data type {dt.name} is registered twice with different definitions; only the newly registered one will be used.",
-                        UserWarning,
-                    )
+                if dt != dtypes_dict[dt.name]:
+                    warnings.warn(
+                        f"Data type {dt.name} is registered twice with different definitions; only the newly registered one will be used.",
+                        UserWarning,
+                        stacklevel=2,
+                    )
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 5aade0d and cfa28d6.

📒 Files selected for processing (3)
  • dpdata/data_type.py (1 hunks)
  • dpdata/system.py (1 hunks)
  • tests/test_custom_data_type.py (2 hunks)
🧰 Additional context used
🪛 GitHub Actions: Python package
dpdata/data_type.py

[error] 168-168: dpdata.data_type.DataError: test_diff not found in data. This error occurs in multiple test cases during data validation checks in dpdata system initialization.

dpdata/system.py

[error] 221-221: Data check failed in dpdata.system.System.check_data() due to missing 'test_diff' data type in the dataset.

tests/test_custom_data_type.py

[error] 129-129: Unit tests failed due to dpdata.data_type.DataError: test_diff not found in data during setup of TestDeepmdLoadDumpCompUnlabeled tests.

🪛 Ruff (0.12.2)
dpdata/system.py

1107-1107: No explicit stacklevel keyword argument found

Set stacklevel=2

(B028)

🔇 Additional comments (4)
dpdata/data_type.py (2)

67-88: LGTM! Well-implemented equality comparison.

The __eq__ method correctly implements equality comparison by checking the instance type and comparing all relevant attributes. The implementation follows Python best practices by returning False for non-DataType objects and comparing all fields that define the identity of a DataType.


90-102: LGTM! Clear and informative string representation.

The __repr__ method provides a comprehensive string representation that includes all relevant attributes. The format is clear and follows Python conventions for repr methods.

dpdata/system.py (1)

1106-1111: LGTM! Improved warning logic prevents unnecessary alerts.

The updated logic correctly uses the new __eq__ method to only warn when data types with the same name have different definitions. This addresses the PR objective of skipping warnings for identical duplicate registrations.

tests/test_custom_data_type.py (1)

13-35: LGTM! Comprehensive tests for DataType methods.

The test methods for __eq__ and __repr__ are well-structured and cover the expected behaviors thoroughly, including edge cases like comparing with non-DataType objects.

Copy link

codecov bot commented Aug 1, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 85.41%. Comparing base (ee0ca89) to head (2a8ac2e).
⚠️ Report is 3 commits behind head on devel.

Additional details and impacted files
@@           Coverage Diff           @@
##            devel     #856   +/-   ##
=======================================
  Coverage   85.41%   85.41%           
=======================================
  Files          82       82           
  Lines        7567     7577   +10     
=======================================
+ Hits         6463     6472    +9     
- Misses       1104     1105    +1     

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

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Signed-off-by: Chun Cai <[email protected]>
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

🧹 Nitpick comments (1)
dpdata/system.py (1)

1105-1110: Good improvement to the duplicate registration logic!

The condition now correctly checks for both name collision AND different definitions before warning, which aligns perfectly with the PR objective. However, consider adding stacklevel=2 to the warning for better debugging experience.

             if dt.name in dtypes_dict and dt != dtypes_dict[dt.name]:
                 warnings.warn(
                     f"Data type {dt.name} is registered twice with different definitions; only the newly registered one will be used.",
                     UserWarning,
+                    stacklevel=2,
                 )

Regarding the past review comment about merging lines 1105-1106: The current multi-line format is actually more readable given the complexity of the condition and warning message, so the suggestion may not be applicable here.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 51d6790 and 2a8ac2e.

📒 Files selected for processing (1)
  • dpdata/system.py (1 hunks)
🧰 Additional context used
🪛 Ruff (0.12.2)
dpdata/system.py

1106-1106: No explicit stacklevel keyword argument found

Set stacklevel=2

(B028)

⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (1)
  • GitHub Check: benchmark

@njzjz njzjz merged commit efa66a7 into deepmodeling:devel Aug 2, 2025
12 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