MCPcopy Index your code
hub / github.com/python-telegram-bot/python-telegram-bot / _insert_defaults

Method _insert_defaults

src/telegram/_bot.py:643–684  ·  view source on GitHub ↗

This method is here to make ext.Defaults work. Because we need to be able to tell e.g. `send_message(chat_id, text)` from `send_message(chat_id, text, parse_mode=None)`, the default values for `parse_mode` etc are not `None` but `DEFAULT_NONE`. While this *could* be done in E

(self, data: dict[str, object])

Source from the content-addressed store, hash-verified

641 )
642
643 def _insert_defaults(self, data: dict[str, object]) -> None:
644 """This method is here to make ext.Defaults work. Because we need to be able to tell
645 e.g. `send_message(chat_id, text)` from `send_message(chat_id, text, parse_mode=None)`, the
646 default values for `parse_mode` etc are not `None` but `DEFAULT_NONE`. While this *could*
647 be done in ExtBot instead of Bot, shortcuts like `Message.reply_text` need to work for both
648 Bot and ExtBot, so they also have the `DEFAULT_NONE` default values.
649
650 This makes it necessary to convert `DefaultValue(obj)` to `obj` at some point between
651 `Message.reply_text` and the request to TG. Doing this here in a centralized manner is a
652 rather clean and minimally invasive solution, i.e. the link between tg and tg.ext is as
653 small as possible.
654 See also _insert_defaults_for_ilq
655 ExtBot overrides this method to actually insert default values.
656
657 If in the future we come up with a better way of making `Defaults` work, we can cut this
658 link as well.
659 """
660 # We
661 # 1) set the correct parse_mode for all InputMedia objects
662 # 2) replace all DefaultValue instances with the corresponding normal value.
663 for key, val in data.items():
664 # 1)
665 if isinstance(val, InputMedia):
666 # Copy object as not to edit it in-place
667 new = copy.copy(val)
668 with new._unfrozen():
669 new.parse_mode = DefaultValue.get_value(new.parse_mode)
670 data[key] = new
671 elif (
672 key == "media"
673 and isinstance(val, Sequence)
674 and not isinstance(val[0], InputPaidMedia)
675 ):
676 # Copy objects as not to edit them in-place
677 copy_list = [copy.copy(media) for media in val]
678 for media in copy_list:
679 with media._unfrozen():
680 media.parse_mode = DefaultValue.get_value(media.parse_mode)
681 data[key] = copy_list
682 # 2)
683 else:
684 data[key] = DefaultValue.get_value(val)
685
686 async def _post(
687 self,

Callers 1

_postMethod · 0.95

Calls 3

copyMethod · 0.80
_unfrozenMethod · 0.80
get_valueMethod · 0.80

Tested by

no test coverage detected