Skip to content

[Fix] Replace str(target.kind) with target.kind.name for Target objects#18959

Merged
tqchen merged 1 commit intoapache:mainfrom
akaashrp:target
Apr 2, 2026
Merged

[Fix] Replace str(target.kind) with target.kind.name for Target objects#18959
tqchen merged 1 commit intoapache:mainfrom
akaashrp:target

Conversation

@akaashrp
Copy link
Copy Markdown
Contributor

Replace str(target.kind) with target.kind.name for Target objects since target.kind is a TargetKind object while target.kind.name yields a string describing the target

Copy link
Copy Markdown
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 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
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

medium

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.

Suggested change
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
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

medium

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.

Suggested change
target_kind = target.kind.name
target_kind = getattr(target.kind, "name", str(target.kind))

@tqchen tqchen merged commit 81889de into apache:main Apr 2, 2026
10 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