Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit df11158

Browse files
committed
Document the new formatter behaviour, skip relevant test on 3.14+
1 parent 0b05010 commit df11158

2 files changed

Lines changed: 15 additions & 1 deletion

File tree

IPython/utils/text.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -439,6 +439,10 @@ class EvalFormatter(Formatter):
439439
standard string formatting), so if slicing is required, you must explicitly
440440
create a slice.
441441
442+
Note that on Python 3.14+ this version interprets `[]` as indexing operator
443+
so you need to use generators instead of list comprehensions, for example:
444+
`list(i for i in range(10))`.
445+
442446
This is to be used in templating cases, such as the parallel batch
443447
script templates, where simple arithmetic on arguments is useful.
444448

tests/test_text.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
# -----------------------------------------------------------------------------
1414

1515
import os
16+
import sys
1617
import math
1718
import random
1819

@@ -27,13 +28,19 @@
2728
# -----------------------------------------------------------------------------
2829

2930

31+
def eval_formatter_list_comprehension_check(f):
32+
ns = dict(n=12)
33+
s = f.format("{[n//i for i in range(1,8)]}", **ns)
34+
assert s == "[12, 6, 4, 3, 2, 2, 1]"
35+
36+
3037
def eval_formatter_check(f):
3138
ns = dict(n=12, pi=math.pi, stuff="hello there", os=os, u="café", b="café")
3239
s = f.format("{n} {n//4} {stuff.split()[0]}", **ns)
3340
assert s == "12 3 hello"
3441
s = f.format(" ".join(["{n//%i}" % i for i in range(1, 8)]), **ns)
3542
assert s == "12 6 4 3 2 2 1"
36-
s = f.format("{[n//i for i in range(1,8)]}", **ns)
43+
s = f.format("{list(n//i for i in range(1,8))}", **ns)
3744
assert s == "[12, 6, 4, 3, 2, 2, 1]"
3845
s = f.format("{stuff!s}", **ns)
3946
assert s == ns["stuff"]
@@ -78,12 +85,15 @@ def test_eval_formatter():
7885
f = text.EvalFormatter()
7986
eval_formatter_check(f)
8087
eval_formatter_no_slicing_check(f)
88+
if sys.version_info < (3, 14):
89+
eval_formatter_list_comprehension_check(f)
8190

8291

8392
def test_full_eval_formatter():
8493
f = text.FullEvalFormatter()
8594
eval_formatter_check(f)
8695
eval_formatter_slicing_check(f)
96+
eval_formatter_list_comprehension_check(f)
8797

8898

8999
def test_dollar_formatter():

0 commit comments

Comments
 (0)