Skip to content

fix(util): drop the empty part smart_split appends, accept None in extract_arguments - #2648

Closed
beduldul wants to merge 1 commit into
eternnoir:masterfrom
beduldul:fix/util-split-and-none
Closed

beduldul wants to merge 1 commit into
eternnoir:masterfrom
beduldul:fix/util-split-and-none

Conversation

@beduldul

Copy link
Copy Markdown
Contributor

Description

Two input-handling bugs in telebot/util.py.

smart_split appends an empty part. It compared with <, so any text whose length is an exact multiple of chars_per_string went through the split path one last time on the remainder and appended '':

>>> smart_split("abc", 3)
['abc', '']          # expected ['abc']

>>> smart_split("a" * 4096)
['a'*4096, '']       # a full-length message

Sending that empty part to Telegram fails with Bad Request: message text is empty. Changed to <=, which covers every multiple, not just the 4096 case.

extract_arguments raises on None. It compiled the regexp and matched before checking the argument, so a non-text message (message.text is None) raised TypeError. is_command and extract_command already tolerate None, so this now matches them.

>>> extract_arguments(None)
TypeError: expected string or bytes-like object, got 'NoneType'

Describe your tests

Added tests/test_util.py — smart_split had no coverage at all. It walks exact multiples across several chunk sizes, checks the newline/space preference, the 4096 cap, and the None handling plus the normal extract_arguments cases.

Full suite: 314 passed, 73 skipped.

Python version: 3.12.3

OS: macOS 27.2

Checklist:

  • I added/edited example on new feature/change (if exists)
  • My changes won't break backward compatibility
  • I made changes both for sync and async

…tract_arguments

smart_split compared with `<`, so text whose length is an exact multiple of
chars_per_string went through the split path one last time on the remainder and
appended ''. smart_split("abc", 3) returned ["abc", ""], and sending that empty
part to Telegram fails with "message text is empty".

extract_arguments compiled its regexp and matched before looking at None, so a
non-text message raised TypeError on message.text. is_command and
extract_command already tolerate None, so this now matches them.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

No active review issues are identified for the input-handling fixes in the shared Telegram bot utilities.

No open issues 🎉

What changed in this PR

Fixes two input-handling bugs in shared Telegram bot utilities: trailing empty chunks from smart_split and errors when extract_arguments receives None.

Changes:

  • Stops splitting when the remaining text fits exactly within the chunk limit.
  • Returns None for non-text input to extract_arguments.
  • Adds regression tests for chunk boundaries, split preferences, length limits, and argument extraction.
File Description
tests/​test_util.py Adds regression coverage for splitting and argument extraction.
telebot/​util.py Fixes the split boundary condition and handles None arguments.

Review effort: Balanced

@Badiboy

Badiboy commented Oct 31, 2026 •

Copy link
Copy Markdown
Collaborator

Alsready applied here:
#2649

Also do not see anything changed for smart_split empty part here?

@Badiboy Badiboy closed this Oct 31, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants