To avoid needing to worry about lifetimes at all, it is also possible to use the new PyBackedStr type, which stores a reference to the Python str without a lifetime attachment. In particular, PyBackedStr helps for abi3 builds for Python older than 3.10. Due to limitations in the abi3 CPython API for those older versions, PyO3 cannot offer a FromPyObjectBound implementation for &str on those versions.
So some code processing bytes and strings may potentially be faster or simpler once Python 3.10 is the minimum supported version, by removing the need for PyBackedStr/PyBackedBytes.
Description
Per PyO3 migration guide:
So some code processing bytes and strings may potentially be faster or simpler once Python 3.10 is the minimum supported version, by removing the need for
PyBackedStr/PyBackedBytes
.