Recognize ignores after multiline strings inside f-strings - #4953
Open
WilliamK112 wants to merge 1 commit into
Open
WilliamK112 wants to merge 1 commit into
WilliamK112 wants to merge 1 commit into
Conversation
Contributor
|
This pull request has been imported. If you are a Meta employee, you can view this in D120384969. (Because this pull request was imported automatically, there will not be any future comments.) |
|
According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅ |
rchen152
approved these changes
Sep 18, 2026
rchen152
left a comment
Contributor
There was a problem hiding this comment.
Review automatically exported from Phabricator review in Meta.
meta-codesync Bot
pushed a commit
that referenced
this pull request
Sep 18, 2026
Summary: **Context** Pyrefly must decide whether a hash character starts a comment or is part of a Python string. The old ignore scanner tracked quotes without parsing f-string replacement fields. It could treat a hash inside a nested string as an ignore directive. It could also treat a triple-quoted string in a replacement field as an open string and then miss a real ignore directive on a later line. **Goal** Use the Python parser result to identify comments. Ignore handling must follow the same syntax as the parsed module. This fixes GitHub issue 4118. **Changes** - Read ignore directives only from Comment tokens recorded by the parser. - Remove the old quote scanner and its multiline-string state. - Use the same physical-line rules for line-level and file-level directives. - Test multiline strings in f-strings, raw f-strings, and t-strings. - Test same-quote nesting, CRLF, and bare CR. - Test hash characters inside replacement fields. - Test suppression insertion after an f-string expression. **Behavior** A hash inside an f-string replacement field is no longer treated as a comment. A real ignore directive after a replacement field or a multiline string is now recognized. File-level directives also keep the correct line number when the source contains a bare carriage return. **Source** - GitHub issue: #4118 - GitHub pull request: #4953 - GitHub author: Ching-Wei Kang <164879897+WilliamK112@users.noreply.github.com> Reviewed By: rchen152 Differential Revision: D120384969 fbshipit-source-id: 48c246f82ec96fa4ee8b060ecdc501ffa438f88c
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #4118.
The lightweight suppression scanner treats a triple-quoted string inside an f-string expression as a new, still-open multiline string. That hides real ignore comments on later lines. When a file may contain both an f-string and a multiline string, this change uses the existing Ruff lexer to locate actual comment tokens; other files retain the lightweight scanner. Regression coverage checks the original example, an embedded fake ignore, CRLF input, and an end-to-end suppressed assignment.
Validation:
cargo test -p pyrefly_python --lib: 79 passed.cargo test -p pyrefly test::suppression --lib: 44 passed.python3 test.py --no-test --no-tensor-shapes --no-conformance --no-jsonschema: passed.cargo test -p pyrefly --lib: 8565 passed, 3 failed locally in unrelated LSP fixture tests. Two concernwhatthepatchimport diagnostics; the third istest_workspace_discovers_project_venv, where the test used/opt/anaconda3instead of its fixture.venv. These fixture files do not enter the changed f-string lexer path. Hosted CI will provide an independent result.