Fixes #20587: Handle stale ContentTypes in has_feature() #20592
Merged
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.
Fixes: #20587
Fixes #20587 (and its duplicate #20588) by adding a null check when handling stale
ContentTypes inhas_feature().When running
remove_stale_contenttypes(executed byupgrade.sh), the management command fails withTypeError: issubclass() arg 1 must be a class. This occurs when deleting ContentTypes that no longer have corresponding model classes—referred to as "stale" ContentTypes. These arise when models are renamed/removed between versions or when plugins are uninstalled.The error path:
pre_deletesignal fires when deleting the stale ContentTypenotify_object_changed()signal handler callshas_feature(instance, 'notifications')has_feature()callsmodel_class()on the ContentType, which returnsNonefor stale typesNonegets passed directly to the feature test lambda:issubclass(None, NotificationsMixin)→ TypeErrorRoot Cause:
Commit 5ceb6a6 (which fixed #20290) changed the ContentType code path in
has_feature()to use direct feature registry lookups in addition to ObjectType lookups. However, this refactoring inadvertently removed the null safety check that existed in the previous implementation.https://gist.github.com/jnovinger/35948fc653c0425a5f9b207f9b77883f is available as a reproduction script to verify this fix.