You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
We are using pyenv to manage our virtual environments.
I was unable to get the pre-commit hook working in a way that it could be added to our repositories. It always fails to resolve imports.
This fails in constructing the venv path and concatenates the current dir with the venvPath /home/spe/myproject/~/.pyenv/versions.
Entering a absolute path works, but then I can't commit this to our repos.
Our team does have the envs all in the same place, as that is how pyenv puts them. So it would be great if the relative home dir path would work.
Since the pre-commit plugin runs the CLI command, I tried adding the --pythonpath as args to the pre-commit hook definition (https://pre-commit.com/#hooks-args), but this seems to be ignored or not passed on.
I then switched to a local hook definition based on https://github.com/RobertCraigie/pyright-python/blob/main/.pre-commit-hooks.yaml.
This doesn't work as it uses the python language, which as stated by the maintainer of pre-commit, creates an isolated virtual env for the python execution (https://stackoverflow.com/a/70780205/2230045), always missing the imports.
I therefore switched to the system language, which didn't work either when the hook is executed through the VS Code source control commit dialog, as VS code does not seem to make use of the currently active venv for these executions.
What finally worked:
.pre-commit-config.yaml
repos:
- repo: localhooks:
- id: pyrightname: pyrightdescription: "Python command line wrapper for pyright, a static type checker"entry: bin/lint-pyright.shlanguage: script"types_or": [python, pyi]require_serial: trueadditional_dependencies: []minimum_pre_commit_version: "2.9.2"
bin/lint-pyright.sh
#!/bin/bash
VENV_NAME="myproject"eval"$(pyenv init -)"# initialize pyenv for current shelleval"$(pyenv virtualenv-init -)"# initialize pyenv-virtualenv for current shell
pyenv shell $VENV_NAME
python -m pyright "$@"
requirements-dev.txt
pyright==1.1.359
Maybe this helps someone search for a solution.
But more importantly, maybe something could be improved:
Properly handle the home dir shortcut ~ in venvPath in pyrightconfig.json
Accept pythonpath as args for the pre-commit hook
Provide pythonPath in pyrightconfig.json
Directly accept the venvPath and venv arguments for the pre-commit hook
Provide a more direct way to configure the pre-commit hook to run on a virtual env.
The text was updated successfully, but these errors were encountered:
Thanks for the report and sorry for the delayed response.
Properly handle the home dir shortcut ~ in venvPath in pyrightconfig.json
Provide pythonPath in pyrightconfig.json
These are feature requests for the main pyright package, pyright-python does not handle the pyrightconfig.json file itself. If you'd still like these to be supported I'd recommend asking in the https://github.com/microsoft/pyright repo.
Accept pythonpath as args for the pre-commit hook
Directly accept the venvPath and venv arguments for the pre-commit hook
Provide a more direct way to configure the pre-commit hook to run on a virtual env.
I don't use pre-commit anymore so I'm not interested in supporting this myself but I'd be happy to accept a PR adding these.
We are using pyenv to manage our virtual environments.
I was unable to get the pre-commit hook working in a way that it could be added to our repositories. It always fails to resolve imports.
The docs directly address this here: https://github.com/RobertCraigie/pyright-python?tab=readme-ov-file#pre-commit
There one is instructed to set the
venvPath
andvenv
settings. We do not have a pytoml in our projects so I tried to configure this using thepyrightconfig.json
.This fails in constructing the venv path and concatenates the current dir with the venvPath
/home/spe/myproject/~/.pyenv/versions
.Entering a absolute path works, but then I can't commit this to our repos.
Our team does have the envs all in the same place, as that is how
pyenv
puts them. So it would be great if the relative home dir path would work.When I execute pyright via CLI I can simply provide the
--pythonpath
argument (https://github.com/microsoft/pyright/blob/main/docs/command-line.md),The JSON config does not have this option (https://github.com/microsoft/pyright/blob/main/docs/configuration.md#main-configuration-options).
Since the pre-commit plugin runs the CLI command, I tried adding the
--pythonpath
asargs
to the pre-commit hook definition (https://pre-commit.com/#hooks-args), but this seems to be ignored or not passed on.I then switched to a local hook definition based on https://github.com/RobertCraigie/pyright-python/blob/main/.pre-commit-hooks.yaml.
This doesn't work as it uses the
python
language, which as stated by the maintainer of pre-commit, creates an isolated virtual env for the python execution (https://stackoverflow.com/a/70780205/2230045), always missing the imports.I therefore switched to the
system
language, which didn't work either when the hook is executed through the VS Code source control commit dialog, as VS code does not seem to make use of the currently active venv for these executions.What finally worked:
.pre-commit-config.yaml
bin/lint-pyright.sh
requirements-dev.txt
Maybe this helps someone search for a solution.
But more importantly, maybe something could be improved:
~
invenvPath
inpyrightconfig.json
pythonpath
asargs
for the pre-commit hookpythonPath
inpyrightconfig.json
venvPath
andvenv
arguments for the pre-commit hookThe text was updated successfully, but these errors were encountered: