def _named_inline_multidimvars(
    constraint: _NamedConstraint,
    solutions: dict[str | tuple[str, str], _Binding],
) -> tuple[_NamedConstraint | _UnsatisfiedConstraint, str]:
  """Simplifies a named constraint by inlining multivars."""
  new_pattern = {}
  inlinable: list[MultiDimVar] = []
  inline_summary = []
  for key, val in constraint.pattern.items():
    if isinstance(key, MultiDimVar) and key.name in solutions:
      assert val == RemainingAxisPlaceholder()
      inlinable.append(key)
    else:
      new_pattern[key] = val

  for key in inlinable:
    binding = solutions[key.name]
    assert isinstance(binding.value, dict)
    for subkey, subval in binding.value.items():
      assert isinstance(subkey, str)
      if subkey in new_pattern:
        return (
            _UnsatisfiedConstraint(
                "Key conflict while substituting unpacked variable"
                f" {_show_dim(key)} for known named sub-shape"
                f" {binding.value} from"
                f" {binding.source}: name {subkey} is"
                " already present in the shape"
                f" {_summarize_pattern(constraint.pattern)}."
            ),
            "".join(inline_summary),
        )
      new_pattern[subkey] = KnownDim(
          (key.name, subkey), subval, from_keypath=binding.source
      )
    inline_summary.append(
        f"\n  After inlining var({repr(key.name)}) = {binding.value} from"
        f" {binding.source}"
    )
  return (
      _NamedConstraint(value=constraint.value, pattern=new_pattern),
      "".join(inline_summary),
  )
Simplifies a named constraint by inlining multivars.
penzai/core/shapecheck.py