Skip to content

Commit 1b9530c

Browse files
authored
bpo-23894: make lib2to3 recognize f-strings (#1733)
Note: this doesn't unpack f-strings into the underlying JoinedStr AST. Ideally we'd fully implement JoinedStr here but given its additional complexity, I think this is worth bandaiding as is. This unblocks tools like https://github.com/google/yapf to format 3.6 syntax using f-strings.
1 parent ddbfa2c commit 1b9530c

File tree

2 files changed

+7
-1
lines changed

2 files changed

+7
-1
lines changed

Lib/lib2to3/pgen2/tokenize.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ def maybe(*choices): return group(*choices) + '?'
7474
Single3 = r"[^'\\]*(?:(?:\\.|'(?!''))[^'\\]*)*'''"
7575
# Tail end of """ string.
7676
Double3 = r'[^"\\]*(?:(?:\\.|"(?!""))[^"\\]*)*"""'
77-
_litprefix = r"(?:[uUrRbB]|[rR][bB]|[bBuU][rR])?"
77+
_litprefix = r"(?:[uUrRbBfF]|[rR][bB]|[bBuU][rR])?"
7878
Triple = group(_litprefix + "'''", _litprefix + '"""')
7979
# Single-line ' or " string.
8080
String = group(_litprefix + r"'[^\n'\\]*(?:\\.[^\n'\\]*)*'",
@@ -110,12 +110,14 @@ def maybe(*choices): return group(*choices) + '?'
110110
"r'''": single3prog, 'r"""': double3prog,
111111
"u'''": single3prog, 'u"""': double3prog,
112112
"b'''": single3prog, 'b"""': double3prog,
113+
"f'''": single3prog, 'f"""': double3prog,
113114
"ur'''": single3prog, 'ur"""': double3prog,
114115
"br'''": single3prog, 'br"""': double3prog,
115116
"rb'''": single3prog, 'rb"""': double3prog,
116117
"R'''": single3prog, 'R"""': double3prog,
117118
"U'''": single3prog, 'U"""': double3prog,
118119
"B'''": single3prog, 'B"""': double3prog,
120+
"F'''": single3prog, 'F"""': double3prog,
119121
"uR'''": single3prog, 'uR"""': double3prog,
120122
"Ur'''": single3prog, 'Ur"""': double3prog,
121123
"UR'''": single3prog, 'UR"""': double3prog,
@@ -127,13 +129,15 @@ def maybe(*choices): return group(*choices) + '?'
127129
"RB'''": single3prog, 'RB"""': double3prog,
128130
'r': None, 'R': None,
129131
'u': None, 'U': None,
132+
'f': None, 'F': None,
130133
'b': None, 'B': None}
131134

132135
triple_quoted = {}
133136
for t in ("'''", '"""',
134137
"r'''", 'r"""', "R'''", 'R"""',
135138
"u'''", 'u"""', "U'''", 'U"""',
136139
"b'''", 'b"""', "B'''", 'B"""',
140+
"f'''", 'f"""', "F'''", 'F"""',
137141
"ur'''", 'ur"""', "Ur'''", 'Ur"""',
138142
"uR'''", 'uR"""', "UR'''", 'UR"""',
139143
"br'''", 'br"""', "Br'''", 'Br"""',
@@ -146,6 +150,7 @@ def maybe(*choices): return group(*choices) + '?'
146150
"r'", 'r"', "R'", 'R"',
147151
"u'", 'u"', "U'", 'U"',
148152
"b'", 'b"', "B'", 'B"',
153+
"f'", 'f"', "F'", 'F"',
149154
"ur'", 'ur"', "Ur'", 'Ur"',
150155
"uR'", 'uR"', "UR'", 'UR"',
151156
"br'", 'br"', "Br'", 'Br"',

Lib/lib2to3/tests/test_parser.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,7 @@ class TestStringLiterals(GrammarTest):
344344
"r'", 'r"', "R'", 'R"',
345345
"u'", 'u"', "U'", 'U"',
346346
"b'", 'b"', "B'", 'B"',
347+
"f'", 'f"', "F'", 'F"',
347348
"ur'", 'ur"', "Ur'", 'Ur"',
348349
"uR'", 'uR"', "UR'", 'UR"',
349350
"br'", 'br"', "Br'", 'Br"',

0 commit comments

Comments
 (0)