Skip to content

Aiohttp request#126

Merged
Bibo-Joshi merged 9 commits intomainfrom
aiohttpRequest
Aug 2, 2025
Merged

Aiohttp request#126
Bibo-Joshi merged 9 commits intomainfrom
aiohttpRequest

Conversation

@Poolitzer
Copy link
Member

This is my take on solving python-telegram-bot/python-telegram-bot#4560. I think the implementation is correct, I copied all appropriate httpx tests over.

There are open points I need feedback on:
Implementation:

  • read_timeout property needs to be implemented, but this doesn't make sense since aiohttp doesn't have that timeout. Why do even have this property do we actually need it? Should I just pass the most fitting timeout like I do?
  • same, do_request needs all the httpx timeout params. They dont make sense at all with aiohttp, should I raise an error? I just ignore them anyway lol

Tests:

  • the two init tests are skipped because I dont get at all which params are selected in the httpx test suit to be passed here and which aren't, can someone explain?
  • the last test is skipped for now because I want to understand if I have to build my own bot instance or not
  • the failing test test_multiple_init_cycles I don't understand at all why it fails. Because the async with block should (as implemented and kinda tested above) properly shut down and initialize the instance, why doesn't it
  • and the timeout test raise makes the text xfail which is a) really funny and b) also kinda a successful test in its own, right?

Copy link
Member

@Bibo-Joshi Bibo-Joshi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi. Thanks for taking the time to PR for that :) I hope the authors of python-telegram-bot/python-telegram-bot#4560 appreciate it :)

  • read_timeout property needs to be implemented, but this doesn't make sense since aiohttp doesn't have that timeout. Why do even have this property do we actually need it? Should I just pass the most fitting timeout like I do?

This was introduced in python-telegram-bot/python-telegram-bot#3963 and enables Bot.get_updates to use the default read timeout specified in the request object rather than hard-coding a custom default value.

  • same, do_request needs all the httpx timeout params. They dont make sense at all with aiohttp, should I raise an error? I just ignore them anyway lol

IMO you should ensure that aiohttprequest.do_request(…, read_timeout=aiohttprequest.read_timeout) behaves the same as aiohttprequest.do_request(…). If aiohttp has a different set of timeout settings, then you should map our parameters to them as good as possible.
If you do not use all of them, I'd personally recommend to raise a warning (in case the passed value differs from DEFAULT_NONE) but not raise an exception.


I did not look at the tests yet

Comment on lines 25 to 26
from telegram._utils.logging import get_logger
from telegram._utils.types import ODVInput
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please do not use ptb internals

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have to, otherwise I can't check if the parameter is a default instance, or?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we have https://docs.python-telegram-bot.org/en/stable/telegram.request.baserequest.html#telegram.request.BaseRequest.DEFAULT_NONE in place. for the typing. I would suggest to copy-paste from PTB code if that helps …

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried that but then the isinstance call breaks because its not an instance of the copied class, it is of PTB.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no, I mean coyping the type annotation, not copying the DefaultValue class. for checking if the argument is DEFAULT_NONE, you shoul use if parameter is (not) BaseRequest.DEFAULT_NONE as described in the docs IMO :)

from typing import Any, Optional, Union

import aiohttp
import yarl
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is not listed in requirements.txt

Copy link
Member Author

@Poolitzer Poolitzer Jul 27, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No aiohttp depends on it, it is how they type hint an URL in their library.

Do I need to explicitly add it? Or can I rely on it implicitly, since when they drop it we would realize it in unit tests...

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd personally add it as requirement, yes, since you explicitly use it. but on ptbcontrib I'd be okay with skippen, then

Comment on lines 61 to 67
This parameter is intended for advanced users that want to fine-tune the behavior
of the underlying ``aiohttp`` clientSession. The values passed here will override
all the defaults set by ``python-telegram-bot`` and all other parameters passed to
:class:`ClientSession`. The only exception is the :paramref:`media_write_timeout`
parameter, which is not passed to the client constructor.
No runtime warnings will be issued about parameters that are overridden in this
way.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please update this. AiohttpRequest is no PTB class and there is also no parameter media_write_timeout here.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you mean with no PTB class? PTB will still pass defaults to this class, and passing this param will override these defaults

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not a class provided through the PTB package. so it's not used in the ApplicationBuilder and the defaults are not set by PTB, but by ptbcontrib :)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

well if I add it like in the ReadMe, applicationbuilder still adds its default timeouts

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, ApplicationBuilder will not build instances of AiohttpRequest, it doesn't know how to. you'd have to call https://docs.python-telegram-bot.org/en/stable/telegram.ext.applicationbuilder.html#telegram.ext.ApplicationBuilder.request and get_updates_request, passing a ready-build instance.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah okay. Though get_updates does kinda change the timeouts by default, which is a bit unfitting for this module, but it doesn't really matter since it just increases the timeout.

Comment on lines +90 to +91
# this is needed because there are errors if one uses async with or a normal def
# with ApplicationBuilder, apparently. I am confused. But it works.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what is "this" here? I am confused about what you're confused about.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The try except part around which loop function to use, you need one with the async def situation, and one for with ApplicationBuilder.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah same loop setup there

@Bibo-Joshi
Copy link
Member

@Poolitzer are you still interested in working on this?

@Poolitzer
Copy link
Member Author

yes just got busy

@Poolitzer
Copy link
Member Author

If aiohttp has a different set of timeout settings, then you should map our parameters to them as good as possible.

Well the issue is basically httpx (and thus PTB usage where someone sets custom timeouts) expects read, write, connect, pool timeout to basically add together to an overall timeout.

Aiohttp otoh has a total timeout for everything (hence by default I only set that, and to 15, which is the 5 default timeouts httpx implementation has summed up). Then it has further limits for some other parts of the connection process (for reverence).

I will implement it now as you said with a best effort match basis, but for the future it would make more sense to have one "timeout" param and each implementation can decide what they want to pass there in what form.

Poolitzer and others added 3 commits July 27, 2025 14:55
Co-authored-by: Bibo-Joshi <22366557+Bibo-Joshi@users.noreply.github.com>
@Bibo-Joshi Bibo-Joshi self-requested a review August 2, 2025 15:05
Copy link
Member

@Bibo-Joshi Bibo-Joshi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I only looked at the diff since my last review. Two minor findings

Co-authored-by: Bibo-Joshi <22366557+Bibo-Joshi@users.noreply.github.com>
@Bibo-Joshi Bibo-Joshi merged commit 7dec4c1 into main Aug 2, 2025
10 of 11 checks passed
@Bibo-Joshi Bibo-Joshi deleted the aiohttpRequest branch August 2, 2025 19:36
@Bibo-Joshi
Copy link
Member

Thank you very much for the contribution!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants