[Fix] Replace str(target.kind) with target.kind.name for Target objects#18959
[Fix] Replace str(target.kind) with target.kind.name for Target objects#18959tqchen merged 1 commit intoapache:mainfrom
Conversation
There was a problem hiding this comment.
Code Review
This pull request updates several files to use target.kind.name instead of str(target.kind) and updates type hints in docstrings to include tvm.target.Target. Review feedback points out that directly accessing .name on target.kind may cause an AttributeError if target.kind is a string, suggesting a more robust approach using getattr.
| target = target.get("kind", "") | ||
| elif hasattr(target, "kind"): | ||
| target = str(target.kind) | ||
| target = target.kind.name |
There was a problem hiding this comment.
Using target.kind.name is less robust than the previous str(target.kind). In TVM, some objects like tvm.runtime.Module have a kind attribute that is a string. For such objects, hasattr(target, "kind") will be true, but target.kind.name will raise an AttributeError because strings do not have a .name attribute. Since str(TargetKind) is equivalent to TargetKind.name, it is safer to use getattr(target.kind, "name", str(target.kind)) to maintain compatibility with non-Target objects.
| target = target.kind.name | |
| target = getattr(target.kind, "name", str(target.kind)) |
| target_kind = target["kind"] | ||
| elif hasattr(target, "kind"): | ||
| target_kind = str(target.kind) | ||
| target_kind = target.kind.name |
There was a problem hiding this comment.
Similar to the change in tvm.runtime.enabled, using target.kind.name here can cause an AttributeError if target is an object where kind is a string (e.g., a tvm.runtime.Module). Using getattr with a fallback to str() ensures robustness while still preferring the .name attribute if present.
| target_kind = target.kind.name | |
| target_kind = getattr(target.kind, "name", str(target.kind)) |
Replace
str(target.kind)withtarget.kind.nameforTargetobjects sincetarget.kindis aTargetKindobject whiletarget.kind.nameyields a string describing the target