Skip to content

Support semantic tokens for format specifiers in logging calls - #4003

Closed
kavix wants to merge 1 commit into
meta:mainfrom
kavix:fix-logging-format-specifiers
Closed

kavix wants to merge 1 commit into
meta:mainfrom
kavix:fix-logging-format-specifiers

Conversation

@kavix

@kavix kavix commented Jul 1, 2026

Copy link
Copy Markdown
Contributor

Summary

Fixes #3908

This PR adds a formatSpecifier semantic token for Python logging calls, similar to how clangd highlights format specifiers in C/C++.

Since Ruff flags Python logging f-strings (for lazy evaluation performance reasons via rule G004), users are encouraged to use percent-style formatting. This change ensures that when users follow this best practice, their format specifiers (%s, %d, %(name)s, etc.) are properly syntax-highlighted by the IDE for readability.

Approach & Details

  1. Registered formatSpecifier: Added this custom token type to the SemanticTokensLegends registry.
  2. Detection Heuristic: Introduced a lightweight AST heuristic (is_logger_candidate) to detect common logging functions (e.g. logger.info, logging.debug, LOGGER.warn) to avoid costly symbol resolutions on the hot path.
  3. Format Specifier Parsing: Added a OnceLock cached regex that strictly follows Python's printf-style format specifiers to scan string literals passed to the detected logging calls.
  4. Source Propagation: Updated the process_ast semantic token builder pipeline to propagate the source text string down through the visitor so that string literals can be sliced and matched against the regex.
  5. Testing: Added the semantic_tokens_format_specifier integration test to lsp_interaction which successfully asserts the LSP token mapping over the string "Hello %s %d".

@meta-cla meta-cla Bot added the cla signed label Jul 1, 2026
@kavix kavix changed the title Support semantic tokens for format specifiers in logging calls (fixes… Support semantic tokens for format specifiers in logging calls Jul 1, 2026
@github-actions github-actions Bot added the size/l label Jul 1, 2026
@kinto0 kinto0 self-assigned this Jul 7, 2026
@meta-codesync

meta-codesync Bot commented Jul 7, 2026

Copy link
Copy Markdown
Contributor

@kinto0 has imported this pull request. If you are a Meta employee, you can view this in D110947960.

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

hey, thanks for taking this on. left a few suggestions

Comment thread pyrefly/lib/lsp/wasm/semantic_tokens.rs Outdated
Comment thread pyrefly/lib/test/lsp/lsp_interaction/semantic_tokens.rs
Comment thread pyrefly/lib/state/semantic_tokens.rs Outdated
Comment thread pyrefly/lib/state/semantic_tokens.rs Outdated
Comment thread pyrefly/lib/state/semantic_tokens.rs Outdated
Comment thread pyrefly/lib/state/semantic_tokens.rs Outdated
Comment thread pyrefly/lib/state/semantic_tokens.rs Outdated
Comment thread pyrefly/lib/test/lsp/lsp_interaction/semantic_tokens.rs
@github-actions

This comment has been minimized.

@github-actions github-actions Bot added size/l and removed size/l labels Jul 8, 2026
@kavix
kavix requested a review from kinto0 July 8, 2026 22:20

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

remaining questions about the logging calls

Comment thread pyrefly/lib/state/semantic_tokens.rs Outdated
Comment thread pyrefly/lib/test/lsp/lsp_interaction/semantic_tokens.rs
@kavix
kavix force-pushed the fix-logging-format-specifiers branch from 67ba85c to 5056bfd Compare July 9, 2026 07:53
@github-actions github-actions Bot added size/l and removed size/l labels Jul 9, 2026
@kavix
kavix requested a review from kinto0 July 9, 2026 07:55
@kinto0

kinto0 commented Jul 9, 2026 •

Copy link
Copy Markdown
Contributor

thanks! looks good to me. then we will get another quick set of eyes on it and then we can merge!

@github-actions

github-actions Bot commented Jul 9, 2026

Copy link
Copy Markdown

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

@kavix

kavix commented Jul 13, 2026

Copy link
Copy Markdown
Contributor Author

Hi @kinto0, could you please Re-review this PR ? Thanks!

@yangdanny97 yangdanny97 left a comment

Copy link
Copy Markdown
Collaborator

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.

@github-actions github-actions Bot added size/l and removed size/l labels Jul 13, 2026

@kavix kavix left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Great feedback — thanks for the detailed review.

I agree on both points and will follow up with

  1. Additional format-specifier coverage for %%, named + width/precision cases, and malformed/incomplete specifiers.
  2. Clear documentation of is_logger_candidate boundaries (expected false positives/false negatives), plus focused tests for those limits where practical.

@kavix
kavix requested a review from yangdanny97 July 13, 2026 15:10

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

sorry, a few more changes were requested (I don't think you saw these yet)

Comment thread pyrefly/lib/state/semantic_tokens.rs Outdated
Comment thread pyrefly/lib/state/semantic_tokens.rs Outdated
Comment thread pyrefly/lib/state/semantic_tokens.rs Outdated
Comment thread pyrefly/lib/state/semantic_tokens.rs
@github-actions

Copy link
Copy Markdown

This pull request has been automatically marked as stale because it has not had recent activity for more than 2 weeks.

If you are still working on this this pull request, please add a comment or push new commits to keep it active. Otherwise, please unassign yourself and allow someone else to take over.

Thank you for your contributions!

@github-actions github-actions Bot added the stale label Jul 29, 2026
@github-actions github-actions Bot added size/xl and removed size/l labels Aug 18, 2026
@kavix
kavix requested a review from kinto0 August 18, 2026 09:49
@kavix
kavix force-pushed the fix-logging-format-specifiers branch from c213f8a to 36dffcd Compare August 18, 2026 09:52
@github-actions github-actions Bot added size/xl and removed size/xl labels Aug 18, 2026
…3908)

Why:
Highlight printf-style format specifiers inside logging and formatting strings, similar to clangd in C/C++. Since Ruff flags Python logging f-strings for lazy evaluation performance reasons, users are encouraged to use percent-style formatting, which should be highlighted for readability.

What:
- Add `formatSpecifier` semantic token type to LSP legends.
- Scan string literals passed to calls or as left operand of `%` operator for printf-style format specifiers using a regex.
- Scan raw source slice directly to accurately map token byte offsets without character drift from escape sequences.
- Consume `%%` as literal percent without emitting tokens.
- Add comprehensive integration tests covering basic specifiers, escaped prefixes, `%%`, named arguments, width, precision, and string `%` modulo operations.

Fixes meta#3908
@kavix
kavix force-pushed the fix-logging-format-specifiers branch from 36dffcd to 70cb635 Compare August 18, 2026 10:02
@github-actions github-actions Bot added size/xl and removed size/xl labels Aug 18, 2026
@kavix

kavix commented Aug 23, 2026

Copy link
Copy Markdown
Contributor Author

Hi @kinto0 @yangdanny97 , could you please Re-review this PR ? Thanks!

@github-actions

Copy link
Copy Markdown

Closing this PR because linked issue #3908 was closed.

@github-actions github-actions Bot closed this Sep 11, 2026
@kinto0

kinto0 commented Sep 11, 2026

Copy link
Copy Markdown
Contributor

hey, sorry about the delay and the back/forth. it actually looks like we already support these format specifiers syntactically in editors that combine our results with textmate grammars (see the issue). if it's absolutely necessary, we might add a textmate parser for all syntax, but until then, I don't think the pyrefly analysis should have this role.

I'm sorry I marked it a good-first-issue. I now can see our struggles implementing it and realize it is difficult and out of scope for pyrefly.

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.

highlight for %s in string

3 participants