fix: patch urllib3.fields for urllib3-future compatibility#5890
Conversation
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>
There was a problem hiding this comment.
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.
| 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 |
There was a problem hiding this comment.
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.
| 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
- 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)
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.