Skip to content

[BugFix][TVMScript] Fix invalid f-string format spec causing TypeError on Python 3.14 - #19362

Merged
tlopex merged 3 commits into
apache:mainfrom
swjng:fix/tvmscript-fstring-py314-compat
Apr 6, 2026
Merged

tlopex merged 3 commits into
apache:mainfrom
swjng:fix/tvmscript-fstring-py314-compat

Conversation

@swjng

@swjng swjng commented Apr 6, 2026 •

Copy link
Copy Markdown
Contributor

Problem

On Python 3.14, any use of TVMScript raises a TypeError before the module body is even parsed:

TypeError: unsupported format string passed to type.__format__

The traceback points to python/tvm/script/parser/core/diagnostics.py:120:

raise TypeError(f"Source for {obj:!r} not found")

Root Cause

{obj:!r} is an invalid f-string expression. The : introduces a format_spec, so !r is passed to type.__format__ as a format string — which it does not support.

The intended syntax for a repr() conversion is {obj!r} (no colon).

Python 3.14 re-implemented f-string parsing under PEP 701 and now strictly validates format specs, surfacing this latent bug. Python 3.10–3.13 silently passed the invalid spec to __format__ and happened not to raise in most code paths, so the bug went unnoticed.

Fix

- raise TypeError(f"Source for {obj:!r} not found")
+ raise TypeError(f"Source for {obj!r} not found")

One character change. Valid across all Python versions >= 3.6.

Testing

Verified on Python 3.14.2 (darwin/arm64):

  • TVMScript ir_module + prim_func parses and compiles correctly after the fix
  • Full TVMScript test suite: 628 passed, 1 xfailed (the 1 failure in test_tvmscript_roundtrip.py::test_roundtrip[relax_symbolic_size_var] is pre-existing and unrelated to this change)

…ect_getfile

Replace `{obj:!r}` with `{obj!r}` in diagnostics.py. The colon-prefixed
form was treated as a format_spec by the f-string parser, which is invalid.
Python 3.14 re-implemented f-string parsing (PEP 701) and now strictly
validates format specs, causing a TypeError at import time when using
TVMScript on Python 3.14.

The correct syntax for repr() conversion in f-strings is `{obj!r}` (no colon).

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

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.

Code Review

This pull request fixes an f-string formatting error in the _patched_inspect_getfile function within diagnostics.py, correcting the syntax from {obj:!r} to {obj!r}. I have no feedback to provide as there are no review comments.

@tlopex tlopex left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM! Thank you for the contribution! @soowonj

@tlopex
tlopex merged commit 93e28d1 into apache:main Apr 6, 2026
7 checks passed
Aharrypotter pushed a commit to Aharrypotter/tvm that referenced this pull request Apr 10, 2026
…r on Python 3.14 (apache#19362)

## Problem

On Python 3.14, any use of TVMScript raises a `TypeError` before the
module body is even parsed:

```
TypeError: unsupported format string passed to type.__format__
```

The traceback points to
`python/tvm/script/parser/core/diagnostics.py:120`:

```python
raise TypeError(f"Source for {obj:!r} not found")
```

## Root Cause

`{obj:!r}` is an invalid f-string expression. The `:` introduces a
`format_spec`, so `!r` is passed to `type.__format__` as a format string
— which it does not support.

The intended syntax for a `repr()` conversion is `{obj!r}` (no colon).

Python 3.14 re-implemented f-string parsing under [PEP
701](https://peps.python.org/pep-0701/) and now strictly validates
format specs, surfacing this latent bug. Python 3.10–3.13 silently
passed the invalid spec to `__format__` and happened not to raise in
most code paths, so the bug went unnoticed.

## Fix

```diff
- raise TypeError(f"Source for {obj:!r} not found")
+ raise TypeError(f"Source for {obj!r} not found")
```

One character change. Valid across all Python versions >= 3.6.

## Testing

Verified on Python 3.14.2 (darwin/arm64):

- TVMScript `ir_module` + `prim_func` parses and compiles correctly
after the fix
- Full TVMScript test suite: **628 passed, 1 xfailed** (the 1 failure in
`test_tvmscript_roundtrip.py::test_roundtrip[relax_symbolic_size_var]`
is pre-existing and unrelated to this change)
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