Files
nuitka-mirror/tests/basics/TemplateStringTest314.py
Kay Hayen 3a0c2bd9ae Python3.14+: Fix, template string result handling
* Use "withObjectCodeTemporaryAssignment" for template strings and
  interpolations, so that they use well in boolean contexts.
2026-10-29 20:13:08 +01:00

53 lines
1.6 KiB
Python

# Copyright 2026, Kay Hayen, mailto:kay.hayen@gmail.com find license text at end of file
"""Test Python 3.14 template strings with conversions."""
width = 10
def dump(template):
print("strings:", template.strings)
for interpolation in template.interpolations:
print(
"interpolation:",
repr(interpolation.value),
repr(interpolation.expression),
repr(interpolation.conversion),
repr(interpolation.format_spec),
)
dump(t"hi {'world'!r}")
dump(t"{'world'!s}")
dump(t"{'world'!a}")
dump(t"{1 + 2!r}")
dump(t"{'x'!s:>10}")
dump(t"{'x'!a:{width}}")
dump(t"before {'value'!r} after")
# Unused t-strings must still be built and discarded safely.
t"unused {'world'}"
t"unused {'world'!r:>10}"
# T-strings in boolean context require conversion to a boolean.
if t"{'world'!r}":
print("t-string is truthy")
assert t"", "Template objects are truthy"
# Python tests originally created or extracted from other peoples work. The
# parts were too small to be protected.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.