Incorrect return type annotation for shortcuts.redirect
if using the default argument for permanent
in the call
#1138
Labels
bug
Something isn't working
Bug report
What's wrong
The first two overload definitions for
shortcuts.redirect
overlap, leading to an incorrect return type annotation if the default value for the kwargpermanent
is used (i.e., calling the function without providing a value for the kwargpermanent
yourself).Normally,
mypy
produces an error for such an overlap, but it's explicitly ignored on L24:django-stubs/django-stubs/shortcuts.pyi
Lines 23 to 34 in 74e31c7
Here's a related mypy issue with an explanation for why this behaviour occurs.
How to reproduce
Adding this line to the
check_redirect_return_annotation
test case intests/typecheck/test_shortcuts.yml
produces a failing test case:The full test case could now be:
This will now produce a failing test:
In addition, removing the
# type: ignore
from L24 also reveals the problem with the overlap ifmypy
is now run:How is that should be
As the default value for
permanent
in theredirect
function signature isFalse
, the return type should bedjango.http.response.HttpResponseRedirect
.To fix this,
permanent: Literal[True] = ...
can be changed topermanent: Literal[True]
in the first overload. This also removes the overlap, which means the# type: ignore
can now also be removed.This results in:
The logic here is that the first overload, with a
Literal[True]
can never be trigger without providing an argument (i.e., when falling back to the default argument). This means that this overload should not specify the= ...
to say that it should match situations in which the default argument is used.The second overload now matched both an explicit
permanent=False
, as well as the default argument (which is alsoFalse
). The third overload still matched theUnion
case defined in the test.System information
python
version: 3.9 & 3.10django
version: 3.2, 4.0, 4.1mypy
version: 0.971django-stubs
version: 1.12.0 (master
)django-stubs-ext
version: 0.6.0 (master
)The text was updated successfully, but these errors were encountered: