Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix lexing single-quoted f-string with multi-line format spec #7787

Merged
merged 5 commits into from
Oct 5, 2023

Conversation

dhruvmanila
Copy link
Member

Summary

Reported at python/cpython#110259

Test Plan

Add test cases for the fix and update the snapshots

@dhruvmanila
Copy link
Member Author

Current dependencies on/for this PR:

This comment was auto-generated by Graphite.

@dhruvmanila dhruvmanila added the bug Something isn't working label Oct 3, 2023
@codspeed-hq
Copy link

codspeed-hq bot commented Oct 3, 2023

CodSpeed Performance Report

Merging #7787 will improve performances by 2.5%

Comparing dhruv/multiline-fstring-with-format-spec (9e6ae10) with main (17fba99)

Summary

⚡ 1 improvements
✅ 24 untouched benchmarks

Benchmarks breakdown

Benchmark main dhruv/multiline-fstring-with-format-spec Change
linter/default-rules[pydantic/types.py] 38.9 ms 38 ms +2.5%

@github-actions
Copy link
Contributor

github-actions bot commented Oct 3, 2023

PR Check Results

Ecosystem

✅ ecosystem check detected no changes.

@dhruvmanila dhruvmanila force-pushed the dhruv/multiline-fstring-with-format-spec branch 3 times, most recently from 68f3d35 to 9c3edf5 Compare October 4, 2023 03:31
@dhruvmanila dhruvmanila marked this pull request as ready for review October 4, 2023 10:44
@dhruvmanila dhruvmanila force-pushed the dhruv/multiline-fstring-with-format-spec branch from 9c3edf5 to 745a878 Compare October 5, 2023 13:46
@dhruvmanila
Copy link
Member Author

The CPython PR has been merged: python/cpython#110271

@pablogsal
Copy link

Friendly reminder to review the discussion in python/cpython#110259 so you end implementing the same as CPython 😉

@dhruvmanila dhruvmanila force-pushed the dhruv/multiline-fstring-with-format-spec branch from 745a878 to 8638838 Compare October 5, 2023 13:49
@dhruvmanila
Copy link
Member Author

Friendly reminder to review the discussion in python/cpython#110259 so you end implementing the same as CPython 😉

I did, thanks for the heads up! :)

@dhruvmanila
Copy link
Member Author

Actually, there's more to it for single-quoted f-string that I missed (sorry!). The newline should basically end the lexing of format spec which we currently don't.

@pablogsal
Copy link

Actually, there's more to it for single-quoted f-string that I missed (sorry!). The newline should basically end the lexing of format spec which we currently don't.

That's why I suggested to double check :)

@dhruvmanila
Copy link
Member Author

That's why I suggested to double check :)

Hehe, I must've missed that part 😅


I'm going to try a couple solutions and look at the benchmarks, please bear with me :)

Comment on lines +205 to +220
FStringMiddle {
value: "a",
is_raw: false,
},
99..100,
),
(
NonLogicalNewline,
100..101,
),
(
Name {
name: "b",
},
109..110,
),
Copy link
Member Author

Choose a reason for hiding this comment

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

It's important that b is a Name token as we ended the format spec lexing at the newline. This will help the parser to raise a syntax error (unexpected token 'b') similar to the CPython implementation.

f'__{
	x:a
		b
}__'

@dhruvmanila dhruvmanila changed the title Allow multi-line f-string with format spec Fix lexing single-quoted f-string with multi-line format spec Oct 5, 2023
@dhruvmanila
Copy link
Member Author

Update after the first review is that the following case will now raise a syntax error as it should:

f'__{
	x:a
		b
}__'

The reason it didn't earlier is because we didn't end the format spec lexing after we reached the newline (after a). Now, we do which means the lexer will emit a Name token for b for which the parser will raise an unexpected token error.

@dhruvmanila dhruvmanila merged commit 709abd5 into main Oct 5, 2023
16 checks passed
@dhruvmanila dhruvmanila deleted the dhruv/multiline-fstring-with-format-spec branch October 5, 2023 17:42
konstin pushed a commit that referenced this pull request Oct 11, 2023
## Summary

Reported at python/cpython#110259

## Test Plan

Add test cases for the fix and update the snapshots
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants