Move Pants stub bootstrap to conftest.py and clean test_rules.py imports#1024
Draft
Copilot wants to merge 3 commits intopants-reqsfrom
Draft
Move Pants stub bootstrap to conftest.py and clean test_rules.py imports#1024Copilot wants to merge 3 commits intopants-reqsfrom
conftest.py and clean test_rules.py imports#1024Copilot wants to merge 3 commits intopants-reqsfrom
Conversation
Signed-off-by: Ryan Northey <ryan@synca.io>
Agent-Logs-Url: https://github.com/phlax/toolshed/sessions/e3d80d33-9045-49ab-b291-a01976622917 Co-authored-by: phlax <454682+phlax@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Fix code smells in test_rules.py
Move Pants stub bootstrap to May 7, 2026
conftest.py and clean test_rules.py imports
9a02b17 to
629bcac
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
py/pants-toolshed/toolshed_publish_reqs/test_rules.pymixed import-time stub bootstrapping with test code, causingF811(pantsredefinition), late imports (E402suppressions), and brittle module setup.This change moves Pants stub installation into sibling
conftest.pyso pytest loads stubs before test module import, allowingtest_rules.pyto remain a normal test module.Stub bootstrap moved to pytest lifecycle
py/pants-toolshed/toolshed_publish_reqs/conftest.py._install_pants_stubs()andtry: import pants / except ModuleNotFoundErrorthere._FakeTargetAdaptor.pants→pants_moduleto avoid name collision withimport pants.test_rules.pysimplified to standard importssys,types,_install_pants_stubs, and module-leveltry/except.# noqa: E402.import pytestfrom toolshed_publish_reqs.names import _publish_req_target_namefrom toolshed_publish_reqs.rules import _adaptors_for_install_requiresBUILD target shape unchanged
conftest.pyis consumed as source alongside tests under existing target layout.Example of the new import flow:
Original prompt
Context
This PR is being raised against the branch
pants-reqsinphlax/toolshed(the head branch of envoyproxy#4188). Current head SHA is7d55701de9caf33c8bd13f7835a5ae235cd2c8a4. Previous review-fixup work was squashed onto that branch.Do not target
main— targetpants-reqs.Problem
py/pants-toolshed/toolshed_publish_reqs/test_rules.pycurrently has serious code smells:F811 redefinition of unused 'pants' from line 56— the localpants = types.ModuleType("pants")inside_install_pants_stubs(line 8) collides with the module-levelimport pants(line 56) that gates the stub installation._publish_req_target_nameand_adaptors_for_install_requireshappen halfway through the file (after thetry/except ModuleNotFoundErrorblock), each requiring# noqa: E402.try: import pants / except ModuleNotFoundError: _install_pants_stubs()block is at module import time in the test file itself.This is fragile and ugly.
Goal
Move the
pants.*stub installation out oftest_rules.pyinto a siblingconftest.py. Pytest auto-importsconftest.pyfiles before any test module in the same directory is collected, so by the timetest_rules.pyis imported,pants.*modules are already available insys.modules. This meanstest_rules.pycan have clean top-of-file imports with zeronoqa, zerosys.modulesmanipulation, and zero F811.Required changes
1. Create
py/pants-toolshed/toolshed_publish_reqs/conftest.pyMove the
_install_pants_stubsfunction and itstry/exceptinvocation here verbatim fromtest_rules.py. Module-level docstring should explain why this exists. Example shape:The
# noqa: F401onimport pantsis acceptable in conftest.py because the import is a pure existence-check (it has no other purpose). It must NOT trigger F811: keep the local variable name inside_install_pants_stubsaspants_module = types.ModuleType("pants")(renaming the local to avoid shadowing the imported name) — apply the same rename insidesys.modules["pants"] = pants_module. The other locals (engine,fs,internals,synthetic_targets,target_adaptor,intrinsics,rules,unions) do not collide with anything imported at module top-level and can stay as-is.The
_FakeTargetAdaptorrecording class must remain inside_install_pants_stubsexactly as today — it is the same class the new rule-helper tests assert against.2. Rewrite
py/pants-toolshed/toolshed_publish_reqs/test_rules.pyAfter the conftest move, this file should look like a normal, boring pytest module: