From cfc75bb08ba07aac21cedd7d946fcb3bcaf4cdef Mon Sep 17 00:00:00 2001 From: Harshil <37377066+harshil21@users.noreply.github.com> Date: Sun, 30 Jun 2024 12:22:12 -0400 Subject: [PATCH] Bump `ruff` and Add New Rules (#4329) --- .pre-commit-config.yaml | 2 +- examples/passportbot.py | 20 ++++++++++---------- pyproject.toml | 7 +++---- telegram/__main__.py | 1 + telegram/ext/_jobqueue.py | 4 +++- tests/request/test_request.py | 4 ++-- tests/test_bot.py | 14 +++++++------- 7 files changed, 27 insertions(+), 25 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 08c51942a..a15b1f1bf 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -7,7 +7,7 @@ ci: repos: - repo: https://github.com/astral-sh/ruff-pre-commit - rev: 'v0.4.3' + rev: 'v0.5.0' hooks: - id: ruff name: ruff diff --git a/examples/passportbot.py b/examples/passportbot.py index e6d783240..c883e0f76 100644 --- a/examples/passportbot.py +++ b/examples/passportbot.py @@ -47,9 +47,9 @@ async def msg(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None: # Files will be downloaded to current directory for data in passport_data.decrypted_data: # This is where the data gets decrypted if data.type == "phone_number": - print("Phone: ", data.phone_number) + logger.info("Phone: %s", data.phone_number) elif data.type == "email": - print("Email: ", data.email) + logger.info("Email: %s", data.email) if data.type in ( "personal_details", "passport", @@ -58,7 +58,7 @@ async def msg(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None: "internal_passport", "address", ): - print(data.type, data.data) + logger.info(data.type, data.data) if data.type in ( "utility_bill", "bank_statement", @@ -66,28 +66,28 @@ async def msg(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None: "passport_registration", "temporary_registration", ): - print(data.type, len(data.files), "files") + logger.info(data.type, len(data.files), "files") for file in data.files: actual_file = await file.get_file() - print(actual_file) + logger.info(actual_file) await actual_file.download_to_drive() if ( data.type in ("passport", "driver_license", "identity_card", "internal_passport") and data.front_side ): front_file = await data.front_side.get_file() - print(data.type, front_file) + logger.info(data.type, front_file) await front_file.download_to_drive() if data.type in ("driver_license" and "identity_card") and data.reverse_side: reverse_file = await data.reverse_side.get_file() - print(data.type, reverse_file) + logger.info(data.type, reverse_file) await reverse_file.download_to_drive() if ( data.type in ("passport", "driver_license", "identity_card", "internal_passport") and data.selfie ): selfie_file = await data.selfie.get_file() - print(data.type, selfie_file) + logger.info(data.type, selfie_file) await selfie_file.download_to_drive() if data.translation and data.type in ( "passport", @@ -100,10 +100,10 @@ async def msg(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None: "passport_registration", "temporary_registration", ): - print(data.type, len(data.translation), "translation") + logger.info(data.type, len(data.translation), "translation") for file in data.translation: actual_file = await file.get_file() - print(actual_file) + logger.info(actual_file) await actual_file.download_to_drive() diff --git a/pyproject.toml b/pyproject.toml index 13ae98395..2484250c3 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -124,17 +124,16 @@ show-fixes = true [tool.ruff.lint] preview = true -explicit-preview-rules = true +explicit-preview-rules = true # TODO: Drop this when RUF022 and RUF023 are out of preview ignore = ["PLR2004", "PLR0911", "PLR0912", "PLR0913", "PLR0915", "PERF203"] select = ["E", "F", "I", "PL", "UP", "RUF", "PTH", "C4", "B", "PIE", "SIM", "RET", "RSE", "G", "ISC", "PT", "ASYNC", "TCH", "SLOT", "PERF", "PYI", "FLY", "AIR", "RUF022", - "RUF023", "Q", "INP", "W", "YTT", "DTZ", "ARG"] -# Add "FURB" after it's out of preview + "RUF023", "Q", "INP", "W", "YTT", "DTZ", "ARG", "T20", "FURB"] # Add "A (flake8-builtins)" after we drop pylint [tool.ruff.lint.per-file-ignores] "tests/*.py" = ["B018"] -"tests/**.py" = ["RUF012", "ASYNC101", "DTZ", "ARG"] +"tests/**.py" = ["RUF012", "ASYNC230", "DTZ", "ARG", "T201"] "docs/**.py" = ["INP001", "ARG"] "examples/**.py" = ["ARG"] diff --git a/telegram/__main__.py b/telegram/__main__.py index 7f79dc027..2491a330a 100644 --- a/telegram/__main__.py +++ b/telegram/__main__.py @@ -17,6 +17,7 @@ # You should have received a copy of the GNU Lesser Public License # along with this program. If not, see [http://www.gnu.org/licenses/]. # pylint: disable=missing-module-docstring +# ruff: noqa: T201 import subprocess import sys from typing import Optional diff --git a/telegram/ext/_jobqueue.py b/telegram/ext/_jobqueue.py index 6edd5a892..db84841d5 100644 --- a/telegram/ext/_jobqueue.py +++ b/telegram/ext/_jobqueue.py @@ -106,7 +106,9 @@ class JobQueue(Generic[CCT]): self._application: Optional[weakref.ReferenceType[Application]] = None self._executor = AsyncIOExecutor() - self.scheduler: "AsyncIOScheduler" = AsyncIOScheduler(**self.scheduler_configuration) + self.scheduler: "AsyncIOScheduler" = AsyncIOScheduler( # noqa: UP037 + **self.scheduler_configuration + ) def __repr__(self) -> str: """Give a string representation of the JobQueue in the form ``JobQueue[application=...]``. diff --git a/tests/request/test_request.py b/tests/request/test_request.py index ecfb65ece..0f664cbdb 100644 --- a/tests/request/test_request.py +++ b/tests/request/test_request.py @@ -605,9 +605,9 @@ class TestHTTPXRequestWithoutRequest: read_timeout=default_timeouts.read, write_timeout=default_timeouts.write, pool_timeout=default_timeouts.pool, - ) as httpx_request: + ) as httpx_request_ctx: monkeypatch.setattr(httpx.AsyncClient, "request", make_assertion) - await httpx_request.do_request( + await httpx_request_ctx.do_request( method="GET", url="URL", connect_timeout=manual_timeouts.connect, diff --git a/tests/test_bot.py b/tests/test_bot.py index d22ea96db..f3902f33a 100644 --- a/tests/test_bot.py +++ b/tests/test_bot.py @@ -559,11 +559,11 @@ class TestBotWithoutRequest: copied_result = copy.copy(result) ext_bot = bot - for bot in (ext_bot, raw_bot): + for bot_type in (ext_bot, raw_bot): # We need to test 1) below both the bot and raw_bot and setting this up with # pytest.parametrize appears to be difficult ... - monkeypatch.setattr(bot.request, "post", make_assertion) - web_app_msg = await bot.answer_web_app_query("12345", result) + monkeypatch.setattr(bot_type.request, "post", make_assertion) + web_app_msg = await bot_type.answer_web_app_query("12345", result) assert params, "something went wrong with passing arguments to the request" assert isinstance(web_app_msg, SentWebAppMessage) assert web_app_msg.inline_message_id == "321" @@ -761,11 +761,11 @@ class TestBotWithoutRequest: copied_results = copy.copy(results) ext_bot = bot - for bot in (ext_bot, raw_bot): + for bot_type in (ext_bot, raw_bot): # We need to test 1) below both the bot and raw_bot and setting this up with # pytest.parametrize appears to be difficult ... - monkeypatch.setattr(bot.request, "post", make_assertion) - assert await bot.answer_inline_query( + monkeypatch.setattr(bot_type.request, "post", make_assertion) + assert await bot_type.answer_inline_query( 1234, results=results, cache_time=300, @@ -790,7 +790,7 @@ class TestBotWithoutRequest: copied_results[idx].input_message_content, "disable_web_page_preview", None ) - monkeypatch.delattr(bot.request, "post") + monkeypatch.delattr(bot_type.request, "post") async def test_answer_inline_query_no_default_parse_mode(self, monkeypatch, bot): async def make_assertion(url, request_data: RequestData, *args, **kwargs):