Replace sys.last_type / sys.last_value / sys.last_traceback with sys.last_exc

Proprietary

sys.last_type, sys.last_value, and sys.last_traceback were deprecated in Python 3.12. Replace reads of them with their sys.last_exc-based equivalents: type(sys.last_exc), sys.last_exc, and sys.last_exc.__traceback__ respectively, and a hasattr(sys, "last_type") guard with hasattr(sys, "last_exc"). Assignment, del, for and with targets are left alone, and so is every read in a file that assigns or deletes one of the attributes, since sys.last_exc would not hold what it set. sys.last_type is left alone where type is shadowed.

org.openrewrite.python.migrate.ReplaceSysLastExcInfo
mod run . --recipe=org.openrewrite.python.migrate.ReplaceSysLastExcInfo

Install it once, then run it on every repository under the current directory. New to the Moderne CLI? Set it up.