Skip to content

Commit

Permalink
Export HTTPMove (#6594) (#7288)
Browse files Browse the repository at this point in the history
Nitpick: If you catch `HTTPRedirection` then mypy complains that it
doesn't have a `location` attribute (this is used in
aiohttp-debugtoolbar, for example).

Proposal to export `HTTPMove`, in order to catch any exceptions with
location.

(cherry picked from commit a5d6418)

<!-- Thank you for your contribution! -->

## What do these changes do?

<!-- Please give a short brief about these changes. -->

## Are there changes in behavior for the user?

<!-- Outline any notable behaviour for the end users. -->

## Related issue number

<!-- Are there any issues opened that will be resolved by merging this
change? -->

## Checklist

- [ ] I think the code is well written
- [ ] Unit tests for the changes exist
- [ ] Documentation reflects the changes
- [ ] If you provide code modification, please add yourself to
`CONTRIBUTORS.txt`
  * The format is &lt;Name&gt; &lt;Surname&gt;.
  * Please keep alphabetical order, the file is sorted by names.
- [ ] Add a new news fragment into the `CHANGES` folder
  * name it `<issue_id>.<type>` for example (588.bugfix)
* if you don't have an `issue_id` change it to the pr id after creating
the pr
  * ensure type is one of the following:
    * `.feature`: Signifying a new feature.
    * `.bugfix`: Signifying a bug fix.
    * `.doc`: Signifying a documentation improvement.
    * `.removal`: Signifying a deprecation or removal of public API.
* `.misc`: A ticket has been closed, but it is not of interest to users.
* Make sure to use full sentences with correct case and punctuation, for
example: "Fix issue with non-ascii contents in doctest text files."
  • Loading branch information
Dreamsorcerer committed May 14, 2023
1 parent 44b8fd4 commit 64594af
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 10 deletions.
2 changes: 2 additions & 0 deletions CHANGES/6594.feature
@@ -0,0 +1,2 @@
Exported ``HTTPMove`` which can be used to catch any redirection request
that has a location -- :user:`dreamsorcerer`.
2 changes: 2 additions & 0 deletions aiohttp/web.py
Expand Up @@ -44,6 +44,7 @@
HTTPLengthRequired as HTTPLengthRequired,
HTTPMethodNotAllowed as HTTPMethodNotAllowed,
HTTPMisdirectedRequest as HTTPMisdirectedRequest,
HTTPMove as HTTPMove,
HTTPMovedPermanently as HTTPMovedPermanently,
HTTPMultipleChoices as HTTPMultipleChoices,
HTTPNetworkAuthenticationRequired as HTTPNetworkAuthenticationRequired,
Expand Down Expand Up @@ -177,6 +178,7 @@
"HTTPLengthRequired",
"HTTPMethodNotAllowed",
"HTTPMisdirectedRequest",
"HTTPMove",
"HTTPMovedPermanently",
"HTTPMultipleChoices",
"HTTPNetworkAuthenticationRequired",
Expand Down
17 changes: 9 additions & 8 deletions aiohttp/web_exceptions.py
Expand Up @@ -18,6 +18,7 @@
"HTTPNoContent",
"HTTPResetContent",
"HTTPPartialContent",
"HTTPMove",
"HTTPMultipleChoices",
"HTTPMovedPermanently",
"HTTPFound",
Expand Down Expand Up @@ -160,7 +161,7 @@ class HTTPPartialContent(HTTPSuccessful):
############################################################


class _HTTPMove(HTTPRedirection):
class HTTPMove(HTTPRedirection):
def __init__(
self,
location: StrOrURL,
Expand All @@ -184,21 +185,21 @@ def __init__(
self.location = location


class HTTPMultipleChoices(_HTTPMove):
class HTTPMultipleChoices(HTTPMove):
status_code = 300


class HTTPMovedPermanently(_HTTPMove):
class HTTPMovedPermanently(HTTPMove):
status_code = 301


class HTTPFound(_HTTPMove):
class HTTPFound(HTTPMove):
status_code = 302


# This one is safe after a POST (the redirected location will be
# retrieved with GET):
class HTTPSeeOther(_HTTPMove):
class HTTPSeeOther(HTTPMove):
status_code = 303


Expand All @@ -208,16 +209,16 @@ class HTTPNotModified(HTTPRedirection):
empty_body = True


class HTTPUseProxy(_HTTPMove):
class HTTPUseProxy(HTTPMove):
# Not a move, but looks a little like one
status_code = 305


class HTTPTemporaryRedirect(_HTTPMove):
class HTTPTemporaryRedirect(HTTPMove):
status_code = 307


class HTTPPermanentRedirect(_HTTPMove):
class HTTPPermanentRedirect(HTTPMove):
status_code = 308


Expand Down
4 changes: 2 additions & 2 deletions aiohttp/web_middlewares.py
Expand Up @@ -2,7 +2,7 @@
from typing import TYPE_CHECKING, Tuple, Type, TypeVar

from .typedefs import Handler, Middleware
from .web_exceptions import HTTPPermanentRedirect, _HTTPMove
from .web_exceptions import HTTPMove, HTTPPermanentRedirect
from .web_request import Request
from .web_response import StreamResponse
from .web_urldispatcher import SystemRoute
Expand Down Expand Up @@ -40,7 +40,7 @@ def normalize_path_middleware(
append_slash: bool = True,
remove_slash: bool = False,
merge_slashes: bool = True,
redirect_class: Type[_HTTPMove] = HTTPPermanentRedirect,
redirect_class: Type[HTTPMove] = HTTPPermanentRedirect,
) -> Middleware:
"""Factory for producing a middleware that normalizes the path of a request.
Expand Down

0 comments on commit 64594af

Please sign in to comment.