Skip to content

fix: handle tuple dtype in DataType repr and error message#1005

Open
CedricConday wants to merge 1 commit into
deepmodeling:masterfrom
CedricConday:fix/datatype-tuple-dtype-repr-989
Open

fix: handle tuple dtype in DataType repr and error message#1005
CedricConday wants to merge 1 commit into
deepmodeling:masterfrom
CedricConday:fix/datatype-tuple-dtype-repr-989

Conversation

@CedricConday

@CedricConday CedricConday commented Jun 30, 2026

Copy link
Copy Markdown

Summary

Fixes #989.

DataType documents and supports dtype as a type or a tuple[type]check() already uses isinstance(data, self.dtype), which is tuple-aware. But __repr__ and the type-mismatch error in check() formatted the dtype via self.dtype.__name__, which raises AttributeError: 'tuple' object has no attribute '__name__' whenever dtype is a tuple. So repr(DataType("x", (list, tuple))) crashes instead of returning a string.

Changes

  • Added a small _dtype_name() helper that renders either a single type or a tuple of types.
  • Used it in __repr__ and in the check() error message.

Testing

  • Added regression tests: repr of a tuple-dtype DataType no longer raises and lists both type names; a type mismatch against a tuple dtype raises a readable DataError (not AttributeError). Both fail on master and pass with this change.
  • ruff check clean; existing TestDataType tests pass.

Disclosure: authored with AI assistance (Claude Code); reviewed by me and I'll shepherd it through review.

Summary by CodeRabbit

  • Bug Fixes
    • Improved how data type information is shown in object summaries and error messages, including support for multiple allowed data types.
    • Fixed validation so mismatched data types now produce a clear, consistent error instead of an unexpected failure.
  • Tests
    • Added regression coverage for tuple-based data type formatting and validation.

DataType accepts dtype as a type or a tuple[type] (check() already uses
isinstance with it), but __repr__ and the type-mismatch error formatted
via self.dtype.__name__, which raised AttributeError for a tuple dtype.
Add a small helper to render either form.

Fixes deepmodeling#989

Assisted-by: Claude Opus 4.8
@dosubot dosubot Bot added size:S This PR changes 10-29 lines, ignoring generated files. bug Something isn't working dpdata labels Jun 30, 2026
@coderabbitai

coderabbitai Bot commented Jun 30, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 72da9943-c4a2-4c22-a75b-5bb4a8dd66eb

📥 Commits

Reviewing files that changed from the base of the PR and between a7a50bf and 45338d8.

📒 Files selected for processing (2)
  • dpdata/data_type.py
  • tests/test_custom_data_type.py

📝 Walkthrough

Walkthrough

Adds a _dtype_name() helper in dpdata/data_type.py that produces a readable string for both single types and tuples of types. DataType.__repr__ and DataType.check() are updated to use this helper, fixing AttributeError when dtype is a tuple. Two regression tests are added.

Tuple dtype fix

Layer / File(s) Summary
_dtype_name helper and DataType formatting fixes
dpdata/data_type.py
Adds _dtype_name(dtype) that joins type names for tuples; __repr__ and check() dtype-mismatch error message both switch from self.dtype.__name__ to _dtype_name(self.dtype).
Regression tests for tuple dtype
tests/test_custom_data_type.py
Imports DataError and adds test_repr_tuple_dtype and test_check_tuple_dtype_error_message to verify correct repr output and proper DataError raising for tuple dtypes.

Estimated code review effort

🎯 1 (Trivial) | ⏱️ ~4 minutes

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly summarizes the main fix: handling tuple dtypes in DataType formatting and error messages.
Linked Issues check ✅ Passed The code and tests address issue #989 by preventing tuple dtypes from crashing repr() and mismatch errors.
Out of Scope Changes check ✅ Passed The changes stay focused on tuple dtype formatting and regression tests, with no unrelated additions.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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

Comment @coderabbitai help to get the list of available commands.

@codspeed-hq

codspeed-hq Bot commented Jul 1, 2026

Copy link
Copy Markdown

Merging this PR will degrade performance by 10.13%

⚠️ Unknown Walltime execution environment detected

Using the Walltime instrument on standard Hosted Runners will lead to inconsistent data.

For the most accurate results, we recommend using CodSpeed Macro Runners: bare-metal machines fine-tuned for performance measurement consistency.

⚠️ Different runtime environments detected

Some benchmarks with significant performance changes were compared across different runtime environments,
which may affect the accuracy of the results.

Open the report in CodSpeed to investigate

❌ 1 regressed benchmark
✅ 1 untouched benchmark

Warning

Please fix the performance issues or acknowledge them on CodSpeed.

Performance Changes

Mode Benchmark BASE HEAD Efficiency
WallTime test_import 10.1 ms 11.2 ms -10.13%

Tip

Investigate this regression by commenting @codspeedbot fix this regression on this PR, or directly use the CodSpeed MCP with your agent.


Comparing CedricConday:fix/datatype-tuple-dtype-repr-989 (45338d8) with master (a7a50bf)

Open in CodSpeed

@codecov

codecov Bot commented Jul 1, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 86.70%. Comparing base (a7a50bf) to head (45338d8).

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #1005      +/-   ##
==========================================
+ Coverage   86.68%   86.70%   +0.01%     
==========================================
  Files          89       89              
  Lines        8120     8124       +4     
==========================================
+ Hits         7039     7044       +5     
+ Misses       1081     1080       -1     

☔ View full report in Codecov by Harness.
📢 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working dpdata size:S This PR changes 10-29 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Code scan] DataType crashes for tuple dtypes despite documenting them

1 participant