Skip to content

fix: patch urllib3.fields for urllib3-future compatibility#5890

Merged
jxxghp merged 1 commit into
jxxghp:v2from
DDSRem-Dev:fix/urllib3-future-telebot-compat
Jun 3, 2026
Merged

fix: patch urllib3.fields for urllib3-future compatibility#5890
jxxghp merged 1 commit into
jxxghp:v2from
DDSRem-Dev:fix/urllib3-future-telebot-compat

Conversation

@DDSRem-Bot

Copy link
Copy Markdown
Collaborator

urllib3-future replaces the urllib3 namespace and drops format_header_param (renamed to format_header_param_rfc2231 in 2.1+), causing pyTelegramBotAPI to raise AttributeError on import. Apply a compat shim at module level in lifecycle.py before any app modules are loaded.

urllib3-future replaces the urllib3 namespace and drops format_header_param
(renamed to format_header_param_rfc2231 in 2.1+), causing pyTelegramBotAPI
to raise AttributeError on import. Apply a compat shim at module level in
lifecycle.py before any app modules are loaded.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

@gemini-code-assist gemini-code-assist Bot 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.

Code Review

This pull request introduces a compatibility patch in app/startup/lifecycle.py to restore format_header_param in urllib3.fields if it is missing, preventing crashes in telebot when urllib3-future is used. It also reformats some imports. The feedback recommends replacing the broad except Exception: block with specific exceptions (ImportError, AttributeError) to avoid silently suppressing unrelated errors, and provides a code suggestion to implement this change.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

Comment thread app/startup/lifecycle.py
Comment on lines +7 to +17
try:
import urllib3.fields as _urllib3_fields

if not hasattr(_urllib3_fields, "format_header_param") and hasattr(
_urllib3_fields, "format_header_param_rfc2231"
):
_urllib3_fields.format_header_param = (
_urllib3_fields.format_header_param_rfc2231
)
except Exception:
pass

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.

medium

Using a broad except Exception: block is discouraged as it can silently suppress unexpected errors (such as NameError or TypeError if the code is refactored in the future). It is highly recommended to catch only the specific exceptions that are expected to be raised during this patch operation, namely ImportError (which covers ModuleNotFoundError if urllib3 is not installed) and AttributeError (if the attributes are missing or cannot be set).

Additionally, to ensure this compatibility shim is applied as early as possible, consider placing this block at the very top of the file (before from fastapi import FastAPI), or even in the main entry point (app/main.py), to prevent any transitive imports from loading urllib3 or telebot before the patch is active.

Suggested change
try:
import urllib3.fields as _urllib3_fields
if not hasattr(_urllib3_fields, "format_header_param") and hasattr(
_urllib3_fields, "format_header_param_rfc2231"
):
_urllib3_fields.format_header_param = (
_urllib3_fields.format_header_param_rfc2231
)
except Exception:
pass
try:
import urllib3.fields as _urllib3_fields
if not hasattr(_urllib3_fields, "format_header_param") and hasattr(
_urllib3_fields, "format_header_param_rfc2231"
):
_urllib3_fields.format_header_param = (
_urllib3_fields.format_header_param_rfc2231
)
except (ImportError, AttributeError):
pass
References
  1. PEP 8 recommends catching specific exceptions whenever possible instead of using a broad 'except Exception:' or bare 'except:' clause to avoid suppressing unrelated errors. (link)

@jxxghp jxxghp merged commit dc0ee2b into jxxghp:v2 Jun 3, 2026
1 check passed
@DDSRem DDSRem deleted the fix/urllib3-future-telebot-compat branch June 3, 2026 23:02
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