Skip to content

Ignore brackets inside f-string text in check_complete 馃馃 - #15407

Open
MohammadHijjawi97 wants to merge 1 commit into
ipython:mainfrom
MohammadHijjawi97:fix-fstring-brackets-check-complete
Open

MohammadHijjawi97 wants to merge 1 commit into
ipython:mainfrom
MohammadHijjawi97:fix-fstring-brackets-check-complete

Conversation

@MohammadHijjawi97

Copy link
Copy Markdown

Fixes #14481 馃馃

Since Python 3.12, f-strings are tokenized as FSTRING_START / FSTRING_MIDDLE / FSTRING_END. A literal [ or ] in the text part of an f-string therefore becomes a token whose .string is a bracket. make_tokens_by_line and has_sunken_brackets counted those as real brackets, so check_complete:

Only OP tokens are now counted as brackets. Regression cases added to test_check_complete_param.

Since Python 3.12, f-strings are tokenized into FSTRING_START /
FSTRING_MIDDLE / FSTRING_END, so a literal "[" or "]" in the text part
of an f-string shows up as a token whose string is a bracket.
make_tokens_by_line and has_sunken_brackets counted those as real
brackets, which made check_complete:

- report 'complete' for an indented block whose last line is a comment
  following e.g. `x = f"[{i}] "`, so the block ran early (ipython#14481);
- report 'invalid' for valid code like `x = f"]"`.

Only count OP tokens as brackets.

Fixes ipython#14481

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Comment line executes loop if the previous line is an f-string?

1 participant