#Let's talk about pre-commit... again?
1 messages · Page 1 of 1 (latest)
I'm struggling to get it to work properly in my env
oO
Intersting
For me it was the same without that and adding it fixed the problem 😄
What problems is it causing for you?
well, now that i'm looking
it's not a pre-commit issue
make lint gives me a ton of errors
it runs this
.PHONY: type-check
type-check: mypy pyright ## Run all type checking
is see the problem
it doesn't ahve the env prefix
so it's not using the mypy installed in the venv
which is my issue
it's using the system 3.11 interpreter for mypy and pyright
I was incorrectly thinking this was pre-commit related
well, pyright has the env check
actually all of them do
.PHONY: mypy
mypy: ## Run mypy
@echo "=> Running mypy"
@$(ENV_PREFIX)dmypy run
@echo "=> mypy complete"
.PHONY: mypy-nocache
mypy-nocache: ## Run Mypy without cache
@echo "=> Running mypy without a cache"
@$(ENV_PREFIX)dmypy run -- --cache-dir=/dev/null
@echo "=> mypy complete"
.PHONY: pyright
pyright: ## Run pyright
@echo "=> Running pyright"
@$(ENV_PREFIX)pyright
@echo "=> pyright complete"
here was the tail of the last time i ran make lint
/home/oracle/.pyenv/versions/3.11.5/lib/python3.11/typing.py:3413:23 - error: "tuple" is not defined (reportUndefinedVariable)
/home/oracle/.pyenv/versions/3.11.5/lib/python3.11/typing.py:3413:29 - error: "type" is not defined (reportUndefinedVariable)
/home/oracle/.pyenv/versions/3.11.5/lib/python3.11/typing.py:3415:6 - error: Variable not allowed in type expression (reportGeneralTypeIssues)
13699 errors, 73 warnings, 0 informations
=> pyright complete
Huh
I always do pyenv local 3.8.x -> create venv -> install deps -> activate venv -> pre-commit install
That usually works
Idk, I'm not a huge fan of the current workflow tbh :/
Still better than the poetry dependency issues tho 👀
yeah, i agree.
ok, this is probably just user error
make install was re-installing with 3.11
no, that wasn't it either
/home/oracle/.pyenv/versions/3.8.18/lib/python3.8/typing.py:1971:6 - error: "property" is not defined (reportUndefinedVariable)
/home/oracle/.pyenv/versions/3.8.18/lib/python3.8/typing.py:1973:33 - error: "bool" is not defined (reportUndefinedVariable)
/home/oracle/.pyenv/versions/3.8.18/lib/python3.8/typing.py:1976:6 - error: "property" is not defined (reportUndefinedVariable)
/home/oracle/.pyenv/versions/3.8.18/lib/python3.8/typing.py:1978:27 - error: Variable not allowed in type expression (reportGeneralTypeIssues)
14228 errors, 65 warnings, 0 informations
=> pyright complete
Was this the thread that led to https://github.com/litestar-org/litestar/pull/2705 ?
hopefully I'm not hijacking
make lint only runs two pre-commit checks now
also occurring in CI: https://github.com/litestar-org/litestar/actions/runs/6916362683/job/18816353425#step:6:29
pretty sure its from adding the pre-push default stage b/c when I try to explicitly run the ruff hook I get: "No hook with id ruff in stage pre-commit"
Are the changes in litestar#2705 making it harder or easier for non .venv venvs to utilize the makefile? I almost never use .venv for litestar projects because the way we have everthing set up. just wondering
Easier.
Why though? The hook is still there..? Shouldn't it just run? :/
found it - there's a --hook-stage flag which must default to something that doesn't have pre-push in it
--hook-stage {commit-msg,post-checkout,post-commit,post-merge,post-rewrite,pre-commit,pre-merge-commit,pre-push,pre-rebase,prepare-commit-msg,manual}
The stage during which the hook is fired. One of commit-msg, post-checkout, post-commit, post-merge, post-rewrite, pre-commit, pre-merge-commit, pre-push, pre-rebase, prepare-commit-msg, manual
How about we add all the pre commit hooks as both pre-commit and pre-push and then whoever doesn't want to run the hooks on the commiting could use git commit -m "some message" --no-verify?
NOTE: I'm biased towards keeping them in the pre-commit stage itself because I rely on them in my workflow 😛
I didn't understand this. The docs you posted show pre-push as one of the options.
it's one of the options but my guess is that if you don't provide any value it is prob the same as --hook-stage pre-commit... i've already spent more time on this than I want to though so 🤷
Yes it is.
There's a value you can set in .pre-commit-config.yaml that will automatically install the different hooks.
Hold on. I think I have it in one of my config files.
The docs.
this isn't really a matter of installing the hooks though
And then we can just set the default_stages as pre-commit and pre-push.
the issue was that pre-commit run --all-files was only running a couple of the hooks
Oh oops you're right.
Then maybe we need to take advantage of pre-commit run --hook-stage pre-commit?
Or rather, if it's only going to have pre-push, then it would need to be pre-comm it run --hook-stage pre-push.
I think this requires us to set the stage of each of the hooks as pre-push as well.
i think that's what @marsh glade's PR did - but I backed it out (obviously too hastily)
I don't think that PR set pre-push as an included stage for each hook though.
The default is almost always pre-commit and I believe that's the default for ruff and others.
I'm sure you are right - so what does default_stages do (also, telling me to RTFM is a valid response here :D)?
It tells pre-commit on which stages to run. So, I think we have a hook for the conventional commit. The stage for this is commit-msg.
But this only applies to hooks which have not set any value for stages.
I'm assuming some mismatch here is why some of the hooks weren't running in the CI.
default_stages
(optional: default (all stages)) a configuration-wide default for the stages property of hooks. This will only override individual hooks that do not set stages.
stages
(optional) selects which git hook(s) to run for. See Confining hooks to run at certain stages.
a comment in the code reads:
# manual is not invoked by any installed git hook. See #719
so maybe we just needed to set default_stages: [pre-push, manual]?
ahhh nope
pre_commit/main.py lines 76 to 82
parser.add_argument(
'--hook-stage',
choices=clientlib.STAGES,
type=clientlib.transform_stage,
default='pre-commit',
help='The stage during which the hook is fired. One of %(choices)s',
)
this is the --hook-stage argument for pre-commit run - and default is pre-commit stage
so if we add default_stages: [pre-push] then we need to change the run commands that we use in places to $ pre-commit run --all-files --hook-stage=pre-push
actually: $ pre-commit run --all-files --hook-stage=pre-push --hook-stage=pre-commit because there are a couple of hooks that still run on the pre-commit stage even after setting pre-push as default
I'm happy to revert my revert for adding this, but I'll leave it as is unless you guys want me to make the above changes too
This may be a sacrilegious question but.. Are we sure we actually need pre-commit..?
Couldn't we just have a make lint and make type-check command and that's it?
Sounds like it could save us a lot of trouble
Regarding the workflow, I don't know about yours, but I am like @polar wind in that I don't actually run pre-commit on every commit; I just run it when I push
It sounds like you guys should stop hurting my and guacs beautiful pre-commit because we actually do use it pre-commit 🥹
Using tools the way they are intended?!
😄
That doesn't sound like a reasonable request at all
Seriously though, that's why I made that suggestion. I think before we start forcing pre-commit to do something it wasn't meant to do, it's better to just use something else
There has to be a way around this though
I dont quite understand what the issue is so far, im trying to grok
Basically @marsh glade changed pre-commit so it would run not pre-commit, but pre-push
Which some people liked
😛
this sounds like for people that like that, they shouldnt need/use pre-commit
But as it turns out, that breaks our CI
just use the makefile 😄
oh 😄
We do have pre-commit however
And maintaining two sets of linting/type-checking scripts sounds meh
😄
So, pre-commit is actually cooperating with me it's the second section of make lint that's causing grief i think
pyright doesn't seem to use the correct python no matter what
Couldn't we just, instead of using pre-commit install, have a script that installs a git hook, which runs pre-commit run on pre-push?
And then if you don't want your pre-commit to run pre-commit, you just use that
yeah, my main gripe with pre-commit install is that it forces me to make my commits meet every standard
when it should only be when it's pushed back to the main repo where that should matter
Well it makes sense if you're not using the squash workflow
Then you want your individual commits to adhere to all the rules
But since we don't, there's really no need for it
since you both dont use pre-commit can we make it good again
we don't use "pre-commit install"
not we don't use precommit
and i'd argue we'd make it worse again 😉
(clearly somethng needs to change though)
if you dont install the hooks whats the point i dont get it? wouldnt that just be the same as make lint
yeah, but that's the thing
make lint doesn't work for me
which is the reason for this post
@cofin I backed this change out because it caused us to only run a couple of the hooks in CI.
It feels like the rules should only be applied when you are pushing something back into the main repo.
Agree - this is what I do but have manually managed the process by not installing the hook at all and running make lint once when I'm ready.
Just to experiment I created this as litestar/.git/hooks/pre-push and it seems to work:
#!/bin/sh
pdm run --venv 3.8 make pre-commit
...although I get warnings like:
Inside an active virtualenv /home/peter/.local/share/pdm/venvs/litestar-dj-FOhMr-3.8, reusing it.
Set env var PDM_IGNORE_ACTIVE_VENV to ignore it.
in the output - so maybe specifying the venv isn't necessary.
I find its still useful as a framework to opt into using a lot of different linting tools in a consistent way
we don't use pre-commit in our internal projects - I just have a shell script that runs a bunch of tools by calling $ ./scripts/tests - but we don't run anywhere near the number of tools that litestar does
when you run $ make lint do you do it from an activated venv shell, or outside?
I've never had a problem with it, so trying to understand the difference in our workflows
It's usually after, but because it runs with the PDM command, it shouldn't matter
do you have a .pdm-python file?
yeah, it points the venv in ./.venv
and is that 3.8?
yep
then I'm out of ideas
when you run $ make lint, what is the PDM output at the top? E.g., mine says: Inside an active virtualenv /home/peter/.local/share/pdm/venvs/litestar-dj-FOhMr-3.8, reusing it.
/home/oracle/.pyenv/versions/3.8.18/lib/python3.8/typing.py:1968:25 - error: Variable not allowed in type expression (reportGeneralTypeIssues)
/home/oracle/.pyenv/versions/3.8.18/lib/python3.8/typing.py:1968:34 - error: "str" is not defined (reportUndefinedVariable)
/home/oracle/.pyenv/versions/3.8.18/lib/python3.8/typing.py:1971:6 - error: "property" is not defined (reportUndefinedVariable)
/home/oracle/.pyenv/versions/3.8.18/lib/python3.8/typing.py:1973:33 - error: "bool" is not defined (reportUndefinedVariable)
/home/oracle/.pyenv/versions/3.8.18/lib/python3.8/typing.py:1976:6 - error: "property" is not defined (reportUndefinedVariable)
/home/oracle/.pyenv/versions/3.8.18/lib/python3.8/typing.py:1978:27 - error: Variable not allowed in type expression (reportGeneralTypeIssues)
14317 errors, 65 warnings, 0 informations
=> pyright complete
that's the end of make lint there's a huge amount of errors above that
❯ make lint
=> Running pre-commit process
fix end of files.........................................................Passed
trim trailing whitespace.................................................Passed
=> Pre-commit complete
=> Running mypy
Success: no issues found in 652 source files
=> mypy complete
=> Running pyright
then there will be a bunch of errors after this
str is not defined 😲
(litestar-3.8) peter@pop-os:~/PycharmProjects/litestar$ make lint
=> Running pre-commit process
Inside an active virtualenv /home/peter/.local/share/pdm/venvs/litestar-dj-FOhMr-3.8, reusing it.
Set env var PDM_IGNORE_ACTIVE_VENV to ignore it.
this is no the correct venv though: /home/oracle/.pyenv/versions/3.8.18/lib/python3.8/typing.py
well, i guess it's the base of the venv
seems like its using pyenvs python instead of the venv python
and why is it type checking the python sauce?
if you first run $ $(pdm venv activate) that activates the venv shell right?
❯ pdm venv activate
source /home/oracle/Code/Starlite/litestar/.venv/bin/activate
❯ which python3
~/Code/Starlite/litestar/.venv/bin/python3
Looks the same to me
❯ $(pdm venv activate)
❯ which python3
~/Code/Starlite/litestar/.venv/bin/python3
this is the correct python though
if i run make lint after that, i get the errors you see above
yes, and I'm not sure why that is yet, but without $(...) you aren't executing anything, pdm venv activate just echo's the text that you see after it in the first example
any other tooling that might be modifying your path to point at the local venv?
yeah, it has to be something like this
pyenv and.python-version or anything like that?
He has his reasons somewhere..
https://github.com/pdm-project/pdm/issues/2249#issuecomment-1715719491 for example, but there are more
but yeah.. it kinda sucks but i have some aliases and scripts that help me
alias av "source .venv/bin/activate.fish" # from inside a dir and venv deactivated
and my auto one https://github.com/JacobCoffee/dotfiles/blob/main/.config/fish/functions/vswp.fish which basically does
eval (pdm venv activate)
but can take a discriminator like a venv name since we have a few
what if you just run pdm run pre-commit run --all-files manually?
thats what linting does, do you mean the type-check?
if so what about manually pdm run dmypy run?
sorry, yes. Type check
pre-commit most likely works fine b/c its managing its own environments for each hook
so pdm run dmypy run works?
❯ pdm run dmypy run
Success: no issues found in 652 source files
this makes me thing make isnt running in the right venv
I'm wondering if we should try to simplify the Makefile a bit, and remove all of the venv discovery logic, making it the callers responsibility to ensure they are calling it inside the right environment
i.e., if you don't want to activate a venv, then run pdm run make lint
or if you have a venv active, just run make lint
etc
i actually think this issue is unrelated to this
afaict, the makefile always thinks I don't have a venv because I don't have .venv/bin/activate
so I don't know why it works for me
ok it works for me b/c the lint command doesn't use any of the VENV_EXISTS stuff
why do we need to do USING_PDM in there?
we can remove that extra check. It used to work for PDM or Poetry
ahh i see, makes sense
still, the Makefile assumes that one is using a venv in project, called .venv - but pdm doesn't enforce either of those things
yeah, it assumes you'll install with make install
i'm wondering if you see the same issue if you do make install && make lint
but that also changes your pdm config to use venv.in_project - which is global isn't it?
@if [ "$(USING_PDM)" ]; then $(PDM) config venv.in_project true && python3 -m venv --copies .venv && . $(ENV_PREFIX)/activate && $(ENV_PREFIX)/pip install --quiet -U wheel setuptools cython mypy pip; fi
so this line is modifying user's global pdm config, I think
We can do project-wide config instead
I understand why its there, from a beginner standpoint running make install is going to be a lot easier, and I can see how having some opinion involved in that scenario is good
yeah, that would be better if we could confine it to the project
fyi these packages wheel setuptools cython mypy pip were installed separately because in poetry i had issues with other packages not cythonizing or compiling with mypyc without it. We may no longer need it with the swap to PDM
this is what I have
Home configuration (/Users/jcoffee5/Library/Application Support/pdm/config.toml):
venv.in_project = True
venv.with_pip = True
Project configuration (/Users/jcoffee5/git/public/litestar-org/litestar/pdm.toml):
venv.in_project = False
venv.with_pip = True
I don't like the extra pdm.toml, maybe it can read from pyproject
I don't know if we want to set that in pyproject though b/c it would apply to all users then
huh
if make install could generate a pdm.toml that sets venv.in_project = True inside it, that would be better than changing the global config
we can do that. And then we can add pdm.toml to the ignore file
it's actually already in .gitignore
we just need to add --local to the config command, I think
oh easy
so back to your issues
when you run at the command line pdm run dmypy run it works
yep
but when you run make mypy - which runs pdm run dmypy run it doesn't
pdm run pyright is the only one that fails
correct. yep
and at the command line, pdm run pyright fails the same way?
sweet - that's an easy change then - anyone who already has a workflow with PDM probably won't try to use make install anyway
what does which pyright output?
❯ which pyright
~/Code/Starlite/litestar/.venv/bin/pyright
ok, we have some progress
i have a feeling it's related to caching. Maybe pdm caching
=> Running pyright
WARNING: there is a new pyright version available (v1.1.335 -> v1.1.336).
Please install the new version or set PYRIGHT_PYTHON_FORCE_VERSION to `latest`
/home/oracle/Code/Starlite/litestar/litestar/channels/backends/redis.py
/home/oracle/Code/Starlite/litestar/litestar/channels/backends/redis.py:7:12 - error: Import "importlib_resources" could not be resolved (reportMissingImports)
/home/oracle/Code/Starlite/litestar/litestar/utils/helpers.py
/home/oracle/Code/Starlite/litestar/litestar/utils/helpers.py:101:14 - error: Import "exceptiongroup" could not be resolved (reportMissingImports)
2 errors, 0 warnings, 0 informations
=> pyright complete
I got this after adding the --local and re-running make install && make lint
that's definitely an improvement!
those missing imports are interesting
.venv is definitely 3.8?
pyproject.toml line 37
"importlib-resources>=5.12.0; python_version < \"3.9\"",
no. It's 3.11
wtf
ok, i think i have some global pdm setting that's overriding
ok, i'm going to take a break on this one. I can work around it until i figure out exactly what's happening
if you run pyenv shell 3.8.18 before make install does it help?
anyway that would be my only suggestion for that
glad we got somewhere!
yeah, this gives me a few additional things to try
this did it
so, the --local + running the above
oh great!
You should be able to create a 3.8 named venv and do that pdm venv activate if that’s the case
nah, i had 3.11 set to global
setting the local fixed it
at least i think that's what happened
you can do pyenv local 3.8.18 so that you don't have to run pyenv shell ... every time
it puts the .python-version file in your directory
yeah, I see that creates the .python-version file
if you don’t want to do that each time or have non 3.8 venvs you could do the pdm venv with the specifier to select each instead of setting local for whatever version you need
this is what I do - I've got 2 litestar venvs, one for 3.8 and one for 3.12
but, I don't think i need to do it again now that i have the Pyenv python version file in my folder
I got tired of running rtx shell [email protected]
And docs require 3.11/12 I think
at some point you'll need a venv > 3.8 if you want to build the docs
today you are too fast
Someone should make a Python packaging tool to manage all these well
Like a.. pdm/hatch/poetry or something
I hate Python packaging 😦
this environment management business is a mess
there are workflows that work fine (for the most part)
but I wouldn't want to be a beginner trying to figure this stuff out
I see what poetry and pdm mean on the environment stuff but it’s not easy and pdm feels especially bad at it comparatively