Skip to content

[Dynamo] Fix stale funcname scope for one-line definitions - #199357

Closed
Mohit-25-tech wants to merge 1 commit into
pytorch:mainfrom
Mohit-25-tech:fix/dynamo-funcname-one-line-def-199314
Closed

Mohit-25-tech wants to merge 1 commit into
pytorch:mainfrom
Mohit-25-tech:fix/dynamo-funcname-one-line-def-199314

Conversation

@Mohit-25-tech

Copy link
Copy Markdown
Contributor

Issue being fixed

Fixes #199314

BC-breaking?

No.

Summary (human written only)

Fix stale function/class scopes in Dynamo's source-name cache for one-line
definitions.

Agent details

Prepared with assistance from Codex.

Dynamo's source-name cache removes definition scopes on DEDENT. One-line
def and class bodies produce no DEDENT, so their names remain active
and incorrectly qualify subsequent source lines.

For example:

class E(Exception): pass
def f():
    return 1

The line inside f is reported as E.f instead of f. Consecutive
one-line overload definitions can similarly produce names such as
Module.to.to.to.to._call_impl.

After recording a definition's logical NEWLINE, this change removes
its scope if the next significant token is not INDENT. Comments and
blank lines are ignored when checking for an indented body. The
declaration's own name is preserved, along with the existing
DEDENT-based handling of multiline definitions and nested scopes.

Added regression coverage for one-line classes and functions, consecutive
overloads, nested methods, decorators, one-line async functions, and
multiline signatures with comments and blank lines before the body.

Validation used a prebuilt CPU nightly with the modified parser loaded
into the installed package:

  • The focused regression test passed.
  • Eight explicit scope cases passed.
  • Applicable Python lint/format checks passed.
  • Pyrefly reported zero errors.
  • The existing funcname test retained its Windows skip.
  • A full misc-file attempt with fail-fast stopped at its first test
    because MSVC cl was unavailable. That test failed identically with
    the original parser, confirming an environment limitation. The full
    file is not claimed to pass.

cc @chauhang @penguinwu @voznesenskym @EikanWang @jgong5 @Guobing-Chen @XiaobingSuper @zhuhaozhe @blzheng @wenzhe-nrv @jiayisunx @kadeng @amjames @jataylo @azahed98

@pytorch-bot

pytorch-bot Bot commented Oct 1, 2026 •

Copy link
Copy Markdown

PR Status: Approved 🚀

Please fix all CI failures and trigger merge by commenting "@pytorchbot merge".

✅ AUTOMATED REVIEW - Ready for human review:

This PR fixes torch/_dynamo/funcname_cache.py so that a one-line def or class
(body on the header line) closes its own scope at its logical NEWLINE, and it
adds a regression test in test/dynamo/test_misc.py. The review recommends
Approve: nothing blocks the PR, and the only remaining point is a cosmetic
naming nit.

Findings (1):

[minor] torch/_dynamo/funcname_cache.py:72
Non-blocking, carried over from the last review: `next_token` holds an index (it
is used as `tokens[next_token]`), not a token. That clashes with the loop's
`i`/`token` naming, and `j` or `next_idx` would read better. The `next_token ==
len(tokens)` check on line 78 can never be true, because `generate_tokens`
always ends with `ENDMARKER` and a failed tokenize never produces a partial
list. The check is harmless and matches the defensive bounds check on line 57,
so keeping it is fine.

Reviewed commit: 10eff85

Review run

🐛 Report a bad review

🔗 Helpful Links

🧪 See artifacts and rendered test results at hud.pytorch.org/pr/199357

Note: Links to docs will display an error until the docs builds have been completed.

✅ No Failures

As of commit 10eff85 with merge base 68f0e54 (image):
💚 Looks good so far! There are no failures yet. 💚

This comment was automatically generated by Dr. CI and updates every 15 minutes.

@pytorch-bot pytorch-bot Bot added ciflow/inductor ciflow/torchtitan Run TorchTitan integration tests module: dynamo labels Oct 1, 2026
@pytorch-bot

pytorch-bot Bot commented Oct 1, 2026 •

Copy link
Copy Markdown

Workflows were awaiting approval. CI has now been triggered for the ciflow labels on this PR.

@pytorch-bot

pytorch-bot Bot commented Oct 1, 2026

Copy link
Copy Markdown

The following ciflow label(s) have been added but CI has not been triggered yet because the workflows are awaiting approval:

  • ciflow/inductor
  • ciflow/torchtitan

Once a maintainer approves the workflows (scroll to the bottom of the PR page), the corresponding CI jobs will be triggered automatically. Please ping one of the reviewers if you do not have access to approve and run workflows.

@egeboy35

egeboy35 commented Oct 2, 2026

Copy link
Copy Markdown
Contributor

Drafted by Claude (AI) for @egeboy35, the reporter of #199314.

This PR's funcname_cache.py was checked with the AST comparison described in #199314: every statement line of the torch source (510,281 lines) now gets the same qualified name as ast gives, versus 47,765 wrong lines on main, and the existing test_funcname_cache expectation still matches. Checked by running the file on its own under Python 3.12 and 3.14.

@Mohit-25-tech

Copy link
Copy Markdown
Contributor Author

Thanks for the additional validation! The AST comparison across the PyTorch source tree is really helpful confirmation that the fix handles the single-line definition cases correctly without introducing incorrect qualified names. I'll keep this validation in mind as I finalize the PR.

@github-actions github-actions Bot added bot-triaged This is a label only to be used by the auto triage bot missing actionable issue A PR without a linked actionable issue or maintainer sponsor triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module labels Oct 2, 2026
@guilhermeleobas
guilhermeleobas requested a review from rtimpe October 2, 2026 18:33
@soulitzer soulitzer added missing actionable issue A PR without a linked actionable issue or maintainer sponsor and removed missing actionable issue A PR without a linked actionable issue or maintainer sponsor labels Oct 15, 2026
@Mohit-25-tech

Copy link
Copy Markdown
Contributor Author

@pytorchbot label "topic: not user facing"

@pytorch-bot pytorch-bot Bot added the topic: not user facing topic category label Nov 15, 2026
@Mohit-25-tech

Copy link
Copy Markdown
Contributor Author

@pytorchbot triage

@pytorch-bot pytorch-bot Bot added open source and removed triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module bot-triaged This is a label only to be used by the auto triage bot missing actionable issue A PR without a linked actionable issue or maintainer sponsor open source labels Nov 15, 2026
@github-actions github-actions Bot added bot-triaged This is a label only to be used by the auto triage bot triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module labels Nov 15, 2026
@pytorch-bot pytorch-bot Bot added the in progress PR is being worked on by the author, until the automated review accepts it label Nov 15, 2026
@Mohit-25-tech

Copy link
Copy Markdown
Contributor Author

Hi maintainers,

A quick follow-up on this PR. The linked issue #199314 is now marked actionable, and the latest displayed checks, including EasyCLA, are passing.

Could one of the requested reviewers take a look at the implementation when convenient? I'd appreciate feedback on the scope-handling approach and am happy to make any necessary adjustments.

Thanks for your time!

github-actions[bot]

This comment was marked as outdated.

@guilhermeleobas

Copy link
Copy Markdown
Collaborator

@pytorchbot merge -r

@pytorch-bot

pytorch-bot Bot commented Nov 16, 2026

Copy link
Copy Markdown

Pull workflow has not been scheduled for the PR yet. It could be because author doesn't have permissions to run those or skip-checks keywords were added to PR/commits, aborting merge. Please get/give approval for the workflows and/or remove skip ci decorators before next merge attempt. If you think this is a mistake, please contact PyTorch Dev Infra.

@guilhermeleobas

Copy link
Copy Markdown
Collaborator

@pytorchbot rebase

@pytorchmergebot

Copy link
Copy Markdown
Collaborator

@pytorchbot started a rebase job onto refs/remotes/origin/viable/strict. Check the current status here

The source-name cache removes definition scopes on DEDENT, which is absent
for same-line bodies. Those names therefore leaked into subsequent source
lines. Pop a definition after its logical newline when the next significant
token is not INDENT, preserving the name on the declaration itself.

Add coverage for one-line classes, functions, overloads and async functions,
alongside nested scopes and multiline signatures with comments before the body.

Fixes pytorch#199314

Test Plan:
Used a prebuilt CPU nightly and loaded the modified parser into the installed
package with the temporary run_funcname_tests.py bootstrap; no source build.

```powershell
$env:TORCHINDUCTOR_WINDOWS_TESTS='1'
.venv/Scripts/python.exe -I agent_space/run_funcname_tests.py test_misc.py MiscTests.test_funcname_cache MiscTests.test_funcname_cache_single_line -v
.venv/Scripts/python.exe -I agent_space/run_funcname_tests.py test_logging.py -k trace_source -v
.venv/Scripts/python.exe -I agent_space/run_funcname_tests.py test_logging.py -k trace_call -v
.venv/Scripts/python.exe -I agent_space/check_funcname_scopes.py
.venv/Scripts/python.exe -I agent_space/run_funcname_typecheck.py
.venv/Scripts/python.exe -I agent_space/run_python_lint.py --fix
git diff --check -- torch/_dynamo/funcname_cache.py test/dynamo/test_misc.py
```

The new regression passed and its snapshot failed against the original parser.
Ten AST scope checks and the Module._call_impl reproduction passed. Five
trace_source tests passed; the simple test hit a fallback because MSVC is absent.
The existing funcname test is skipped on Windows; four trace_call tests require
Python >=3.11 and were skipped on Python 3.10. Pyrefly reported zero errors.
The seven selected Python linters passed via Spin's helper. A temporary wrapper
normalized Windows extended paths for the unchanged official formatter.

Prepared with assistance from Codex.
@pytorchmergebot

Copy link
Copy Markdown
Collaborator

Successfully rebased fix/dynamo-funcname-one-line-def-199314 onto refs/remotes/origin/viable/strict, please pull locally before adding more changes (for example, via git checkout fix/dynamo-funcname-one-line-def-199314 && git pull --rebase)

@pytorchmergebot
pytorchmergebot force-pushed the fix/dynamo-funcname-one-line-def-199314 branch from b32ba17 to 10eff85 Compare November 16, 2026 20:09

@github-actions github-actions 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.

Automated review of 10eff85: nothing blocking; ready for human review.

This PR fixes torch/_dynamo/funcname_cache.py so that a one-line def or class (body on the header line) closes its own scope at its logical NEWLINE, and it adds a regression test in test/dynamo/test_misc.py. The review recommends Approve: nothing blocks the PR, and the only remaining point is a cosmetic naming nit.

⚪ 1 non-blocking finding

next_token = i + 1

Non-blocking, carried over from the last review: next_token holds an index (it is used as tokens\[next_token\]), not a token. That clashes with the loop's i/token naming, and j or next_idx would read better. The next_token == len(tokens) check on line 78 can never be true, because generate_tokens always ends with ENDMARKER and a failed tokenize never produces a partial list. The check is harmless and matches the defensive bounds check on line 57, so keeping it is fine.

This review is AI-generated and advisory. Each new automated review replaces the previous one. Report a bad review

@guilhermeleobas

Copy link
Copy Markdown
Collaborator

@pytorchbot merge

@pytorch-bot pytorch-bot Bot added the ciflow/trunk Trigger trunk jobs on your pull request label Nov 16, 2026
@pytorchmergebot

Copy link
Copy Markdown
Collaborator

Merge started

Your change will be merged once all checks pass (ETA 0-4 Hours).

Learn more about merging in the wiki.

Questions? Feedback? Please reach out to the PyTorch DevX Team

Advanced Debugging
Check the merge workflow status
here

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bot-triaged This is a label only to be used by the auto triage bot ciflow/inductor ciflow/torchtitan Run TorchTitan integration tests ciflow/trunk Trigger trunk jobs on your pull request in progress PR is being worked on by the author, until the automated review accepts it Merged module: dynamo open source topic: not user facing topic category triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[dynamo] funcname_cache gives wrong qualified names after a def/class with a same-line body

7 participants