-
-
Notifications
You must be signed in to change notification settings - Fork 2k
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
UrlDispatcher - add_routes returns a list of AbstractRoutes #4141
Conversation
First, thank you for the feedback at #3866, but a part of my question was still left unanswered - what about the static routes? I figured it made more sense to make a PR so we can discuss it here where we can reference the code. |
Codecov Report
@@ Coverage Diff @@
## master #4141 +/- ##
==========================================
- Coverage 97.57% 97.55% -0.03%
==========================================
Files 43 43
Lines 8825 8833 +8
Branches 1381 1381
==========================================
+ Hits 8611 8617 +6
- Misses 92 93 +1
- Partials 122 123 +1
Continue to review full report at Codecov.
|
aiohttp/web_routedef.py
Outdated
@@ -36,7 +39,7 @@ | |||
|
|||
class AbstractRouteDef(abc.ABC): | |||
@abc.abstractmethod | |||
def register(self, router: UrlDispatcher) -> None: | |||
def register(self, router: UrlDispatcher) -> Optional[AbstractRoute]: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is basically the part I have the most questions about. So far, I've made this return value Optional
(and everywhere else where its return value is propagated). The reason for this is because of the StaticDef and its register
implementation. Since add_static
returns an AbstractResource
, do you think it would be sensible for this to be a union of AbstractRoute
and AbstractResource
for that reason?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
StaticResource
internally has self._routes
which is a dict of ResourceRoute
instances.
Perhaps we can extend resource.get_info()
to return routes mapping; it can avoid access to private members.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Commented
aiohttp/web_app.py
Outdated
self.router.add_routes(routes) | ||
def add_routes( | ||
self, routes: Iterable[AbstractRouteDef] | ||
) -> List[Optional[AbstractRoute]]: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Optional
is bad here. The return type should be a strict list.
) -> List[Optional[AbstractRoute]]: | |
) -> List[AbstractRoute]: |
aiohttp/web_routedef.py
Outdated
@@ -36,7 +39,7 @@ | |||
|
|||
class AbstractRouteDef(abc.ABC): | |||
@abc.abstractmethod | |||
def register(self, router: UrlDispatcher) -> None: | |||
def register(self, router: UrlDispatcher) -> Optional[AbstractRoute]: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
StaticResource
internally has self._routes
which is a dict of ResourceRoute
instances.
Perhaps we can extend resource.get_info()
to return routes mapping; it can avoid access to private members.
All, right, I've updated the changes in the I have another question about the tests: I've noticed that there weren't any that check the |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Implementation is good. Please update docs to fix my notes.
docs/web_reference.rst
Outdated
@@ -1413,6 +1413,8 @@ duplicated like one using :meth:`Application.copy`. | |||
The table is a :class:`list` of :class:`RouteDef` items or | |||
:class:`RouteTableDef`. | |||
|
|||
Returns a :class:`list` of registered :class:`AbstractRoute` instances. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- Please use
:returns:
markup as in the method above. - Please add
.. versionchanged:: 3.7
markup, search in docs for the directive usage. The master is not ready for 4.0 release yet, I think we can publish 3.7 earlier.
docs/web_reference.rst
Outdated
@@ -1569,6 +1571,8 @@ Router is any object that implements :class:`AbstractRouter` interface. | |||
The table is a :class:`list` of :class:`RouteDef` items or | |||
:class:`RouteTableDef`. | |||
|
|||
Returns a :class:`list` of registered :class:`AbstractRoute` instances. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The same
docs/web_reference.rst
Outdated
@@ -2080,6 +2084,8 @@ The definition is created by functions like :func:`get` or | |||
|
|||
Abstract method, should be overridden by subclasses. | |||
|
|||
Returns a list of registered `AbstractRoute` objects. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And the same
I've looked up how it's used, hope I got |
Co-Authored-By: Andrew Svetlov <[email protected]>
Co-Authored-By: Andrew Svetlov <[email protected]>
Co-Authored-By: Andrew Svetlov <[email protected]>
Co-Authored-By: Andrew Svetlov <[email protected]>
Co-Authored-By: Andrew Svetlov <[email protected]>
Co-Authored-By: Andrew Svetlov <[email protected]>
Thanks! |
) Co-Authored-By: Andrew Svetlov <[email protected]>. (cherry picked from commit 60e6c22) Co-authored-by: Zlatan <[email protected]>
) (#4233) Co-Authored-By: Andrew Svetlov <[email protected]>. (cherry picked from commit 60e6c22) Co-authored-by: Zlatan <[email protected]>
Don't mention it, glad to help on a library I often use 😄 |
What do these changes do?
These changes are supposed to solve the issue #3866, this is just the basic infrastructure of the changes, some discussion about the changes is still required.
Are there changes in behavior for the user?
aiohttp.web.UrlDispatcher.add_routes
andaiohttp.web.Application.add_routes
return a list of the registeredAbstractRoute
instances. As a consequence,RouteDef.register
now also returns the registeredAbstractRoute
Related issue number
#3866
Checklist
CONTRIBUTORS.txt
CHANGES
folder<issue_id>.<type>
for example (588.bugfix)issue_id
change it to the pr id after creating the pr.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.