PyTorch fixes Python 3.9 runtime crash with Claude-authored PR
Claude fixes PEP 604 union type cast error on Python 3.9 in PyTorch.
PyTorch merged PR #184442, authored by Claude, to fix a Python 3.9 compatibility bug in the target-determination (TD) workflow. The issue arose on self-hosted `linux.2xlarge` runners where `python3` resolves to system Python 3.9. The TD workflow uses `from __future__ import annotations` to defer annotation evaluation, but `typing.cast()` is a regular function call, so its first argument is evaluated at runtime. The code used PEP 604 union syntax (`str | None`), which raises `TypeError: unsupported operand type(s) for |: 'type' and 'NoneType'` on Python 3.9. This exception escaped `_get_pr_merge_base` and cascaded through multiple heuristic functions (EditedByPR, HistoricalClassFailurCorrelation, CorrelatedWithHistoricalFailures, etc.), producing a series of errors and effectively breaking the TD workflow.
The fix is simple and robust: quote the cast targets so the type expression is never evaluated—e.g., `cast('str | None', ...)`. Since `typing.cast` ignores its first argument at runtime, a forward-reference string works on every Python version. This avoids any runtime type evaluation while preserving the intent. The PR was approved by multiple reviewers (jithunnair-amd, cyyever, izaitsevfb) and addresses issue #184441. Notably, Claude authored the fix, highlighting the growing role of AI in maintaining open-source infrastructure. The change ensures the TD workflow runs without errors on Python 3.9 runners, which are common in CI environments.
- PEP 604 union syntax (`str | None`) is unsupported in Python 3.9 and causes `TypeError`.
- `typing.cast()` evaluates its first argument at runtime, bypassing `from __future__ import annotations`.
- Fix uses string literals for cast targets (e.g., `'str | None'`) to defer evaluation, working on all Python versions.
Why It Matters
Ensures PyTorch's CI target-determination workflow works reliably on Python 3.9 runners.