Bump pytest to >=7.4.0
The 7.4.0 release of pytest broke mypy because we were using some undocumented, private API that was removed. Ideally we'd stop using the private API, but nobody seems to remember why we started using the private API in the first place (see https://github.com/python/mypy/pull/15501#issuecomment-1607083799, and following comments). For now it (unfortunately) seems safer to just migrate to the new private API rather than try to figure out an alternative using public API.
I also took @bluetech's advice in https://github.com/python/mypy/pull/15501#issuecomment-1606259499 to improve the type annotations in the method in question.
Diff from mypy_primer, showing the effect of this PR on open source code:
vision (https://github.com/pytorch/vision): typechecking got 1.32x faster (42.3s -> 32.0s)
(Performance measurements are based on a single noisy sample)