A decorator to make `parse_float` safe. `parse_float` must not return dicts or lists, because these types would be mixed with parsed TOML tables and arrays, thus confusing the parser. The returned decorated callable raises `ValueError` instead of returning illegal types.
(parse_float: ParseFloat)
| 738 | |
| 739 | |
| 740 | def make_safe_parse_float(parse_float: ParseFloat) -> ParseFloat: |
| 741 | """A decorator to make `parse_float` safe. |
| 742 | |
| 743 | `parse_float` must not return dicts or lists, because these types |
| 744 | would be mixed with parsed TOML tables and arrays, thus confusing |
| 745 | the parser. The returned decorated callable raises `ValueError` |
| 746 | instead of returning illegal types. |
| 747 | """ |
| 748 | # The default `float` callable never returns illegal types. Optimize it. |
| 749 | if parse_float is float: |
| 750 | return float |
| 751 | |
| 752 | def safe_parse_float(float_str: str) -> Any: |
| 753 | float_value = parse_float(float_str) |
| 754 | if isinstance(float_value, (dict, list)): |
| 755 | raise ValueError("parse_float must not return dicts or lists") |
| 756 | return float_value |
| 757 | |
| 758 | return safe_parse_float |
no outgoing calls
no test coverage detected
searching dependent graphs…