-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Make calling ruff programmatically slightly faster #3694
Comments
If helpful in the interim: Ruff can take code via stdin:
|
(But there's no way to provide configuration programmatically. You could write to a tempfile and pass it in as |
Thanks for the pointer on
yup, that's what I'm doing now. BTW, it's slightly surprising that |
Haha it's hidden! We deprecated it but it's still possible :D |
ha, thanks for the pointer about stdin, that's working well (seems with stdin, you have to manually define the config file). It would amazing if config could be set via an (albeit) very ugly command line flag, perhaps base64 encoded to avoid newline weirdnesses. My solution still involves writing the config on every test which is slowing things down. |
Idk if this sounds crazy or not, but if ruff were to support file-level configuration comments akin to the noqa ones (but also for selection) i.e. With the extra benefit that the new behavior would be cross functional for things outside this usecase. |
I would refrain from adding new features to ruff only to make programmatically calling ruff via the CLI slightly faster. The proper solution in my view is to add a python API to lint a file given its content as a string and the settings |
I would think file-level configuration comments (which enable both selecting and ignoring) would be a generally useful feature. given that you can already ...and it just so happens that it would enable you to avoid writing files (even if it's somewhat of a hack for you). Although i absolutely agree, an actual programmatic API would make more sense trying to address this specific problem. |
I'm sorry. My intention wasn't to say that this feature isn't useful or that I consider it a hack. The only thing I wanted to convey is that I don't recommend any new features with no practical use other than speeding up ruff's programmatic CLI invocation. |
That's exactly what I'm trying to do, but since Ruff doesn't build with pyo3 whatsoever, adding a formal python interface is a massive change. Hence allowing full functionality via subprocess without touching the filesystem might be the best solution IMHO. Also @charliermarsh, I've run into a problem using stdin - when using Surely the formatted python is going to |
Hmm yeah, perhaps they should be included as If you're looking to parse the diagnostics, you could also consider using |
Currently I'm just using a regex to fix the file name and line number, when printing. Would be great if stderr could get the diagnostics in this case. |
For anyone else running into this problem, my fix is to run ruff again without |
See #5102 for another use case where an option to pass the configuration (they propose to pass the config as JSON string) would enable more advanced workflows. |
It probably doesn't satisfy all your needs and we don't make any stability guarantees but an alternative is to run ruff's wasm version. I don't know how fast https://github.com/wasmerio/wasmer-python is... could as well be slower than spawning a new process |
This would also help when providing pre-commit hooks to others. I'd like to allow others to use prepared hooks to get a consistant linting/formatting without having to add additional config files to their repository. This also applies to CI jobs, but there you could just copy a config file from somewhere else, of course. (Thanks for this great tool!) |
@charliermarsh any progress on this? This problem get's worse now that we have
🙏 thank you. |
Hey Samuel!
|
I don’t know that the LSP rewrite will help here — would need to look deeper… Since you’ll still have subprocess overhead when calling from Python. |
I think ruff-lsp might work, creating one subprocess is absolutely fine if we can use it for all tests. The problem in our case is creating one process (two now we're using Timing examples:
Those timings roughly match my intuition that launching a rust process takes ~10ms. |
We do support formatting of docstring code snippets now :) perhaps what you really want is linter support for code snippets without extracting them yourself. |
ye, quite possibly. A few questions:
Sorry to derail this discussions, just interested in whether we could get rid of the linting inside pytest-examples, it would make it much simpler. cc @davidhewitt |
The latest release provides a way to set arbitrary configuration options via the CLI, and you can pass the file content on via stdin. Could you let me know if this is enough for your use case? Also, @snowsignal is working on our LSP rewrite in rust. Stay tuned. |
(this is low priority, but maybe interesting)
I using ruff in pytest-examples to lint and fix code examples.
Currently I have to write a
ruff.toml
config file, then write the python file, then call ruff bysubprocess
.I guess if ruff had someway to set all config via cli arguments, then receive the python code via stdin, I could avoid needing to write to files.
The text was updated successfully, but these errors were encountered: