Skip to content

Commit 3cfdab0

Browse files
committed
fix(run): alert user about using run inside a loop
1 parent fd5beaa commit 3cfdab0

File tree

3 files changed

+15
-13
lines changed

3 files changed

+15
-13
lines changed

.pre-commit-config.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ repos:
1111
- id: check-yaml
1212

1313
- repo: https://github.com/astral-sh/ruff-pre-commit
14-
rev: v0.4.6
14+
rev: v0.4.7
1515
hooks:
1616
- id: ruff-format
1717
- id: ruff

hydrogram/methods/utilities/run.py

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
# along with Hydrogram. If not, see <http://www.gnu.org/licenses/>.
1919

2020
import asyncio
21-
import inspect
2221
from typing import TYPE_CHECKING
2322

2423
from hydrogram.methods.utilities.idle import idle
@@ -32,12 +31,12 @@ def run(self: "hydrogram.Client", coroutine=None):
3231
"""Start the client, idle the main script and finally stop the client.
3332
3433
When calling this method without any argument it acts as a convenience method that calls
35-
:meth:`~hydrogram.Client.start`, :meth:`~hydrogram.idle` and :meth:`~hydrogram.Client.stop` in sequence.
36-
It makes running a single client less verbose.
34+
:meth:`~hydrogram.Client.start`, :meth:`~hydrogram.idle` and :meth:`~hydrogram.Client.stop`
35+
in sequence. It makes running a single client less verbose.
3736
38-
In case a coroutine is passed as argument, runs the coroutine until it's completed and doesn't do any client
39-
operation. This is almost the same as :py:obj:`asyncio.run` except for the fact that Hydrogram's ``run`` uses the
40-
current event loop instead of a new one.
37+
In case a coroutine is passed as argument, runs the coroutine until it's completed and
38+
doesn't do any client operation. This is almost the same as :py:obj:`asyncio.run` except
39+
for the fact that Hydrogram's ``run`` uses the current event loop instead of a new one.
4140
4241
If you want to run multiple clients at once, see :meth:`hydrogram.compose`.
4342
@@ -76,11 +75,14 @@ async def main():
7675

7776
if coroutine is not None:
7877
run(coroutine)
79-
elif inspect.iscoroutinefunction(self.start):
78+
else:
79+
if loop.is_running():
80+
raise RuntimeError(
81+
"You must call client.run() method outside of an asyncio event loop. "
82+
"Otherwise, you can use client.start(), client.idle(), and client.stop() "
83+
"methods. Refer to https://docs.hydrogram.org/en/latest/api/methods/run.html"
84+
)
85+
8086
run(self.start())
8187
run(idle())
8288
run(self.stop())
83-
else:
84-
self.start()
85-
run(idle())
86-
self.stop()

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ build-backend = "hatchling.build"
5151
[tool.rye]
5252
managed = true
5353
dev-dependencies = [
54-
"ruff>=0.4.6",
54+
"ruff>=0.4.7",
5555
"pytest>=7.4.3",
5656
"pytest-asyncio>=0.23.2",
5757
"pytest-cov>=4.1.0",

0 commit comments

Comments
 (0)