-
-
Notifications
You must be signed in to change notification settings - Fork 515
Open
Labels
bugSomething isn't workingSomething isn't working
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
| def refresh_from_db( | |
| self, | |
| using: str | None = None, | |
| fields: Iterable[str] | None = None, | |
| from_queryset: QuerySet[Self] | None = None, | |
| ) -> None: ... | |
| async def arefresh_from_db( | |
| self, | |
| using: str | None = None, | |
| fields: Iterable[str] | None = None, | |
| from_queryset: QuerySet[Self] | None = None, | |
| ) -> None: ... |
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
pythonversion: 3.13djangoversion: 5.2mypyversion:pyright==1.1.399orbasedpyright==1.28.5django-stubsversion: 5.1.3django-stubs-extversion: 5.1.3
Metadata
Metadata
Assignees
Labels
bugSomething isn't workingSomething isn't working