Use Model instead of Self in parms and add AltersData inheritance#3228
Use Model instead of Self in parms and add AltersData inheritance#3228emmanuel-ferdman wants to merge 3 commits intotypeddjango:masterfrom
Model instead of Self in parms and add AltersData inheritance#3228Conversation
…ritance Signed-off-by: Emmanuel Ferdman <emmanuelferdman@gmail.com>
sobolevn
left a comment
There was a problem hiding this comment.
Let's please add a assert_type test for this, it should be possible to override Model.refresh_from_db method with the correct annotation.
…ritance Signed-off-by: Emmanuel Ferdman <emmanuelferdman@gmail.com>
|
@sobolevn had it ready in my local tests :) |
| self, | ||
| using: str | None = None, | ||
| fields: Iterable[str] | None = None, | ||
| from_queryset: QuerySet[models.Model] | None = None, |
There was a problem hiding this comment.
MyModel won't work here, right?
There was a problem hiding this comment.
Right, QuerySet[MyModel] errors on both mypy and pyright since it narrows the parameter type. Should we add another test case for it?
There was a problem hiding this comment.
Yes, please. But, it seems kinda right to have it typed as QuerySet[MyModel]?
Because passing from_queryset=QuerySet[SomeOther] with no error does seem like a false negative to me.
Do you have any ideas on how we can make this right?
There was a problem hiding this comment.
We could try self: _Self with QuerySet[_Self] - this ties the queryset to the actual model type. So user.refresh_from_db(from_queryset=article_qs) would correctly error. It's stricter than runtime since QuerySet[Model] gets rejected for typed subclass instances, but I think passing a wrong model's queryset is a bug waiting to happen anyway :)
…ritance Signed-off-by: Emmanuel Ferdman <emmanuelferdman@gmail.com>
70a4b70 to
173f747
Compare
PR Summary
refresh_from_db,arefresh_from_db,_do_update,_get_pk_val, andunique_error_messageusedSelfin their parameter types, which is unsound in contravariant positions - overriding these methods in a subclass with the sameSelfpattern triggersreportIncompatibleMethodOverridein pyright/ty/pyrefly becauseQuerySet[Model]is not assignable toQuerySet[MyModel](LSP violation). This PR changes these to useModelinstead, which is the widest valid type and what the runtime actually accepts. Also this PR adds the missingAltersDatatoModelandQuerySetbase classes to match the runtime MRO.Fixes #2605.