Skip to content
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

Fix return type for django.shortcuts.render #1140

Conversation

SebastiaanZ
Copy link
Contributor

The return type for calling shorcuts.render without providing a value for the permanent kwarg was HttpResponsePermanentRedirect, while it should be HttpResponseRedirect.

The reason is that the first two overloads of the type stub overlap for the case of using the default argument. While mypy does issue an error for this, it was previously ignored with the # type: ignore comment.

As the first overload annotates the function as having the return type HttpResponsePermanentRedirect, this overlap would cause mypy to assume that the return type is HttpResponsePermanentRedirect instead of the actual return type HttpResponseRedirect.

Since calling django.shortcuts.redirect without providing an argument for permanent is the same as calling it with a Literal[False] (as the default value is a False), we can improve the stub by only specifying the option to use the default argument (= ...) in the second overload.

This also removes the overlap in stub definitions, meaning that the # type: ignore can also be removed.

Related issues

The return type for calling `shorcuts.render` without providing a value
for the `permanent` kwarg was `HttpResponsePermanentRedirect`, while it
should be `HttpResponseRedirect`.

The reason is that the first two overloads of the type stub overlap for
the case of using the default argument. While `mypy` does issue an error
for this, it was previously ignored with the `# type: ignore` comment.

As the first overload annotates the function as having the return type
`HttpResponsePermanentRedirect`, this would make mypy assume that the
return type is that instead of `HttpResponseRedirect`.

Since calling `django.shortcuts.redirect` without providing an argument
for `permanent` is the same as calling it with a `Literal[False]`, as
the default value is a `False`, we can improve the stub by only
specifying the option to use the default argument (`= ...`) in the
second overload. This also removes the overlap in stub definitions,
meaning that the `# type: ignore` can now be removed.

This commit fixes typeddjango#1138.
Copy link
Member

@sobolevn sobolevn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!

@sobolevn sobolevn merged commit a1d3aec into typeddjango:master Aug 30, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
2 participants