repair pre-commit hooks to use project venv instead of pre-commit hook's venv#10935
repair pre-commit hooks to use project venv instead of pre-commit hook's venv#10935MarcBresson wants to merge 5 commits into
Conversation
There was a problem hiding this comment.
Hey - I've found 1 issue, and left some high level feedback:
- The check for
os.environ.get("PRE_COMMIT") == "1"is quite strict; consider treating any non-emptyPRE_COMMITvalue as enabled to be more robust against variations in how the variable is set. - In the new test, you patch
subprocess.check_outputdirectly; ifenv_managerimportscheck_outputdifferently (e.g.from subprocess import check_output), this patch may not be effective, so it would be safer to patch the symbol on theenv_managermodule instead.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- The check for `os.environ.get("PRE_COMMIT") == "1"` is quite strict; consider treating any non-empty `PRE_COMMIT` value as enabled to be more robust against variations in how the variable is set.
- In the new test, you patch `subprocess.check_output` directly; if `env_manager` imports `check_output` differently (e.g. `from subprocess import check_output`), this patch may not be effective, so it would be safer to patch the symbol on the `env_manager` module instead.
## Individual Comments
### Comment 1
<location path="tests/utils/env/test_env_manager.py" line_range="637-628" />
<code_context>
+ in_project_venv_dir: Path,
+ mocker: MockerFixture,
+) -> None:
+ mocker.patch.dict(
+ os.environ,
+ {"PRE_COMMIT": "1", "VIRTUAL_ENV": "/environment/prefix"},
+ clear=False,
+ )
+ mocker.patch(
+ "subprocess.check_output",
+ side_effect=check_output_wrapper(),
+ )
+
+ env = manager.get()
+
+ assert env.path == in_project_venv_dir
+ assert env.base == Path(sys.base_prefix)
+
+
</code_context>
<issue_to_address>
**nitpick (testing):** Make the test assertion more explicit that the external VIRTUAL_ENV is ignored.
Since `VIRTUAL_ENV` is set to `/environment/prefix`, the test currently only *implicitly* checks that this value is ignored via `assert env.path == in_project_venv_dir`. To make that intent explicit and protect against regressions, please also assert that the external prefix is not used, e.g. `assert str(env.path) != "/environment/prefix"`. This will make the failure mode clearer if the external virtualenv ever becomes preferred again.
</issue_to_address>Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
|
This doesn't look right. Eg not everyone uses in-project venv. Isn't |
I see If my solution of forcing the project's virtual env is not the right one, maybe we could find a better one. For instance, when I have updated the MR to the new proposed solution, let me know what you think |
… forcing a virtual env to be used
|
a more deterministic proposal is to have a new option (not necessarily documented) similar to uv -- EDIT -- |
Why? Surely simpler just to use pre-commit's supported mechanism to not-use-a-precommit-env, rather than allowing pre-commit to create that unwanted environment and then teaching poetry to ignore it. |
|
Indeed For reference, this is the official documentation on the option and the rationale behind it
I can change the MR to just use the |
Pull Request Check List
Resolves: #10934 and #9393
I tested the hook locally onto my project in which I raised #10934, and it worked as expected.
There is no need to update the documentation :