Fix RichText - #2615
Merged
Merged
Conversation
1. It's now ABC 2. de_json with json-string was lost. Now it processes string under some heuristics.
Collaborator
Author
|
@coder2020official - please, take a fast look. Update is small. |
Contributor
There was a problem hiding this comment.
Pull request overview
This PR updates the rich-text deserialization layer in telebot/types.py, aiming to (1) formalize base rich-text types as abstract bases and (2) restore support for passing JSON-encoded strings into de_json via heuristics.
Changes:
- Make
RichTextandRichBlockinherit fromABC. - Update
RichText.de_jsonto treat somestrinputs as JSON and parse them instead of always returning the raw string.
Suppressed comments (1)
telebot/types.py:14743
- RichText.de_json currently treats any valid JSON string as structured rich text, but it then calls check_json() and assumes the result is a dict (uses obj.pop). For inputs like '"hello"' or '[]' (valid JSON that parses to str/list), check_json() returns a non-dict and this will raise at obj.pop. Also, this file optionally aliases
ujsonasjson(types.py:12-16), so referencingjson.JSONDecodeErrorcan fail if ujson doesn't expose that exception type.
try:
# Check if string is valid json. If so, assume it's json, not plain string
json.loads(json_string)
# If it's valid json, continue to json processing below
except (ValueError, TypeError, json.JSONDecodeError):
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
Comment on lines
14735
to
14736
| @@ -14736,8 +14736,14 @@ def de_json(cls, json_string): | |||
| if json_string is None: return None | |||
coder2020official
approved these changes
Aug 15, 2026
"123" is also a valid json :(
Comment on lines
+14739
to
+14748
| if (json_string.startswith('{') and json_string.endswith('}')) or (json_string.startswith('[') and json_string.endswith(']')): | ||
| try: | ||
| # Check if string is valid json. If so, assume it's json, not plain string | ||
| json.loads(json_string) | ||
| # If it's valid json, continue to json processing below | ||
| except (ValueError, TypeError, json.JSONDecodeError): | ||
| # If it's not valid json, return the string as is | ||
| return json_string | ||
| else: | ||
| return json_string |
Collaborator
Author
|
Damn, I include wrong files in last fix and did not noticed that... 🙈🙈🙈 |
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.
Description