Repository navigation
Conversation
A nested "text" that is a number or a boolean reached check_json, which raises ValueError, or was turned into a RichTextBold holding text=5. RichText caught KeyError, TypeError and AttributeError but not ValueError, so the guard added for eternnoir#2647 still let that input crash a bot. Resolution failures now fall back to plain text at the only two places that can raise: the parsed-dict path, and a new _resolve used for the nested text of every RichText* subclass. A nested value that does not resolve to a single RichText no longer builds a parent whose text cannot be serialized back. ValueError on the top-level dict input is left to propagate: a dict we cannot parse is a caller mistake, not text that arrived from Telegram.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
RichText.de_jsoncatches resolution failures and falls back to the string, but the guard missesValueError:That error comes from
check_json, reached throughRichTextBold.de_json→RichText.de_json(5). Theexcept (KeyError, TypeError, AttributeError)inde_jsondoes not catch it, so a payload shaped like RichText but holding a scalar crashes the caller. Before that fix it raised too; the guard only ever covered missing keys.What this changes
Resolution failures fall back to plain text at the two places that can raise:
de_json, which now catchesValueErroras well;RichText._resolve, used for the nestedtextof everyRichText*subclass, which rejects a nested value that is not astr,listordict._resolvealso rejects a nested value that does not resolve to a single RichText. Without that,{"type": "bold", "text": {"type": "not_a_richtext"}}built aRichTextBoldwithtext=None, andto_dict()on it produced a payload Telegram rejects. Same fortext: [], which kept a list where one RichText is expected.The 22 subclasses that called
RichText.de_json(obj['text'])callcls._resolve(obj['text'])now, so the rule lives in one place instead of being repeated per class.UNRESOLVEDis a class attribute, not a module global, so nothing new appears intelebot.types.What stays as it is
ValueErrorfrom a top-level dict still propagates. A dict that cannot be parsed at all is a caller mistake, not text that arrived from Telegram.{"foo": "bar"},[56],{},{"a": [1, 2]},{"type": "not_a_richtext"}and a real serialized RichText all deserialize as before.mentionandhashtagreturn the string unchanged. That is existing behaviour (_from_dicthas no branch for either type, andRichTextMention/RichTextHashtagexist as classes but are never dispatched), not something this PR changes.Tests
Five cases in
tests/test_types.py:textof5,1.5,true,falsestays plain text, both throughRichText.de_jsonand throughRichBlockPreformatted;RichText*type survives5,trueand[]without raising.Full suite:
327 passed, 72 skipped.I also ran 175 type/value combinations and 500 random payloads against the change: no exception, and round-trip
de_json→to_dictis unchanged for valid input.Python 3.12.3, macOS 27.2.
Checklist: