Add str.format_map(mapping, /)

Merged
GA
2d ago
+169-34

Implements str.format_map(mapping, /) on str.

  • Extends FormatArguments in crates/monty/src/str_format.rs with a Mapping(Value) variant so str.format_map() reuses the existing PEP 3101 format string parser, conversions, nested format specs, attribute/item accesses, and resource-limit checks while delegating named root field lookups directly to mapping.py_getitem(key, vm).
  • Supports dict, collections.defaultdict, collections.Counter, and user-defined mappings implementing __getitem__.
  • Rejects positional fields ({}, {0}, etc.) with ValueError: Format string contains positional fields and validates the single positional-only mapping argument.
  • Updates docs/limitations/format.md.

Part of #1275 and #1319.


Summary by cubic

Implements str.format_map(mapping, /), which previously raised AttributeError. It reuses the existing PEP 3101 format string parser from str.format(), so conversions, nested format specs, attribute/item access, and resource-limit checks work identically, but named root field lookups delegate to mapping.__getitem__.

  • Supports dict, defaultdict, Counter, and any user-defined mapping with __getitem__.
  • Positional fields ({}, {0}) raise ValueError: Format string contains positional fields, matching CPython.
  • The mapping argument is positional-only and validated to accept exactly one argument with no kwargs.
  • Removes the str.format_map() limitation from the docs and adds a test case file.