Repository: aiogram/aiogram
Pull request #1117 (2023-02-08 15:36:45)

add new method for Router
# Description

<!--
Please include a summary of the change.

e.g. Add a new awesome feature or Fix documentation typo.

Please also include relevant motivation and context.
If you are fixing an issue, specify what issue is fixed.

e.g. Fix #12345
-->

Added a method that allows you to compactly register routers

## Type of change

<!--
Please delete options that are not relevant.
-->

- Breaking change (fix or feature that would cause existing functionality to not work as expected)
- Bug fix (non-breaking change that fixes an issue)
- New feature (non-breaking change that adds functionality)
- Documentation (typos, code examples or any documentation update)

# How has this been tested?

<!--
Please describe the tests that you ran to verify your changes. Provide instructions so we can reproduce.
Please also list any relevant details for your test configuration.
-->

Type here...

## Test Configuration
- Operating system: Windows 10
- Python version: 3.10.9

# Checklist:

<!--
Please delete options that are not relevant to your change.
-->

- [x] My code follows the style guidelines of this project
- [x] I have performed a self-review of my own code
- [x] I have added tests that prove my fix is effective or that my feature works as expected
- [x] New and existing unit tests pass locally with my changes
- [x] My changes generate no new warnings or errors
- [x] My changes are compatible with minimum requirements of the project
- [x] I have made corresponding changes to the documentation


github-actions[bot]: # :x: Changelog is required!

You need to add a brief description of the changes to the `CHANGES` directory.

For example, you can run `towncrier create <issue>.<type>` to create a file in the change directory and then write a description on that file.

Read more at [Towncrier docs](https://towncrier.readthedocs.io/en/latest/quickstart.html#creating-news-fragments)
codecov[bot]: # [Codecov](https://codecov.io/gh/aiogram/aiogram/pull/1117?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=aiogram) Report
> Merging [#1117](https://codecov.io/gh/aiogram/aiogram/pull/1117?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=aiogram) (9550f6a) into [dev-3.x](https://codecov.io/gh/aiogram/aiogram/commit/e8d279c8f2d904390cb800c1a5a156aff5427f19?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=aiogram) (e8d279c) will **not change** coverage.
> The diff coverage is `100.00%`.

[![Impacted file tree graph](https://codecov.io/gh/aiogram/aiogram/pull/1117/graphs/tree.svg?width=650&height=150&src=pr&token=cFqEr3p9MX&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=aiogram)](https://codecov.io/gh/aiogram/aiogram/pull/1117?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=aiogram)

```diff
@@            Coverage Diff            @@
##           dev-3.x     #1117   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files          340       340           
  Lines         8829      8834    +5     
=========================================
+ Hits          8829      8834    +5     
```

| Flag | Coverage Δ | |
|---|---|---|
| unittests | `100.00% <100.00%> (ø)` | |

Flags with carried forward coverage won't be shown. [Click here](https://docs.codecov.io/docs/carryforward-flags?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=aiogram#carryforward-flags-in-the-pull-request-comment) to find out more.

| [Impacted Files](https://codecov.io/gh/aiogram/aiogram/pull/1117?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=aiogram) | Coverage Δ | |
|---|---|---|
| [aiogram/dispatcher/router.py](https://codecov.io/gh/aiogram/aiogram/pull/1117?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=aiogram#diff-YWlvZ3JhbS9kaXNwYXRjaGVyL3JvdXRlci5weQ==) | `100.00% <100.00%> (ø)` | |


Patch:
diff --git a/CHANGES/1117.feature.rst b/CHANGES/1117.feature.rst
new file mode 100644
index 00000000..cdcf0999
--- /dev/null
+++ b/CHANGES/1117.feature.rst
@@ -0,0 +1,1 @@
+Added a method that allows you to compactly register routers
\ No newline at end of file
diff --git a/aiogram/dispatcher/router.py b/aiogram/dispatcher/router.py
index f1ddc1c4..86359ce0 100644
--- a/aiogram/dispatcher/router.py
+++ b/aiogram/dispatcher/router.py
@@ -179,6 +179,12 @@ class Router:
         self._parent_router = router
         router.sub_routers.append(self)
 
+    def include_routers(self, *routers: Router) -> None:
+        if not routers:
+            raise ValueError("At least one router must be provided")
+        for router in routers:
+            self.include_router(router)
+
     def include_router(self, router: Router) -> Router:
         """
         Attach another router.


Test patch:
diff --git a/tests/test_dispatcher/test_router.py b/tests/test_dispatcher/test_router.py
index 673bb46d..3404822d 100644
--- a/tests/test_dispatcher/test_router.py
+++ b/tests/test_dispatcher/test_router.py
@@ -33,6 +33,20 @@ class TestRouter:
         assert router3.parent_router is router2
         assert router3.sub_routers == []
 
+    def test_including_many_routers(self):
+        router = Router()
+        router1 = Router()
+        router2 = Router()
+
+        router.include_routers(router1, router2)
+
+        assert router.sub_routers == [router1, router2]
+
+    def test_including_many_routers_bad_type(self):
+        router = Router()
+        with pytest.raises(ValueError, match="At least one router must be provided"):
+            router.include_routers()
+
     def test_include_router_by_string_bad_type(self):
         router = Router()
         with pytest.raises(ValueError, match=r"router should be instance of Router"):