Open
Description
In e.g. django.db.models.Model.[a]refresh_from_db
, Self
is used in a contravariant position:
django-stubs/django-stubs/db/models/base.pyi
Lines 96 to 107 in 0be73eb
When I override this in my own model as e.g.
@override
def refresh_from_db(
self,
using: str | None = None,
fields: Iterable[str] | None = None,
from_queryset: QuerySet[Self] | None = None,
) -> None:
it violates the LSP, as explained in https://discuss.python.org/t/unsoundness-of-contravariant-self-type/86338. On pyright>=1.1.399
, it will reportIncompatibleMethodOverride
.
The workaround is to instead use QuerySet[Any]
in the code above.
Perhaps a self: T
with QuerySet[T]
could work here? I'm not sure whether that'd make it theoretically sound, but it might be worth a shot 🤷🏻.
System information
- OS: irrelevant
python
version: 3.13django
version: 5.2mypy
version:pyright==1.1.399
orbasedpyright==1.28.5
django-stubs
version: 5.1.3django-stubs-ext
version: 5.1.3