You are viewing a plain text version of this content. The canonical link for it is here.
Posted to discuss-archive@tvm.apache.org by "junrushao (via GitHub)" <gi...@apache.org> on 2026/04/20 06:56:14 UTC
[PR] fix(enum): add class iteration and len [tvm-ffi]
junrushao opened a new pull request, #558:
URL: https://github.com/apache/tvm-ffi/pull/558
(no comment)
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: discuss-archive-unsubscribe@tvm.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: discuss-archive-unsubscribe@tvm.apache.org
For additional commands, e-mail: discuss-archive-help@tvm.apache.org
Re: [PR] fix(enum): add class iteration and len [tvm-ffi]
Posted by "gemini-code-assist[bot] (via GitHub)" <gi...@apache.org>.
gemini-code-assist[bot] commented on code in PR #558:
URL: https://github.com/apache/tvm-ffi/pull/558#discussion_r3108805685
##########
python/tvm_ffi/dataclasses/enum.py:
##########
@@ -453,6 +453,25 @@ class body instead.
return EnumAttrMap(cls, name, default=default)
+def _enum_meta_iter(cls: type) -> Iterator[Any]:
+ if not issubclass(cls, Enum):
+ raise TypeError(f"'{type(cls).__name__}' object is not iterable")
+ return iter(cls.by_value)
+
+
+def _enum_meta_len(cls: type) -> int:
+ if not issubclass(cls, Enum):
+ raise TypeError(f"object of type '{type(cls).__name__}' has no len()")
+ return len(cls.by_value)
+
+
+_ENUM_META = type(Enum)
+if not hasattr(_ENUM_META, "__iter__"):
+ _ENUM_META.__iter__ = _enum_meta_iter
+if not hasattr(_ENUM_META, "__len__"):
+ _ENUM_META.__len__ = _enum_meta_len
Review Comment:

Monkey-patching the metaclass of `Enum` (which is likely `type` or a shared base metaclass) is a fragile approach and can lead to unexpected side effects or runtime errors. For example, if `type(Enum)` is an immutable built-in type like `type`, this code will raise a `TypeError` at runtime.
Since `Enum` is defined in this file, it is much cleaner and more idiomatic to define a dedicated metaclass and use it in the class definition. This avoids global state modification and the need for defensive `issubclass` checks.
Example implementation:
```python
class EnumMeta(type(Object)):
def __iter__(cls):
return iter(cls.by_value)
def __len__(cls):
return len(cls.by_value)
class Enum(Object, metaclass=EnumMeta):
...
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: discuss-archive-unsubscribe@tvm.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: discuss-archive-unsubscribe@tvm.apache.org
For additional commands, e-mail: discuss-archive-help@tvm.apache.org
Re: [PR] fix(enum): add class iteration and len [tvm-ffi]
Posted by "junrushao (via GitHub)" <gi...@apache.org>.
junrushao merged PR #558:
URL: https://github.com/apache/tvm-ffi/pull/558
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: discuss-archive-unsubscribe@tvm.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: discuss-archive-unsubscribe@tvm.apache.org
For additional commands, e-mail: discuss-archive-help@tvm.apache.org