Skip to content

Use Sequence instead of Union[List[…], Tuple[…]] #3350

@Bibo-Joshi

Description

@Bibo-Joshi

Currently we accept only List for arguments that Telegram documents as "array". This limitation is rather unnecessary. Passing a tuple will in fact also work out of the box since the json module automatically converts tuples to json arrays.

IMO it's sane to expect every object that is iterable, with finite length and fixed order of elements to work - which brings us to Sequence. Once could argue that we don't strictly need fixed order and instead go for Collection, but for most objects, the order of elements does change the behavior of Telegram and using Sequence avoids one possible source of confusion for users. The only example I can think of where order doesn't matter are the (caption_)entities arguments …

This is also in symmetry with #3249, which accepts Sequence for class arguments and converts them to tuple.

The only downside of this approach is that we need one additional check somewhere in RequestParameter along the lines

if not isinstance(object, (tuple, list)):
    object = list(object)

as otherwise json.dumps would fail.

Edit: More precisely, the edit would be required here:

@classmethod
def from_input(cls, key: str, value: object) -> "RequestParameter":
"""Builds an instance of this class for a given key-value pair that represents the raw
input as passed along from a method of :class:`telegram.Bot`.
"""
if isinstance(value, list):

Metadata

Metadata

Assignees

Labels

No labels
No labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions