61bc818b4d
## Summary I needed this for https://github.com/astral-sh/uv/pull/10794, but it makes sense as a standalone change, since it's much more testable. We can also reuse this in at least one more place.