Process superclass methods before subclass methods in semanal by ilevkivskyi · Pull Request #18723 · python/mypy (original) (raw)
This seems to change the order of processing targets, even if derived classes are always after base classes (i.e. current ordering is already fine). I suspect that this will break the current SCC ordering algorithm, which we probably rely on in a bunch of places, and it could explain why things are failing. I think we must mostly follow the SCC ordering or we will have a bunch of weird regressions and generally a bad time.
Here's one potential way to fix this so this only changes the order when necessary:
- Create a linear list of targets, similar to what you currently have.
- Collect a set of all TypeInfos in the targets (e.g. all
active_typevalues). - Iterate over the targets, and keep track of which TypeInfo's we've processed by removing the TypeInfo set created in the previous step. If we encounter a TypeInfo which has some MRO item that is in the set of TypeInfos, move that to a separately list (deferreds) instead of processing now.
- After having iterated all targets, iterate over the deferred items.
The above approach could possibly be made even better by processing deferred nodes immediately after all the MRO entries have been processed, instead of waiting for all targets to be processed.
This has the benefit of not changing the processing order if it's already correct, and if it's incorrect, only the impacted targets will get rescheduled. This also could be a bit faster, since we perform a linear scan instead of a sort.