From dd5e8840d1bda5ea29d410cdf396709474afde2f Mon Sep 17 00:00:00 2001 From: Alex Chen Date: Thu, 16 Jul 2026 16:10:47 +0000 Subject: [PATCH 1/4] Skip test_lazy when html.parser is already imported Django and similar packages import html.parser at startup, which makes the lazy-load pre-condition of test_lazy impossible. Skip in that case instead of failing the suite. Fixes #335 Signed-off-by: Alex Chen --- CONTRIBUTORS | 1 + test_six.py | 5 ++++- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/CONTRIBUTORS b/CONTRIBUTORS index be72290d..46ea1754 100644 --- a/CONTRIBUTORS +++ b/CONTRIBUTORS @@ -5,6 +5,7 @@ otherwise worked to improve six: Marc Abramowitz immerrr again Alexander Artemenko +Alex Chen Aymeric Augustin Lee Ball Ben Bariteau diff --git a/test_six.py b/test_six.py index 8890c0e3..f5a420a2 100644 --- a/test_six.py +++ b/test_six.py @@ -91,7 +91,10 @@ def test_lazy(): html_name = "html.parser" else: html_name = "HTMLParser" - assert html_name not in sys.modules + # Django and other packages may import html.parser at import time, which + # makes the pre-condition of this lazy-load test impossible to satisfy. + if html_name in sys.modules: + pytest.skip("%s already imported (e.g. by django)" % html_name) mod = six.moves.html_parser assert sys.modules[html_name] is mod assert "htmlparser" not in six._MovedItems.__dict__ From a9cb53de5a6958fc632f1fbf7ae202118e7b5718 Mon Sep 17 00:00:00 2001 From: Alex Chen Date: Thu, 16 Jul 2026 17:25:38 +0000 Subject: [PATCH 2/4] test: reword test_lazy skip for preloaded html.parser The six suite does not import Django. Skip only when the lazy-load pre-condition is already broken because html.parser/HTMLParser is present in sys.modules from the surrounding environment. Signed-off-by: Alex Chen --- test_six.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test_six.py b/test_six.py index f5a420a2..38c0d95c 100644 --- a/test_six.py +++ b/test_six.py @@ -91,10 +91,10 @@ def test_lazy(): html_name = "html.parser" else: html_name = "HTMLParser" - # Django and other packages may import html.parser at import time, which - # makes the pre-condition of this lazy-load test impossible to satisfy. + # Skip when the lazy-load pre-condition is already broken (module present + # in sys.modules from the surrounding environment / site-packages). if html_name in sys.modules: - pytest.skip("%s already imported (e.g. by django)" % html_name) + pytest.skip("%s already imported" % html_name) mod = six.moves.html_parser assert sys.modules[html_name] is mod assert "htmlparser" not in six._MovedItems.__dict__ From a882a1171d1e82254c1c2efd978f02cfcb44e180 Mon Sep 17 00:00:00 2001 From: Alex Chen Date: Thu, 16 Jul 2026 17:35:35 +0000 Subject: [PATCH 3/4] test: document why test_lazy sees preloaded html.parser six does not import Django. When Django is installed and an ambient pytest plugin imports it at collection time (hypothesis.internal.compat -> django.test -> django.utils.html), html.parser is already in sys.modules and the lazy-load pre-condition cannot hold. Skip in that case; keep the test when the process is clean. Signed-off-by: Alex Chen --- test_six.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/test_six.py b/test_six.py index 38c0d95c..dc00bffa 100644 --- a/test_six.py +++ b/test_six.py @@ -91,8 +91,13 @@ def test_lazy(): html_name = "html.parser" else: html_name = "HTMLParser" - # Skip when the lazy-load pre-condition is already broken (module present - # in sys.modules from the surrounding environment / site-packages). + # test_lazy needs a clean process where html.parser/HTMLParser is not yet + # imported, so it can prove six.moves.html_parser loads it lazily. + # six itself never imports Django. In polluted site-packages, ambient + # pytest plugins may import Django at collection time when it is installed + # (observed path: hypothesis.internal.compat -> django.test -> ... -> + # django.utils.html -> html.parser; see issue #335 with hypothesis in the + # pytest plugins list). Then the pre-condition is already false. if html_name in sys.modules: pytest.skip("%s already imported" % html_name) mod = six.moves.html_parser From 310f6e5c0f5a3b586977c659b60dfc3fa71d6b7c Mon Sep 17 00:00:00 2001 From: Alex Chen Date: Thu, 16 Jul 2026 18:06:16 +0000 Subject: [PATCH 4/4] test: disable pytest plugin autoload under tox test_lazy needs a clean process. Ambient site-packages plugins can be auto-loaded by pytest and import unrelated packages before the suite runs. Set PYTEST_DISABLE_PLUGIN_AUTOLOAD=1 in tox (pytest-only deps) so the suite does not pick up those plugins. Restore the original pre-condition assert instead of skipping on pollution. Signed-off-by: Alex Chen --- test_six.py | 10 +--------- tox.ini | 2 ++ 2 files changed, 3 insertions(+), 9 deletions(-) diff --git a/test_six.py b/test_six.py index dc00bffa..8890c0e3 100644 --- a/test_six.py +++ b/test_six.py @@ -91,15 +91,7 @@ def test_lazy(): html_name = "html.parser" else: html_name = "HTMLParser" - # test_lazy needs a clean process where html.parser/HTMLParser is not yet - # imported, so it can prove six.moves.html_parser loads it lazily. - # six itself never imports Django. In polluted site-packages, ambient - # pytest plugins may import Django at collection time when it is installed - # (observed path: hypothesis.internal.compat -> django.test -> ... -> - # django.utils.html -> html.parser; see issue #335 with hypothesis in the - # pytest plugins list). Then the pre-condition is already false. - if html_name in sys.modules: - pytest.skip("%s already imported" % html_name) + assert html_name not in sys.modules mod = six.moves.html_parser assert sys.modules[html_name] is mod assert "htmlparser" not in six._MovedItems.__dict__ diff --git a/tox.ini b/tox.ini index f9afc6b2..3290cf86 100644 --- a/tox.ini +++ b/tox.ini @@ -3,6 +3,8 @@ envlist=py{27,36,37,38,39,310,311,312,313,314,py},flake8 [testenv] deps= pytest +setenv = + PYTEST_DISABLE_PLUGIN_AUTOLOAD=1 commands= python -m pytest -rfsxX {posargs} [testenv:flake8]