feat: Add Self return type and QuerySetT type parameter for inheritance classes#652
Draft
lee3jjang wants to merge 1 commit intojazzband:masterfrom
Draft
Conversation
…ce classes This PR improves type safety for InheritanceQuerySetMixin and InheritanceManagerMixin by: 1. InheritanceQuerySetMixin methods now return Self instead of InheritanceQuerySet[ModelT], enabling proper type inference for custom QuerySet subclasses. 2. InheritanceManagerMixin now accepts an optional QuerySetT type parameter (defaults to InheritanceQuerySet[ModelT]) for specifying custom QuerySet types. Changes: - Add Self import (typing for 3.11+, typing_extensions otherwise) - Add QuerySetT TypeVar with default=InheritanceQuerySet[ModelT] - Update InheritanceQuerySetMixin methods to return Self: - select_subclasses() - _chain() - _clone() - annotate() - Update InheritanceQuerySet.instance_of() to return Self - Update InheritanceManagerMixin to be Generic[ModelT, QuerySetT] - Add typing_extensions dependency for Python < 3.13 Backwards compatibility: - Existing InheritanceManager[MyModel] code works unchanged - QuerySetT defaults to InheritanceQuerySet[ModelT] when not specified Closes jazzband#651
e4287d5 to
8020a61
Compare
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.
Summary
This PR improves type safety for
InheritanceQuerySetMixinandInheritanceManagerMixinby:InheritanceQuerySetMixinmethods now returnSelfinstead ofInheritanceQuerySet[ModelT], enabling proper type inference for custom QuerySet subclasses.InheritanceManagerMixinnow accepts an optionalQuerySetTtype parameter (defaults toInheritanceQuerySet[ModelT]) for specifying custom QuerySet types.Problem
Currently, when subclassing
InheritanceQuerySetMixinorInheritanceManagerMixin, methods return fixed types that don't match the actual subclass:Solution
1. InheritanceQuerySetMixin returns Self
2. InheritanceManagerMixin accepts QuerySetT
Usage Examples
Changes
Selfimport (fromtypingfor Python 3.11+,typing_extensionsotherwise)QuerySetTTypeVar withdefault=InheritanceQuerySet[ModelT]InheritanceQuerySetMixinmethods to returnSelf:select_subclasses()_chain()_clone()annotate()InheritanceQuerySet.instance_of()to returnSelfInheritanceManagerMixinto beGeneric[ModelT, QuerySetT]typing_extensions>=4.0.0dependency for Python < 3.13Backwards Compatibility
✅ Fully backwards compatible:
InheritanceManager[MyModel]code works unchangedQuerySetTdefaults toInheritanceQuerySet[ModelT]when not specifiedTest Plan
Closes #651