I started doing this in #1324 but put too much in a single PR. I'll instead submit a series of simpler PRs. The return types of os.stat and similar will require especial care (see Guido's comments in #1324).