key4hep / EDM4hep

Generic event data model for HEP collider experiments
https://cern.ch/edm4hep
Apache License 2.0
25 stars 36 forks source link

add formatting and linting python in pre-commit #370

Closed m-fila closed 2 months ago

m-fila commented 2 months ago

BEGINRELEASENOTES

ENDRELEASENOTES

Linting and formatting done with ruff which should be compatible with black, flake8 and pylint while having a single configuration file. ruff is already in the stack

CI added as local hook

Selected rules ["F", "E", "W", "PLE", "PLW", "PLC"] from flake8 (pyflakes "F", pycodestyle errors "E" and warnings "W") and pylint (errors "PLE", warnings "PLW" and conventions "PLC") I'd also consider adding some of the D1 checks for docstrings but there seems to be only a few docstrings now :upside_down_face:

The codebase seems to use mixed 2-space and 4-space indent, now the default 4-space is used in formatter

Should I also add cmake targets as in #257?

Closes #364

m-fila commented 2 months ago

No ruff in the LCG releases? :eyes:

tmadlener commented 2 months ago

Should I also add cmake targets as in https://github.com/key4hep/EDM4hep/pull/257?

We either adapt that to run the same checks as this, or we have to include flake8 and pylint in this PR as well. Alternatively, we close that and include the changes here.

m-fila commented 2 months ago

Added cmake targets here so #257 can be closed if this is merged

tmadlener commented 2 months ago

Very nice. Thanks :)

jmcarcell commented 2 months ago

What about including formatting as part of Key4hepConfig.cmake? For python ruff is very fast so it won't change much, clang-format for every file I don't know :thinking:

tmadlener commented 2 months ago

I like the idea. We would probably then also need a pre-commit setup that checks in all repositories. In the end the targets are only defined but not run by default, so it would be a somewhat easy way for users to fix formatting issues that are flagged by pre-commit.

m-fila commented 2 months ago

Hmm pre-commit config is just a single file and if we want to centrally manage it then the projects are not supposed to modify it, right? So if we decide that we don't want for instance clang-tidy in pre-commit everywhere then adding clang-tidy just to selected repos would be a problem? Similar problem with ruff config, ruff format and lint are in the same file so if we want to have managed ruff config with just format then it wont be possible to add lint to it. Also the config then should be probably moved from recommended pyproject.toml to ruff specific otherwise it won't be possible to modify pyproject.toml?

tmadlener commented 2 months ago

I think some of the configuration files simply can't be managed centrally, or at least not for all packages. It's not entirely clear to me where to draw the line, but e.g. pyproject.toml is not something I would manage centrally, since there is a lot of project specific metadata that goes into there.

m-fila commented 2 months ago

So should we centrally manage just the cmake target with ruff format but no linting target, pre-commit config, ruff config? Ruff can run on the defaults without any config file, the only difference between the defaults and the config here is the target-version = "py38" vs target-version = "py310"

tmadlener commented 2 months ago

Maybe python related targets need to go into a separate .cmake file? I am not entirely sure if all the repositories will need it in the end. Additionally, Gaudi option files are not easily lintable, I think, since most of the things are dynamically populated.

I would leave the pre-commit config on a per repository basis at least. (The github action can then again be distributed centrally). For the linting, I am not entirely sure, in principle we should apply the same rules for all repositories. However, that would then mean that we would need to go the way of ruff specific config files, right? Because pyproject.toml is definitely repository specific, IMHO.

tmadlener commented 2 months ago

Should we merge this still before the tag (tomorrow)? Or do we postpone this until later and conclude the discussion first?

m-fila commented 2 months ago

I think there is no hurry for this one I just realized I missed there is a black config in k4FWCore with different line width (99). I think I'll change it here to 99 too for some consistency

andresailer commented 2 months ago

No ruff in the LCG releases? 👀

There will in the next one: https://lcginfo.cern.ch/pkg/ruff/

(Hopefully happening Monday)

tmadlener commented 2 months ago

We could also switch over to use a key4hep stack, since that is what we are doing on other repositories, I think. Using an LCG stack was done historically, because we could more easily get an LLVM based stack (for clang-format), but that is now also available from a key4hep stack