In preparation for #2177, this reduces the number of errors reported by mypy 1.9.0 from 1171 to 602 while also making some drive-by code style fixes.