🔒 🐛 Add initial value to vformat results list, fix #781

This commit is contained in:
Mix 2022-02-10 13:17:11 +08:00
parent 455c599b06
commit b7762b9176

View File

@ -104,7 +104,7 @@ class MessageTemplate(Formatter, Generic[TF]):
if recursion_depth < 0: if recursion_depth < 0:
raise ValueError("Max string recursion exceeded") raise ValueError("Max string recursion exceeded")
results: List[Any] = [] results: List[Any] = [self.factory()]
for (literal_text, field_name, format_spec, conversion) in self.parse( for (literal_text, field_name, format_spec, conversion) in self.parse(
format_string format_string
@ -162,10 +162,7 @@ class MessageTemplate(Formatter, Generic[TF]):
formatted_text = self.format_field(obj, str(format_control)) formatted_text = self.format_field(obj, str(format_control))
results.append(formatted_text) results.append(formatted_text)
return ( return functools.reduce(self._add, results), auto_arg_index
self.factory(functools.reduce(self._add, results or [""])),
auto_arg_index,
)
def format_field(self, value: Any, format_spec: str) -> Any: def format_field(self, value: Any, format_spec: str) -> Any:
formatter: Optional[FormatSpecFunc] = self.format_specs.get(format_spec) formatter: Optional[FormatSpecFunc] = self.format_specs.get(format_spec)