allow not-defined fields in format strings

... and replace them with "None", for now
pull/79/head
Mike Fährmann 7 years ago
parent 6a07e38366
commit aa38eab2be
No known key found for this signature in database
GPG Key ID: 5680CA389D365A88

@ -331,6 +331,9 @@ class Formatter():
"""Return value called 'field_name' from 'kwargs'"""
first, rest = _string.formatter_field_name_split(field_name)
if first not in kwargs:
return None
obj = kwargs[first]
for is_attr, i in rest:
if is_attr:

@ -175,6 +175,13 @@ class TestFormatter(unittest.TestCase):
self._run_test("{name}{title4:?//}", "Name")
self._run_test("{name}{title4:? **/''/}", "Name")
def test_missing(self):
replacement = "None"
self._run_test("{missing}", replacement)
self._run_test("{missing.attr}", replacement)
self._run_test("{missing[key]}", replacement)
self._run_test("{missing?a/b/}", replacement)
def _run_test(self, format_string, result):
formatter = util.Formatter()
output = formatter.vformat(format_string, self.kwdict)

Loading…
Cancel
Save