fix(py_class): support tvm_ffi.dtype and tvm_ffi.Device as field type annotations#540
Merged
junrushao merged 1 commit intoapache:mainfrom Apr 12, 2026
Conversation
…type annotations Architecture ------------ `TypeSchema.from_annotation()` in the Cython layer only recognized the C-level `DataType` / `Device` cdef classes via identity checks (`annotation is DataType`). The public Python wrappers — `tvm_ffi.dtype` (a `str` subclass in `_dtype.py`) and `tvm_ffi.Device` (re-exported from `_device.py`) — are distinct types, so using them as `@py_class` field annotations raised `TypeError: Cannot convert <class '...'> to TypeSchema`. The fix widens each identity check to also match the wrapper classes held in the existing `_CLASS_DTYPE` / `_CLASS_DEVICE` module-level sentinels. Public Interfaces ----------------- No new public API. Existing `@py_class` fields annotated with `tvm_ffi.dtype` or `tvm_ffi.Device` now work where they previously raised `TypeError`. UI/UX ----- No changes. Behavioral Changes ------------------ `@py_class` field annotations `tvm_ffi.dtype` and `tvm_ffi.Device` are now accepted and mapped to the `"dtype"` / `"Device"` TypeSchema entries, matching the behavior that the C-level cdef classes already had. Docs ---- No documentation changes required. Tests ----- Added `TestDtypeDeviceFields` with 6 regression tests in `tests/python/test_dataclass_py_class.py`: - dtype field creation and identity - dtype field setter mutation - Device field creation - dtype + Device fields together with a str field - Optional[dtype] field (None and valued) - Optional[Device] field (None and valued) Untested Edge Cases ------------------- - Subclasses of `tvm_ffi.dtype` or `tvm_ffi.Device` used as annotations (identity checks will not match subclasses). - Thread-safety of `_CLASS_DTYPE` / `_CLASS_DEVICE` lazy initialization under free-threaded builds.
Contributor
There was a problem hiding this comment.
Code Review
This pull request enhances the TypeSchema logic in python/tvm_ffi/cython/type_info.pxi to correctly recognize tvm_ffi.dtype and tvm_ffi.Device as valid field types for @py_class decorated objects. The update ensures that annotations are matched against both the standard classes and their internal Cython-defined counterparts (_CLASS_DTYPE and _CLASS_DEVICE). A comprehensive set of regression tests has been added in tests/python/test_dataclass_py_class.py to verify support for these types in mandatory, optional, and multi-field configurations. I have no feedback to provide.
yaoyaoding
approved these changes
Apr 12, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
TypeSchema.from_annotation()in the Cython layer only recognized the C-levelDataType/Devicecdef classes for dtype/device annotations (annotation is DataType). The public Python wrapper classes —tvm_ffi.dtype(class dtype(str)in_dtype.py) andtvm_ffi.Device— are distinct types, so using them as@py_classfield type annotations raisedTypeError: Cannot convert <class '...'> to TypeSchema._CLASS_DTYPE/_CLASS_DEVICEmodule-level sentinels.Optionalvariants.Test plan
uv run pytest -vvs tests/python/test_dataclass_py_class.py— all 342 tests pass (including 6 newTestDtypeDeviceFieldstests)🤖 Generated with Claude Code