File: kwargs_insertion.py

package info (click to toggle)
python-telegram-bot 22.3-1
  • links: PTS
  • area: main
  • in suites: sid
  • size: 11,060 kB
  • sloc: python: 90,298; makefile: 176; sh: 4
file content (108 lines) | stat: -rw-r--r-- 4,005 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#
#  A library that provides a Python interface to the Telegram Bot API
#  Copyright (C) 2015-2025
#  Leandro Toledo de Souza <devs@python-telegram-bot.org>
#
#  This program is free software: you can redistribute it and/or modify
#  it under the terms of the GNU Lesser Public License as published by
#  the Free Software Foundation, either version 3 of the License, or
#  (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU Lesser Public License for more details.
#
#  You should have received a copy of the GNU Lesser Public License
#  along with this program.  If not, see [http://www.gnu.org/licenses/].
import inspect

keyword_args = [
    "Keyword Arguments:",
    (
        "    read_timeout (:obj:`float` | :obj:`None`, optional): Value to pass to "
        "        :paramref:`telegram.request.BaseRequest.post.read_timeout`. Defaults to "
        "        :attr:`~telegram.request.BaseRequest.DEFAULT_NONE`. "
    ),
    (
        "    write_timeout (:obj:`float` | :obj:`None`, optional): Value to pass to "
        "        :paramref:`telegram.request.BaseRequest.post.write_timeout`. Defaults to "
        "        :attr:`~telegram.request.BaseRequest.DEFAULT_NONE`."
    ),
    (
        "    connect_timeout (:obj:`float` | :obj:`None`, optional): Value to pass to "
        "        :paramref:`telegram.request.BaseRequest.post.connect_timeout`. Defaults to "
        "        :attr:`~telegram.request.BaseRequest.DEFAULT_NONE`."
    ),
    (
        "    pool_timeout (:obj:`float` | :obj:`None`, optional): Value to pass to "
        "        :paramref:`telegram.request.BaseRequest.post.pool_timeout`. Defaults to "
        "        :attr:`~telegram.request.BaseRequest.DEFAULT_NONE`."
    ),
    (
        "    api_kwargs (:obj:`dict`, optional): Arbitrary keyword arguments"
        "        to be passed to the Telegram API. See :meth:`~telegram.Bot.do_api_request` for"
        "        limitations."
    ),
    "",
]

media_write_timeout_change_methods = [
    "add_sticker_to_set",
    "create_new_sticker_set",
    "send_animation",
    "send_audio",
    "send_document",
    "send_media_group",
    "send_photo",
    "send_sticker",
    "send_video",
    "send_video_note",
    "send_voice",
    "set_chat_photo",
    "upload_sticker_file",
]
media_write_timeout_change = [
    "    write_timeout (:obj:`float` | :obj:`None`, optional): Value to pass to "
    "        :paramref:`telegram.request.BaseRequest.post.write_timeout`. By default, ``20`` "
    "        seconds are used as write timeout."
    "",
    "",
    "       .. versionchanged:: 22.0",
    "           The default value changed to "
    "           :attr:`~telegram.request.BaseRequest.DEFAULT_NONE`.",
    "",
    "",
]
get_updates_read_timeout_addition = [
    "        :paramref:`timeout` will be added to this value.",
    "",
    "",
    "        .. versionchanged:: 20.7",
    "           Defaults to :attr:`~telegram.request.BaseRequest.DEFAULT_NONE` instead of ",
    "           ``2``.",
]


def find_insert_pos_for_kwargs(lines: list[str]) -> int:
    """Finds the correct position to insert the keyword arguments and returns the index."""
    for idx, value in reversed(list(enumerate(lines))):  # reversed since :returns: is at the end
        if value.startswith("Returns"):
            return idx
    return False


def check_timeout_and_api_kwargs_presence(obj: object) -> int:
    """Checks if the method has timeout and api_kwargs keyword only parameters."""
    sig = inspect.signature(obj)
    params_to_check = (
        "read_timeout",
        "write_timeout",
        "connect_timeout",
        "pool_timeout",
        "api_kwargs",
    )
    return all(
        param in sig.parameters and sig.parameters[param].kind == inspect.Parameter.KEYWORD_ONLY
        for param in params_to_check
    )