-
-
Notifications
You must be signed in to change notification settings - Fork 485
LoginView has a typing issue #514
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
additional info: a work-around is to override form_valid as follows: def form_valid(self, form: BaseForm) -> HttpResponseRedirect:
# insert any additional logic here
return super().form_valid(form) which is fine for my current use-case, but doesn't work if someone needs to completely override the method i think that if this isn't possible to fix in django-stubs, there should be something in the documentation mentioning that workaround |
Yes, I have seen this myself 😞 PRs are welcome! |
i honestly have no idea how to even fix this, i don't think it's possible to do it on django-stubs' end. only thing i can think of would be to make an maybe make |
As a wild idea, we can try to make |
since from typing import Generic, TypeVar
T = TypeVar('T', bound=BaseForm)
class ContextMixin: ...
class BaseForm: ...
class AuthenticationForm(BaseForm): ...
class FormMixin(Generic[T], ContextMixin):
def form_valid(self, form: T): ...
class LoginView(FormView[T]): ...
class FormView(BaseFormView[T]): ...
class BaseFormView(FormMixin[T]): ...
class MyLoginView(LoginView[AuthenticationForm]):
def form_valid(self, form: AuthenticationForm): ... |
whoops i got my class hierarchy wrong. i think it should still work though |
looks good, running tests one last time but i don't think there'll be any issues. i'll send the pr as soon as it finishes! |
bug report
what's wrong
django's LoginView (django.contrib.auth.views.LoginView) violates the Liskov substitution principle. we see here that the view's
form_valid
method callsform.get_user()
, meaning thatform
must be of typeAuthenticationForm
. however,FormMixin
definesform
asBaseForm
, and attempting to typeform
properly results in a violation of the liskov substitution principlethe following minimal failing example overrides LoginView and copies the
form_valid
method exactly (with type hints added):it fails with the following error:
how is that should be
this shouldn't fail. i'm not exactly sure if this can be fixed in django-stubs or if this is something that needs to be fixed in django
clearly django's LoginView class expects form to be an
AuthenticationForm
due to theget_user
method call, but typing it as such would violate the liskov principlesystem information
python
version: 3.8.6django
version: 3.1.2mypy
version: 0.790django-stubs
version: 1.7.0The text was updated successfully, but these errors were encountered: