-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[mypyc] Use mypy.FORMAT_RE and ConversionSpecifier for % interpolation (
#10877) mypy.checkstrformat offers regex and ConversionSpecifier for tokenizer, thus this PR: * deletes the redundant code * uses ConversionSpecifier as FormatOp
- Loading branch information
1 parent
a54a177
commit 84504b0
Showing
4 changed files
with
74 additions
and
57 deletions.
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
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 |
---|---|---|
@@ -1,16 +1,31 @@ | ||
import unittest | ||
from typing import List | ||
|
||
from mypyc.irbuild.format_str_tokenizer import tokenizer_printf_style | ||
|
||
|
||
class TestStringFormatting(unittest.TestCase): | ||
|
||
def test_tokenizer_printf_style(self) -> None: | ||
assert tokenizer_printf_style("I'm %s, id years old") == \ | ||
(["I'm ", ', id years old'], ['%s']) | ||
assert tokenizer_printf_style("Test: %i%%, Test: %02d, Test: %.2f") == \ | ||
(['Test: ', '', ', Test: ', ', Test: ', ''], ['%i', '%%', '%02d', '%.2f']) | ||
assert tokenizer_printf_style("ioasdfyuia%i%%%20s%d%sdafafadfa%s%d%x%E%.2f") == \ | ||
(['ioasdfyuia', '', '', '', '', 'dafafadfa', '', '', '', '', ''], | ||
['%i', '%%', '%20s', '%d', '%s', '%s', '%d', '%x', '%E', '%.2f']) | ||
assert tokenizer_printf_style("Special: %#20.2f%d, test: ") == \ | ||
(['Special: ', '', ', test: '], ['%#20.2f', '%d']) | ||
|
||
def tokenizer_printf_style_helper(format_str: str, | ||
literals: List[str], conversion: List[str]) -> bool: | ||
l, specs = tokenizer_printf_style(format_str) | ||
return literals == l and conversion == [x.whole_seq for x in specs] | ||
|
||
assert tokenizer_printf_style_helper( | ||
"I'm %s, id years old", | ||
["I'm ", ', id years old'], | ||
['%s']) | ||
assert tokenizer_printf_style_helper( | ||
"Test: %i%%, Test: %02d, Test: %.2f", | ||
['Test: ', '', ', Test: ', ', Test: ', ''], | ||
['%i', '%%', '%02d', '%.2f']) | ||
assert tokenizer_printf_style_helper( | ||
"ioasdfyuia%i%%%20s%d%sdafafadfa%s%d%x%E%.2f", | ||
['ioasdfyuia', '', '', '', '', 'dafafadfa', '', '', '', '', ''], | ||
['%i', '%%', '%20s', '%d', '%s', '%s', '%d', '%x', '%E', '%.2f']) | ||
assert tokenizer_printf_style_helper( | ||
"Special: %#20.2f%d, test: ", | ||
['Special: ', '', ', test: '], | ||
['%#20.2f', '%d']) |