[formatter] add 'I' format specifier - identity

This commit is contained in:
Mike Fährmann
2025-12-17 19:09:30 +01:00
parent 717d38f38c
commit ff94002b44
3 changed files with 20 additions and 0 deletions

View File

@@ -375,6 +375,12 @@ Format specifiers can be used for advanced formatting by using the options provi
<td><code>{date:Olocal/}</code></td>
<td><code>2010-01-01 01:00:00</code></td>
</tr>
<tr>
<td><code>I</code></td>
<td>Return the current value as is.<br>Do not convert it to <code>str</code></td>
<td><code>{num:I}</code></td>
<td><code>1</code></td>
</tr>
</tbody>
</table>

View File

@@ -430,6 +430,10 @@ def _parse_maxlen(format_spec, default):
return mlen
def _parse_identity(format_spec, default):
return util.identity
def _parse_join(format_spec, default):
separator, _, format_spec = format_spec.partition(_SEPARATOR)
join = separator[1:].join
@@ -609,6 +613,7 @@ _FORMAT_SPECIFIERS = {
"A": _parse_arithmetic,
"C": _parse_conversion,
"D": _parse_datetime,
"I": _parse_identity,
"J": _parse_join,
"L": _parse_maxlen,
"M": _parse_map,

View File

@@ -369,6 +369,15 @@ class TestFormatter(unittest.TestCase):
with self.assertRaises(ValueError):
self._run_test("{t:Mname", "")
def test_specifier_identity(self):
self._run_test("{a:I}", self.kwdict["a"])
self._run_test("{i:I}", self.kwdict["i"])
self._run_test("{dt:I}", self.kwdict["dt"])
self._run_test("{t!D:I}", self.kwdict["dt"])
self._run_test("{t!D:I/O+01:30}", self.kwdict["dt"])
self._run_test("{i:A+1/I}", self.kwdict["i"]+1)
def test_chain_special(self):
# multiple replacements
self._run_test("{a:Rh/C/RE/e/RL/l/}", "Cello wOrld")