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 tokenization of tqs with escaped newline #305

Merged
merged 1 commit into from Oct 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions reorder_python_imports.py
Expand Up @@ -27,8 +27,8 @@
COMMENT = r'#[^\r\n]*'
NAME = r'\w+'
PREFIX = r'[RrUu]?'
DOUBLE_3 = r'"""[^"\\]*(?:(?:\\.|"(?!""))[^"\\]*)*"""'
SINGLE_3 = r"'''[^'\\]*(?:(?:\\.|'(?!''))[^'\\]*)*'''"
DOUBLE_3 = r'"""[^"\\]*(?:(?:\\.|\\\n|"(?!""))[^"\\]*)*"""'
SINGLE_3 = r"'''[^'\\]*(?:(?:\\.|\\\n|'(?!''))[^'\\]*)*'''"
DOUBLE_1 = r'"[^"\\]*(?:\\.[^"\\]*)*"'
SINGLE_1 = r"'[^'\\]*(?:\\.[^'\\]*)*'"
# END GENERATED
Expand Down
8 changes: 6 additions & 2 deletions testing/generate-tokenize
Expand Up @@ -19,12 +19,16 @@ def raw(s: str) -> str:
return repr(s)


def _escaped_newline(s: str) -> str:
return s.replace(r'\\.|', r'\\.|\\\n|')


def main() -> int:
# only want the prefixes that can be docstrings
string_prefix = '[RrUu]?'

double_3 = f'"""{tokenize.Double3}'
single_3 = f"'''{tokenize.Single3}"
double_3 = f'"""{_escaped_newline(tokenize.Double3)}'
single_3 = f"'''{_escaped_newline(tokenize.Single3)}"
double_1 = f'"{tokenize.Double}'
single_1 = f"'{tokenize.Single}"

Expand Down
12 changes: 11 additions & 1 deletion tests/reorder_python_imports_test.py
Expand Up @@ -26,7 +26,17 @@ def in_tmpdir(tmpdir):


@pytest.mark.parametrize(
's', ("''", '""', 'r"hi"', 'u"hello"', '"""hello\nworld"""', "'''\n'''"),
's',
(
"''",
'""',
'r"hi"',
'u"hello"',
'"""hello\nworld"""',
"'''\n'''",
"'''\\\n'''",
"'''\\\r\n'''",
),
)
def test_tokenize_can_match_strings(s):
tp, pat = TOKENIZE[-1]
Expand Down