-
Notifications
You must be signed in to change notification settings - Fork 2.5k
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 incorrect linenos on fstring tokens with escaped newlines #4423
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
"""Tests for the blib2to3 tokenizer.""" | ||
|
||
import io | ||
import sys | ||
import textwrap | ||
from dataclasses import dataclass | ||
from typing import List | ||
|
||
import black | ||
from blib2to3.pgen2 import token, tokenize | ||
|
||
|
||
@dataclass | ||
class Token: | ||
type: str | ||
string: str | ||
start: tokenize.Coord | ||
end: tokenize.Coord | ||
|
||
|
||
def get_tokens(text: str) -> List[Token]: | ||
"""Return the tokens produced by the tokenizer.""" | ||
readline = io.StringIO(text).readline | ||
tokens: List[Token] = [] | ||
|
||
def tokeneater( | ||
type: int, string: str, start: tokenize.Coord, end: tokenize.Coord, line: str | ||
) -> None: | ||
tokens.append(Token(token.tok_name[type], string, start, end)) | ||
|
||
tokenize.tokenize(readline, tokeneater) | ||
return tokens | ||
|
||
|
||
def assert_tokenizes(text: str, tokens: List[Token]) -> None: | ||
"""Assert that the tokenizer produces the expected tokens.""" | ||
actual_tokens = get_tokens(text) | ||
assert actual_tokens == tokens | ||
|
||
|
||
def test_simple() -> None: | ||
assert_tokenizes( | ||
"1", | ||
[Token("NUMBER", "1", (1, 0), (1, 1)), Token("ENDMARKER", "", (2, 0), (2, 0))], | ||
) | ||
assert_tokenizes( | ||
"'a'", | ||
[ | ||
Token("STRING", "'a'", (1, 0), (1, 3)), | ||
Token("ENDMARKER", "", (2, 0), (2, 0)), | ||
], | ||
) | ||
assert_tokenizes( | ||
"a", | ||
[Token("NAME", "a", (1, 0), (1, 1)), Token("ENDMARKER", "", (2, 0), (2, 0))], | ||
) | ||
|
||
|
||
def test_fstring() -> None: | ||
assert_tokenizes( | ||
'f"x"', | ||
[ | ||
Token("FSTRING_START", 'f"', (1, 0), (1, 2)), | ||
Token("FSTRING_MIDDLE", "x", (1, 2), (1, 3)), | ||
Token("FSTRING_END", '"', (1, 3), (1, 4)), | ||
Token("ENDMARKER", "", (2, 0), (2, 0)), | ||
], | ||
) | ||
assert_tokenizes( | ||
'f"{x}"', | ||
[ | ||
Token("FSTRING_START", 'f"', (1, 0), (1, 2)), | ||
Token("FSTRING_MIDDLE", "", (1, 2), (1, 2)), | ||
Token("LBRACE", "{", (1, 2), (1, 3)), | ||
Token("NAME", "x", (1, 3), (1, 4)), | ||
Token("RBRACE", "}", (1, 4), (1, 5)), | ||
Token("FSTRING_MIDDLE", "", (1, 5), (1, 5)), | ||
Token("FSTRING_END", '"', (1, 5), (1, 6)), | ||
Token("ENDMARKER", "", (2, 0), (2, 0)), | ||
], | ||
) | ||
assert_tokenizes( | ||
'f"{x:y}"\n', | ||
[ | ||
Token(type="FSTRING_START", string='f"', start=(1, 0), end=(1, 2)), | ||
Token(type="FSTRING_MIDDLE", string="", start=(1, 2), end=(1, 2)), | ||
Token(type="LBRACE", string="{", start=(1, 2), end=(1, 3)), | ||
Token(type="NAME", string="x", start=(1, 3), end=(1, 4)), | ||
Token(type="OP", string=":", start=(1, 4), end=(1, 5)), | ||
Token(type="FSTRING_MIDDLE", string="y", start=(1, 5), end=(1, 6)), | ||
Token(type="RBRACE", string="}", start=(1, 6), end=(1, 7)), | ||
Token(type="FSTRING_MIDDLE", string="", start=(1, 7), end=(1, 7)), | ||
Token(type="FSTRING_END", string='"', start=(1, 7), end=(1, 8)), | ||
Token(type="NEWLINE", string="\n", start=(1, 8), end=(1, 9)), | ||
Token(type="ENDMARKER", string="", start=(2, 0), end=(2, 0)), | ||
], | ||
) | ||
assert_tokenizes( | ||
'f"x\\\n{a}"\n', | ||
[ | ||
Token(type="FSTRING_START", string='f"', start=(1, 0), end=(1, 2)), | ||
Token(type="FSTRING_MIDDLE", string="x\\\n", start=(1, 2), end=(2, 0)), | ||
Token(type="LBRACE", string="{", start=(2, 0), end=(2, 1)), | ||
Token(type="NAME", string="a", start=(2, 1), end=(2, 2)), | ||
Token(type="RBRACE", string="}", start=(2, 2), end=(2, 3)), | ||
Token(type="FSTRING_MIDDLE", string="", start=(2, 3), end=(2, 3)), | ||
Token(type="FSTRING_END", string='"', start=(2, 3), end=(2, 4)), | ||
Token(type="NEWLINE", string="\n", start=(2, 4), end=(2, 5)), | ||
Token(type="ENDMARKER", string="", start=(3, 0), end=(3, 0)), | ||
], | ||
) | ||
|
||
|
||
# Run "echo some code | python tests/test_tokenize.py" to generate test cases. | ||
if __name__ == "__main__": | ||
code = sys.stdin.read() | ||
tokens = get_tokens(code) | ||
text = f"assert_tokenizes({code!r}, {tokens!r})" | ||
text = black.format_str(text, mode=black.Mode()) | ||
print(textwrap.indent(text, " ")) |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
consider using the builtin
tokenize
module for generating tests? that way you don't have to verify the locations before adding the test.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure how compatible exactly are the tokens between Python and blib2to3 though.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
They don't match exactly in all cases (e.g., builtin has an extra encoding token at the beginning). We can try to work towards exact compatibility, but not sure that's necessary.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, for now this is good