Skip to content

Commit 0e14b4e

Browse files
committed
refactor: fix tests with t-strings for py3.14
1 parent 41bfc1c commit 0e14b4e

File tree

1 file changed

+45
-12
lines changed

1 file changed

+45
-12
lines changed

tests/test_safe_eval.py

Lines changed: 45 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,14 @@
66
import pytest
77
from djc_core import SecurityError, safe_eval, unsafe
88

9+
# Check if t-strings are supported (Python 3.14+)
10+
try:
11+
from string.templatelib import Template # type: ignore[import-untyped]
12+
13+
TSTRINGS_SUPPORTED = True
14+
except ImportError:
15+
TSTRINGS_SUPPORTED = False
16+
917

1018
Value = NamedTuple("Value", [("value", int)])
1119

@@ -1252,38 +1260,63 @@ def __eq__(self, other):
12521260
def test_transform_tstring_simple(self):
12531261
compiled = safe_eval("t'Hello {name}'")
12541262
context = {"name": "test"}
1255-
with pytest.raises(NotImplementedError):
1256-
compiled(context)
1263+
if TSTRINGS_SUPPORTED:
1264+
# On Python 3.14+, t-strings are supported and return Template objects
1265+
result = compiled(context)
1266+
assert isinstance(result, Template)
1267+
else:
1268+
with pytest.raises(NotImplementedError):
1269+
compiled(context)
12571270

12581271
def test_transform_tstring_with_expression(self):
12591272
compiled = safe_eval("t'Result: {x + 1}'")
12601273
context = {"x": 10}
1261-
with pytest.raises(NotImplementedError):
1262-
compiled(context)
1274+
if TSTRINGS_SUPPORTED:
1275+
result = compiled(context)
1276+
assert isinstance(result, Template)
1277+
else:
1278+
with pytest.raises(NotImplementedError):
1279+
compiled(context)
12631280

12641281
def test_transform_tstring_multiple_interpolations(self):
12651282
compiled = safe_eval("t'{x} and {y}'")
12661283
context = {"x": 10, "y": 10}
1267-
with pytest.raises(NotImplementedError):
1268-
compiled(context)
1284+
if TSTRINGS_SUPPORTED:
1285+
result = compiled(context)
1286+
assert isinstance(result, Template)
1287+
else:
1288+
with pytest.raises(NotImplementedError):
1289+
compiled(context)
12691290

12701291
def test_transform_tstring_with_format_spec(self):
12711292
compiled = safe_eval("t'start {value:.2f} end'")
12721293
context = {"value": 100}
1273-
with pytest.raises(NotImplementedError):
1274-
compiled(context)
1294+
if TSTRINGS_SUPPORTED:
1295+
result = compiled(context)
1296+
assert isinstance(result, Template)
1297+
else:
1298+
with pytest.raises(NotImplementedError):
1299+
compiled(context)
12751300

12761301
def test_transform_tstring_with_conversion(self):
12771302
compiled = safe_eval("t'start {value!r} end'")
12781303
context = {"value": 100}
1279-
with pytest.raises(NotImplementedError):
1280-
compiled(context)
1304+
if TSTRINGS_SUPPORTED:
1305+
result = compiled(context)
1306+
assert isinstance(result, Template)
1307+
else:
1308+
with pytest.raises(NotImplementedError):
1309+
compiled(context)
12811310

12821311
def test_transform_tstring_with_conversion_and_format(self):
12831312
compiled = safe_eval("t'start {value!r:>20} end'")
12841313
context = {"value": 100}
1285-
with pytest.raises(NotImplementedError):
1286-
compiled(context)
1314+
if TSTRINGS_SUPPORTED:
1315+
result = compiled(context)
1316+
assert isinstance(result, Template)
1317+
else:
1318+
with pytest.raises(NotImplementedError):
1319+
compiled(context)
12871320

12881321
# === VARIABLES ===
12891322

0 commit comments

Comments
 (0)