Fix cubic-time from_type() on abstract class hierarchies#4749
Merged
Conversation
ed312aa to
1a65386
Compare
Liam-DeVoe
approved these changes
May 27, 2026
Resolving an abstract type whose subclasses reference the base class in their own annotations re-resolved the entire hierarchy once per reference, taking time cubic in the number of subclasses. Track the abstract types currently being resolved in a guard, and defer any recursive reference back to them by returning the cached from_type() strategy (sharing one object lets the recursion in e.g. is_empty checks terminate), so each type is resolved only once. This guard is kept separate from _recurse_guard so that it catches references arriving by any route (e.g. as a union arg) and doesn't make from_type_guarded treat a subclass's required field of the abstract type as unresolvable. Also filter the registered-subtype lookup before sorting it, so the expensive repr-based sort only runs on the (usually empty) matching set rather than the whole global type lookup on every resolution. https://claude.ai/code/session_01MdLX8p4tdAUaDjSHBnveVy
1a65386 to
34ef684
Compare
auto-merge was automatically disabled
May 27, 2026 16:11
Pull request was closed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Resolving an abstract type whose subclasses reference the base class in their own annotations re-resolved the entire hierarchy once per reference, taking time cubic in the number of subclasses. Guard against re-entry so each type is resolved only once: if we're already resolving an abstract type higher up the stack, return the cached from_type() strategy (sharing one object lets the recursion in e.g. is_empty checks terminate) instead of re-resolving it. We reuse the existing _recurse_guard, which also catches references that reach us as a union arg rather than a field.
Also filter the registered-subtype lookup before sorting it, so the expensive repr-based sort only runs on the (usually empty) matching set rather than the whole global type lookup on every resolution.
Fixes #4729.