Closed mofojed closed 2 months ago
I think this is a great idea. +1 on black.
https://github.com/charliermarsh/ruff is just a linter, but I believe they are working on adding an autoformatter.
The list of projects using it is very impressive, so I can imagine it might replace black in the near future.
Big fan of anything written in rust! Looks like a neat project, I'll definitely keep an eye on it.
https://github.com/charliermarsh/ruff/issues/4041
I believe we have plans to add a Ruff formatter, but it's a ways off. For now, black is still required.
That's right. For now, we don't autofix line wrapping, and we do recommend using Ruff alongside Black.
So, it looks like the devs of ruff like black too; my guess is if they eventually get a Ruff formatter, it will have a black-compatible mode. I think black is still our best bet today.
Black is probably the best choice today, but yeah that could change in the future https://github.com/charliermarsh/ruff/issues/1904
We use prettier for our stuff, so them combining black plus some basic config options like prettier allows would be great.
https://github.com/jpadilla/black-playground https://black.vercel.app
could be useful starting point?
It could be useful to use bidirection plugins to support this use case.
Ruff has a wasm package (have to build it from src) that can be used to format and lint completely client-side
Not something I will currently continue working on (I have other priorities), but I made a quick POC using ruff as wasm. https://github.com/mattrunyon/web-client-ui/tree/ruff-wasm
I built the wasm from source and just included it as part of the branch. Looked at https://github.com/astral-sh/ruff/blob/main/playground/src/Editor/SourceEditor.tsx for how to call the linter/formatter and map the linter results to actions and markers.
I roughly implemented
Would still need to cleanup the code and add
Some issues on my branch
Potentially related would be catching syntax errors on the web UI; I don't know if the code formatting tools considered would expose the appropriate syntax error hints, but that would also be a great addition.
Two questions we should decide an answer on:
@jnumainville for investigating any additional default rules sets that are commonly enabled
https://docs.astral.sh/ruff/settings/#lintflake8-implicit-str-concat I think we should add this rule by default
t = t.update([
"X=X+1" # missing comma at the end not flagged by other rulesets
"Y=Y+1"
])
a quick survey indicates to me that there isn't much of a consistently chosen ruleset https://github.com/pola-rs/polars/blob/f6b4f48f3f7a981f6d022b5ca8a50b40a467d74c/py-polars/pyproject.toml#L133 https://github.com/jupyter/notebook/blob/a1e25b92bf10ef13a760353837114db9b498f242/pyproject.toml#L243 https://github.com/pandas-dev/pandas/blob/529bcc1e34d18f94c2839813d2dba597235c460d/pyproject.toml#L190 https://github.com/vega/altair/blob/f1b4e2c84da2fba220022c8a285cc8280f824ed8/pyproject.toml#L166 https://github.com/matplotlib/matplotlib/blob/46b39ab5bea6668e56140da568bde60276f6f906/pyproject.toml#L139 https://github.com/scikit-learn/scikit-learn/blob/4449ded95bdc7468f7465a3e89ea1b90b1426dfd/pyproject.toml#L143 https://github.com/pytorch/pytorch/blob/56b271fd7a87e03dc6cb4329a702eb5f5031f3e3/pyproject.toml#L91
We should probably default to no style specific rules and focus only on rules that can show actual problems with the code (like inconsistent tabs/spaces). Or if they are style related, they should be to prevent easy to miss mistakes like the implicit string concatenation. That rule is technically style because it's valid Python.
Another style rule we should consider is warning for unused imports/variables.
Users would be able to add the other rules if they wanted to enforce something like mandatory pep8 naming conventions. We shouldn't enforce those on everyone by default unless we feel that strongly about it.
I've been looking at the ruff rules https://docs.astral.sh/ruff/rules
I think we should enable
if-tuple
which is always truex == None
can cause issues in Python or it's just recommended to use x is None
. If the latter then we maybe leave it off by default since it's just a style choice.TypeError
Maybe enable, but might not be very useful. I need a Python person to provide more insight
Instant
in a table or if it auto adds UTC.Probably should not enable, but individual users might want to use these
Formatter settings we should leave as default I think with the following exceptions
indent-width
should match our monaco width. I think both default to 4quote-style
. Default is double
. We could use preserve
, but probably just leave as double and let users configureThe default is ["E", "F"]
which encompasses all the E and F rules, I would propose we only add rules to those, not remove any.
Many of the E rules outside 1xx/2xx/9xx are more stylistic to me. If @jnumainville wants to chime in I'd like his thoughts on the other E rules.
Also, the default is ["E4", "E7", "E9", "F"]
. I don't think E4
and E7
(4xx and 7xx) are actual problems with code
To address some specific points -
On one hand E731
is certainly one I don't think we'd want considering the usefulness of lambdas in React style code.
On the other hand:
E402
could be a code problem if you've accidentally imported something after the fact.
E721
is one that could be a problem if you're subclassing.
E722
could definitely be a code problem by swallowing up exceptions that you shouldn't.
More broadly though, the E
rules are PEP8 - these are rules that most people are going to be used to and reasonably expect. I don't think it's unreasonable to have those in a basic rule set, even though some are stylistic choices. Or at least E4
and E7
(besides E731
) are reasonable. Seems like E1
, E2
, E3
rules are unstable? E3
are ones I wouldn't argue as being important.
Are we planning to provide an easy way to turn off formatting and turn off linting (likely, two different settings)?
I think @jmao-denver and @chipkent should take a look specifically. I'm hoping that the settings we choose to use for web UI can be adopted by deephaven-core python development, ala https://github.com/deephaven/deephaven-core/issues/3638
Yes they will be toggles either in the notebook settings (like mini-map) or the user settings (like shortcuts).
Linting config will be user customizable. Formatting has a few options as well, but I think it's only indent size and single/double quotes for strings.
For aligning w/ deephaven-core Python development, the web UI might be a subset of the rules you want there. There are some rules that are more preference/style that we might want in deephaven-core, but not necessarily applied to all users of DH
DM me if you want me to dig in beyond the list you have.
@chipkent just to clarify the rules list I proposed was for the web UI to show to users in notebooks/terminal. I think most of what you added would probably not be annoying to users, but pep8-naming
might be something we leave up to users to add if they want it. Or we can enable it by default and users can disable if they don't care about it. Just depends on what we want the default experience to be in the web UI.
We can also set some rules to warning (yellow squiggly underline) and others to error (red squiggly underline). That would be up to us to determine the defaults as ruff does not assign any default levels to rules.
If we're adding linting rules to deephaven-core then we can definitely be as opinionated as we want and should add things like pep8-naming
.
The required imports rule is something you can customize. I think by default it does nothing when on because you have to configure a list of required imports. https://docs.astral.sh/ruff/settings/#lint_isort_required-imports
I see. My bad. Let me reasses the list with that in mind.
DM me if you want me to dig in beyond the list you have.
I002
we don't need. It lets you configure a list of imports that must occur in every file. So you could require every file do from __future__ import typings
or import deephaven.special_sauce
if that were required for every file
I am in agreement with @chipkent's preference. During my discussion with @mattrunyon , I raised the issue of Python version compliance/compatibility check, it seems that that's currently not available with Ruff and the rulesets. But one can argue that it is more important to DH than to the users.
Will add to a feature branch first, starting with this PR #2043
@rbasralian would like to make sure there is an option to disable/enable auto format on save.
As a user, I want automatic code formatting in my notebooks so that my code looks pretty.
I would like to have formatting applied by something like black: https://github.com/psf/black.
Ruff wasm Python formatter and linter: