Cache type_object_type() by ilevkivskyi · Pull Request #19514 · python/mypy

This gives almost 4% performance boost (Python 3.12, compiled). Note there is an old bug in type_object_type(), we treat not ready types as Any without deferring, I disable caching in this case.

Unfortunately, using this in fine-grained mode is tricky, essentially I have three options:

  • Use some horrible hacks to invalidate cache when needed
  • Add (expensive) class target dependency from __init__/__new__
  • Only allow constructor caching during initial load, but disable it in fine-grained increments

I decided to choose the last option. I think it has the best balance complexity/benefits.