|
6 | 6 | import pytest |
7 | 7 | from djc_core import SecurityError, safe_eval, unsafe |
8 | 8 |
|
| 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 | + |
9 | 17 |
|
10 | 18 | Value = NamedTuple("Value", [("value", int)]) |
11 | 19 |
|
@@ -1252,38 +1260,63 @@ def __eq__(self, other): |
1252 | 1260 | def test_transform_tstring_simple(self): |
1253 | 1261 | compiled = safe_eval("t'Hello {name}'") |
1254 | 1262 | 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) |
1257 | 1270 |
|
1258 | 1271 | def test_transform_tstring_with_expression(self): |
1259 | 1272 | compiled = safe_eval("t'Result: {x + 1}'") |
1260 | 1273 | 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) |
1263 | 1280 |
|
1264 | 1281 | def test_transform_tstring_multiple_interpolations(self): |
1265 | 1282 | compiled = safe_eval("t'{x} and {y}'") |
1266 | 1283 | 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) |
1269 | 1290 |
|
1270 | 1291 | def test_transform_tstring_with_format_spec(self): |
1271 | 1292 | compiled = safe_eval("t'start {value:.2f} end'") |
1272 | 1293 | 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) |
1275 | 1300 |
|
1276 | 1301 | def test_transform_tstring_with_conversion(self): |
1277 | 1302 | compiled = safe_eval("t'start {value!r} end'") |
1278 | 1303 | 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) |
1281 | 1310 |
|
1282 | 1311 | def test_transform_tstring_with_conversion_and_format(self): |
1283 | 1312 | compiled = safe_eval("t'start {value!r:>20} end'") |
1284 | 1313 | 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) |
1287 | 1320 |
|
1288 | 1321 | # === VARIABLES === |
1289 | 1322 |
|
|
0 commit comments