Skip to content

Recognize ignores after multiline strings inside f-strings - #4953

Open
WilliamK112 wants to merge 1 commit into
facebook:mainfrom
WilliamK112:codex/fstring-ignore-4118
Open

WilliamK112 wants to merge 1 commit into
facebook:mainfrom
WilliamK112:codex/fstring-ignore-4118

Conversation

@WilliamK112

Copy link
Copy Markdown
Contributor

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.
  • Full cargo test -p pyrefly --lib: 8565 passed, 3 failed locally in unrelated LSP fixture tests. Two concern whatthepatch import diagnostics; the third is test_workspace_discovers_project_venv, where the test used /opt/anaconda3 instead of its fixture .venv. These fixture files do not enter the changed f-string lexer path. Hosted CI will provide an independent result.

@meta-codesync

meta-codesync Bot commented Sep 16, 2026

Copy link
Copy Markdown
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.)

@github-actions

Copy link
Copy Markdown

According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅

@rchen152 rchen152 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.

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

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Pyrefly ignores suppressions after multi-line nested strings in f-strings

3 participants