You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I've tried the Gitter help link, but it appears to not be an active place (I left a question there a couple months ago and it never got an answer - and there's only about 5 more messages since then), so I'm trying my luck here.
For boilerplate purposes, I'm trying to provide multiple model Mixins, as well as a base abstract user model to be used in projects. This abstract user model should implement one of these Mixins (SoftDeleteMixin); this mixin implements it's own custom manager. That appears to be the start of the mess, as I can't make the types coherent.
Here are some snippets of the models and what I'm trying to achieve:
# The mixin managerSoftDeleteModel=TypeVar("SoftDeleteModel", bound=SoftDeleteMixin)
classSoftDeleteManager(models.Manager[SoftDeleteModel]):
defget_queryset(self) ->models.QuerySet[SoftDeleteModel]: ...
definclude_deleted(self) ->models.QuerySet[SoftDeleteModel]: ...
# The mixinclassSoftDeleteMixin(models.Model):
objects=SoftDeleteManager[Self]()
# The manager for the users - inherits from the other manager because the GenericUser type will subclass SoftDeleteMixinGenericUser=TypeVar("GenericUser", bound=BaseAbstractUser)
classBaseUserManager(SoftDeleteManager[GenericUser], DjangoBaseUserManager[GenericUser]):
defcreate_user(self, password: str, **fields: Any) ->GenericUser: ...
defcreate_superuser(self, password: str, **fields: Any) ->GenericUser: ...
# The abstract user modelclassBaseAbstractUser(PermissionsMixin, SoftDeleteMixin, DjangoAbstractBaseUser):
objects=BaseUserManager[Self]() # By default uses the base manager
Now this is generating loads of errors with mypy:
Incompatible types in assignment (expression has type "BaseUserManager[BaseAbstractUser]", base class "SoftDeleteMixin" defined the type as "SoftDeleteManager[SoftDeleteMixin]"
Subclasses have incompatible return type in the objects property; for example:
classUserModel(BaseAbstractUser): ...
instance=UserModel.objects.create(...) # instance will evaluate to BaseAbstractUser instead of UserModel# Notably, Pylance on VSCode appears to correctly identify that `create` returns a UserModel, which seems weird to me.
Is this some sort of bug, or am I doing some kind of "anti-pattern" here, typing things incorrectly, etc? I've tried multiple changes with Generics and all that and always end up with some kind of issue in the end. Help would be greatly appreciated.
The text was updated successfully, but these errors were encountered:
I've tried the Gitter help link, but it appears to not be an active place (I left a question there a couple months ago and it never got an answer - and there's only about 5 more messages since then), so I'm trying my luck here.
For boilerplate purposes, I'm trying to provide multiple model Mixins, as well as a base abstract user model to be used in projects. This abstract user model should implement one of these Mixins (SoftDeleteMixin); this mixin implements it's own custom manager. That appears to be the start of the mess, as I can't make the types coherent.
Here are some snippets of the models and what I'm trying to achieve:
Now this is generating loads of errors with mypy:
Incompatible types in assignment (expression has type "BaseUserManager[BaseAbstractUser]", base class "SoftDeleteMixin" defined the type as "SoftDeleteManager[SoftDeleteMixin]"
objects
property; for example:Is this some sort of bug, or am I doing some kind of "anti-pattern" here, typing things incorrectly, etc? I've tried multiple changes with Generics and all that and always end up with some kind of issue in the end. Help would be greatly appreciated.
The text was updated successfully, but these errors were encountered: