Skip to content

fix(python): remove broken __instancecheck__/__subclasscheck__ from _ObjectSlotsMeta#498

Merged
junrushao merged 1 commit intoapache:mainfrom
junrushao:main-staged
Mar 7, 2026
Merged

fix(python): remove broken __instancecheck__/__subclasscheck__ from _ObjectSlotsMeta#498
junrushao merged 1 commit intoapache:mainfrom
junrushao:main-staged

Conversation

@junrushao
Copy link
Member

Summary

  • Remove __instancecheck__ and __subclasscheck__ from _ObjectSlotsMeta in python/tvm_ffi/cython/object.pxi. These methods unconditionally returned True for any CObject instance/subclass regardless of which class was being checked, causing incorrect behavior (e.g., isinstance(Map(...), Array) returning True).
  • The methods are unnecessary because all objects returned from C++ are always constructed as proper Object subclasses, so standard Python MRO-based isinstance/issubclass checks work correctly.
  • Update test_dataclass_init.py to assert correct behavior (parent is not an instance of child).
  • Add 22 new regression tests in TestIsinstanceIssubclass covering containers, inheritance, and cross-hierarchy isinstance/issubclass checks.

Test plan

  • All 1517 existing Python tests pass
  • 22 new tests verify correct isinstance/issubclass behavior for containers (Array, List, Map, Dict), inheritance hierarchies, and cross-hierarchy checks
  • Pre-commit hooks pass (ruff, cython-lint, ty, etc.)

🤖 Generated with Claude Code

@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request addresses a fundamental flaw in the Python FFI layer's type checking, where isinstance and issubclass operations for C++-backed objects were unreliable. By removing the problematic metaclass overrides, the system now correctly reflects the underlying C++ object hierarchy. Furthermore, it introduces a sophisticated, performance-optimized type reflection and conversion framework, enhancing the accuracy and flexibility of type-related operations within the TVM FFI. These changes significantly improve the robustness and predictability of type interactions between Python and C++ components.

Highlights

  • Corrected Type Checking Logic: Removed the erroneous __instancecheck__ and __subclasscheck__ methods from _ObjectSlotsMeta in python/tvm_ffi/cython/object.pxi. This resolves a critical bug where isinstance and issubclass checks for CObject subclasses incorrectly returned True for any FFI object, regardless of its actual type, ensuring proper type hierarchy validation.
  • Enhanced Type Reflection and Conversion: Introduced a new CAny class for owned FFI values and significantly enhanced the TypeSchema with robust type checking and conversion capabilities. This includes new methods like from_annotation, check_value, and convert, powered by a new Cython module (type_check.pxi) for efficient, C-level dispatch.
  • Refactored FFI Reflection Mechanisms: Updated the C++ FFI reflection system to support function objects as field setters (kTVMFFIFieldFlagBitSetterIsFunctionObj) and unified object creation through CreateEmptyObject. This streamlines object instantiation and field manipulation, allowing for more flexible and powerful reflection.
  • Comprehensive Regression Testing: Added 22 new regression tests in TestIsinstanceIssubclass to thoroughly verify the corrected isinstance/issubclass behavior across various FFI object types, including containers, inheritance hierarchies, and cross-hierarchy checks, ensuring the fix is robust.

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Changelog
  • include/tvm/ffi/c_api.h
    • Added kTVMFFIFieldFlagBitSetterIsFunctionObj flag to TVMFFIFieldFlagBitMask.
    • Updated TVMFFIFieldInfo::setter type to void* to support function object setters.
  • include/tvm/ffi/function.h
    • Added CreateEmptyObject function to create objects using native C++ creators or Python's __ffi_new__.
    • Added HasCreator function to check if a type supports reflection creation.
  • include/tvm/ffi/reflection/accessor.h
    • Added CallFieldSetter to dispatch field setting based on kTVMFFIFieldFlagBitSetterIsFunctionObj.
    • Updated FieldSetter::operator() to use CallFieldSetter for field assignment.
    • Modified SetFieldToDefault to utilize CallFieldSetter for default value application.
  • include/tvm/ffi/reflection/creator.h
    • Updated ObjectCreator constructor to use HasCreator for type validation.
    • Modified ObjectCreator::operator() to use CreateEmptyObject and CallFieldSetter.
  • include/tvm/ffi/reflection/init.h
    • Updated MakeInit to use HasCreator for creator availability checks.
    • Modified MakeInit to use CreateEmptyObject for object instantiation.
    • Changed MakeInit to use CallFieldSetter for setting object fields.
  • include/tvm/ffi/reflection/overload.h
    • Cast ReflectionDefBase::FieldSetter<T> to void* for info.setter assignment.
  • include/tvm/ffi/reflection/registry.h
    • Cast FieldSetter<T> to void* for info.setter assignment.
  • python/tvm_ffi/init.py
    • Imported CAny from core module.
  • python/tvm_ffi/core.pyi
    • Added CAny class definition with __init__, type_index, and to_py methods.
    • Added origin_type_index attribute to TypeSchema.
    • Added from_type_index, from_annotation, check_value, convert, and to_json static methods/methods to TypeSchema.
  • python/tvm_ffi/cython/base.pxi
    • Added cause_chain and extra_context fields to TVMFFIErrorMetadata.
    • Introduced new kTVMFFIFieldFlagBitMask values for structural equality/hashing, representation, and comparison.
    • Added kTVMFFIFieldFlagBitSetterIsFunctionObj flag.
    • Updated TVMFFIFieldInfo::setter to void*.
    • Added TVMFFISEqHashKind enum.
    • Changed TVMFFITypeMetadata::total_size to int32_t and added structural_eq_hash_kind.
    • Added new C API functions for type registration and error handling.
    • Modified TVMFFIPyCallFieldSetter signature to include field_flags.
  • python/tvm_ffi/cython/core.pyx
    • Included type_check.pxi for type conversion logic.
  • python/tvm_ffi/cython/object.pxi
    • Removed __instancecheck__ and __subclasscheck__ methods from _ObjectSlotsMeta.
    • Added flags attribute to FieldSetter cdef class.
    • Added CAny cdef class for owned TVMFFIAny values, including __cinit__, __init__, __dealloc__, type_index, to_py, and __repr__.
  • python/tvm_ffi/cython/tvm_ffi_python_helpers.h
    • Updated TVMFFIPyCallManager::SetField to handle field_flags and dispatch setter calls accordingly.
    • Modified TVMFFIPyCallFieldSetter to accept field_flags for setter dispatch.
  • python/tvm_ffi/cython/type_check.pxi
    • Added new file type_check.pxi implementing a Cython-based type conversion and checking system for TypeSchema.
    • Defined _ConvertError for signaling conversion failures.
    • Implemented _TypeConverter cdef class for efficient dispatch of type conversions.
    • Provided various _tc_convert_* and _dispatch_* functions for handling different primitive and composite types.
    • Included logic for __tvm_ffi_value__ protocol fallback and recursion depth protection.
    • Exposed _type_schema_check_value and _type_schema_convert for Python-level TypeSchema methods.
  • python/tvm_ffi/cython/type_info.pxi
    • Imported typing, collections.abc, cached_property, and UnionType.
    • Updated FieldSetter to include flags and use the new TVMFFIPyCallFieldSetter signature.
    • Expanded _TYPE_SCHEMA_ORIGIN_CONVERTER and added _ORIGIN_TO_TYPE_INDEX and _TYPE_INDEX_TO_ORIGIN maps.
    • Added origin_type_index to TypeSchema and updated __post_init__ logic.
    • Implemented _converter as a cached_property for lazy converter building.
    • Added from_type_index, from_annotation, check_value, convert, and to_json methods to TypeSchema.
    • Introduced helper functions _annotation_union and _annotation_cobject for from_annotation.
  • python/tvm_ffi/dataclasses/init.py
    • Imported and exposed the new Field class.
  • python/tvm_ffi/dataclasses/field.py
    • Added new file field.py defining the Field class for describing fields in Python-defined TVM-FFI types, including various configuration options.
  • rust/tvm-ffi-sys/src/c_api.rs
    • Updated TVMFFIFieldInfo::setter to *mut c_void to align with C API changes.
  • src/ffi/extra/reflection_extra.cc
    • Modified MakeObjectFromPackedArgs to use CreateEmptyObject for object creation.
    • Updated MakeObjectFromPackedArgs to use reflection::CallFieldSetter for field assignment.
  • src/ffi/extra/serialization.cc
    • Updated ObjectGraphDeserializer to use CreateEmptyObject for object instantiation.
    • Modified ObjectGraphDeserializer to use reflection::CallFieldSetter for field assignment during deserialization.
  • src/ffi/object.cc
    • Initialized structural_eq_hash_kind for Object in TypeTable.
  • tests/python/test_dataclass_init.py
    • Updated test_parent_isinstance_child_due_to_metaclass to test_parent_not_isinstance_child and changed assertion to assert not isinstance.
  • tests/python/test_object.py
    • Added TestIsinstanceIssubclass class with 22 new regression tests for isinstance/issubclass behavior across various FFI object types.
Activity
  • All 1517 existing Python tests passed.
  • 22 new tests were added to verify correct isinstance/issubclass behavior for containers, inheritance hierarchies, and cross-hierarchy checks.
  • Pre-commit hooks (ruff, cython-lint, ty, etc.) passed.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

…ObjectSlotsMeta

_ObjectSlotsMeta.__instancecheck__ and __subclasscheck__ unconditionally
returned True for any CObject instance/subclass, regardless of which
class was being checked. This caused incorrect behavior such as
isinstance(Map(...), Array) returning True.

The fix removes both methods entirely. They are unnecessary because all
objects returned from C++ are always constructed as proper Object
subclasses (never raw CObject instances), so standard Python MRO-based
isinstance/issubclass checks work correctly.

- Remove __instancecheck__ and __subclasscheck__ from _ObjectSlotsMeta
- Update test_dataclass_init.py to assert correct behavior (parent is
  not an instance of child)
- Add 22 new tests in TestIsinstanceIssubclass covering containers,
  inheritance, and cross-hierarchy isinstance/issubclass checks
Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request correctly fixes a bug in _ObjectSlotsMeta where isinstance and issubclass checks were behaving incorrectly. The removal of the custom __instancecheck__ and __subclasscheck__ methods is the right approach, and the comprehensive new tests in test_object.py provide excellent coverage for this fix.

The PR also introduces a significant number of new features and refactorings to the FFI and reflection systems, including support for function object setters, a unified object creation mechanism, and a powerful new type checking and conversion system for TypeSchema. These additions appear to be of high quality and well-engineered.

I have one suggestion regarding the consistency of how Python's list type is handled in the new type conversion logic.

I am having trouble creating individual review comments. Click here to see my feedback.

python/tvm_ffi/cython/type_check.pxi (873)

medium

For clarity and consistency, should origin == "list" also use _dispatch_list? The from_annotation(list) method in type_info.pxi maps Python's list to TypeSchema("List"), which correctly uses _dispatch_list here. Since Python's list is mutable, mapping it to tvm_ffi.List (which is also mutable) is semantically more intuitive than mapping to the immutable tvm_ffi.Array.

The comment on line 871 states that "list" is mapped to Array, which is what the current code does. However, this seems inconsistent with how from_annotation(list) is handled. If this change is made, the comment on line 871 should also be updated.

        conv.dispatch = _dispatch_list if origin in ("List", "list") else _dispatch_array

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request addresses a critical bug in the Python FFI where isinstance and issubclass checks were behaving incorrectly for CObject subclasses. The fix, which removes the custom __instancecheck__ and __subclasscheck__ methods from _ObjectSlotsMeta, is correct and allows Python to use its standard MRO-based checks. The addition of 22 new regression tests in TestIsinstanceIssubclass thoroughly verifies the corrected behavior.

Beyond the bug fix, this PR introduces significant enhancements to the FFI reflection system to better support Python-defined objects. This includes:

  • A mechanism to create Python-defined objects from C++ using a __ffi_new__ attribute.
  • Support for Python functions as field setters, adding flexibility.
  • A new CAny class for owned FFI value containers.
  • A greatly improved TypeSchema with robust type checking and conversion capabilities, including the ability to create schemas from Python type annotations.
  • A new tvm_ffi.dataclasses.Field descriptor for a more declarative way of defining FFI object fields.

The changes are extensive, touching C++, Python/Cython, and Rust bindings, but they are consistent and well-implemented. The overall code quality is high, and the new features are a valuable addition to TVM's Python FFI capabilities.

Note: Security Review did not run due to the size of the PR.

@junrushao junrushao merged commit 721d878 into apache:main Mar 7, 2026
8 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