From ff967e5636f25474c814ed9f97036292d5c6fc10 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Afonso=20Janu=C3=A1rio?= Date: Tue, 8 Sep 2026 10:08:17 +0100 Subject: [PATCH] Avoid the chardet.universaldetector deprecation warning chardet 7 moved UniversalDetector to chardet.detector and turned the old chardet.universaldetector import path into a stub that raises a DeprecationWarning on import. Projects that run their test suites with warnings turned into errors (like Flexget, per the linked issue) hit a hard failure the moment html5lib falls back to chardet for encoding detection. Try the new import path first and fall back to the old one, so this keeps working across both old and new chardet releases without triggering the warning on the new ones. Fixes #601. --- AUTHORS.rst | 1 + CHANGES.rst | 3 +++ html5lib/_inputstream.py | 12 +++++++++--- 3 files changed, 13 insertions(+), 3 deletions(-) diff --git a/AUTHORS.rst b/AUTHORS.rst index 90401390..998be2f4 100644 --- a/AUTHORS.rst +++ b/AUTHORS.rst @@ -63,4 +63,5 @@ Patches and suggestions - Ville Skyttä - Hugo van Kemenade - Mark Vasilkov +- Afonso Januário diff --git a/CHANGES.rst b/CHANGES.rst index 47dcda3a..ca514ece 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -20,6 +20,9 @@ Bug fixes: * The sanitizer now permits ```` tags. It used to allow ``
`` already. (#423) +* Encoding detection no longer triggers a ``DeprecationWarning`` under + chardet 7+, which moved ``UniversalDetector`` out of + ``chardet.universaldetector``. (#601) 1.1 ~~~ diff --git a/html5lib/_inputstream.py b/html5lib/_inputstream.py index a93b5a4e..a024f4b6 100644 --- a/html5lib/_inputstream.py +++ b/html5lib/_inputstream.py @@ -483,10 +483,16 @@ def determineEncoding(self, chardet=True): # Guess with chardet, if available if chardet: try: - from chardet.universaldetector import UniversalDetector + # chardet 7+ moved UniversalDetector here; the older + # chardet.universaldetector path still works but now raises + # a DeprecationWarning. + from chardet.detector import UniversalDetector except ImportError: - pass - else: + try: + from chardet.universaldetector import UniversalDetector + except ImportError: + UniversalDetector = None + if UniversalDetector is not None: buffers = [] detector = UniversalDetector() while not detector.done: