mllam / neural-lam

Neural Weather Prediction for Limited Area Modeling
MIT License
64 stars 24 forks source link

Simplify pre-commit setup #29

Closed leifdenby closed 1 month ago

leifdenby commented 1 month ago

This PR simplifies the pre-commit setup by:

I also think we should consider using ruff instead of flake8/pylint, here's an article on why: https://pythonspeed.com/articles/pylint-flake8-ruff/. xarray use it so this must think this is ready for primetime.

leifdenby commented 1 month ago

I am a big fan uf Ruff and use it everyday when working with this repo. +++ should replace flake8, pylint and isort in a future PR.

Ok great, yes let's do that in a later PR.

leifdenby commented 1 month ago

@sadamov as I thought somehow linting with same versions of tools but different version of python leads to different results :laughing:

Why the current action appears frozen Cleaning up orphan processes

I think this must be something with the pre-commit action. I don't think it is something I can fix unfortunately. Unless you or @khintz can see an issue with my config?

leifdenby commented 1 month ago

Why the current action appears frozen Cleaning up orphan processes

I think this must be something with the pre-commit action. I don't think it is something I can fix unfortunately. Unless you or @khintz can see an issue with my config?

I just re-ran this test and it didn't hang. I think the github action runner must have borked somehow. The log is the same though, so maybe it was just the log sending that borked.

So, @sadamov and @khintz we just have to decide about the different linting rules that seem to apply when you change python version (even though the flake8 version is the same) :rofl:

khintz commented 1 month ago

A very quick test could be to try a newer flake8 version?

Eg. using

- repo: https://github.com/PyCQA/flake8
   rev: 7.0.0

Instead of rev: 6.1.0

leifdenby commented 1 month ago

A very quick test could be to try a newer flake8 version?

Eg. using

- repo: https://github.com/PyCQA/flake8
   rev: 7.0.0

Instead of rev: 6.1.0

Yes, of course! Good idea. I'll try that now

leifdenby commented 1 month ago

hmm, even with the most recent version of flake8 the linting when using python3.12 is different :disappointed:

sadamov commented 1 month ago

Oh that is quite annoying. I am also fine with pinning a python version for static analysis, as you describe above (e.g. 3.11). Or if we think Ruff wouldn't suffer from python-version specific issues, we could also go there directly. I don't have a strong opinion, please go ahead with your preference :+1:

leifdenby commented 1 month ago

Oh that is quite annoying. I am also fine with pinning a python version for static analysis, as you describe above (e.g. 3.11). Or if we think Ruff wouldn't suffer from python-version specific issues, we could also go there directly. I don't have a strong opinion, please go ahead with your preference 👍

Ok, I'll give ruff a try tomorrow and see if that works the same way across different python versions (which it should since it is actually written in rust). Otherwise I say we stick with python < 3.12 for now for linting. At least we have this issue in case someone comes across issues when doing local development with python 3.12

leifdenby commented 1 month ago

On second thoughts @sadamov I think we should stick with flake8, pylint etc for now and replace with ruff. Otherwise this PR will make too many changes. Are you ok with me simply adding a note to the README section on "developing" to mention that there might be an issue when running linting with python3.12 and link to this PR? That way we can keep a reference this for future work.

khintz commented 1 month ago

Are we sure that the problem is using flake with python3.12 and not a problem of flake not detecting operations inside f-strings for python<3.12 ?

The small changes below works for python3.12 and python3.9

neural_lam/models/ar_model.py:403:53: E226 missing whitespace around arithmetic operator Changing from

f"t={t_i} ({self.step_length*t_i} h)",

to

f"t={t_i} ({self.step_length * t_i} h)",

neural_lam/models/ar_model.py:545:66: E226 missing whitespace around arithmetic operator Changing from

title=f"Test loss, t={t_i} ({self.step_length*t_i} h)",

to

title=f"Test loss, t={t_i} ({self.step_length * t_i} h)",

neural_lam/models/base_hi_graph_model.py:39:55: E226 missing whitespace around arithmetic operator Changing from

title=f"Test loss, t={t_i} ({self.step_length*t_i} h)",

to

title=f"Test loss, t={t_i} ({self.step_length * t_i} h)",
sadamov commented 1 month ago

Good catch @khintz, there is one more operator here print(f" {level_index}<->{level_index + 1}") (in base_hi_graph_model.py) While PEP8 gives some freedom to the user when it comes to whitespaces around operators. https://peps.python.org/pep-0008/#other-recommendations I agree that in the four cases listed above we should simply add the whitespaces in this PR.

leifdenby commented 1 month ago

Are we sure that the problem is using flake with python3.12 and not a problem of flake not detecting operations inside f-strings for python<3.12 ?

That would be a bug in flake with python<3.12 no? :)

Ok, so I think what we're suggesting here is: 1) include python3.12 in cicd linting setup and 2) fix this linting issue as you mention above @khintz (which is ignored by flake for python<3.12). And then in future if linting in python3.12 with flake fails in cicd with expressions in fstrings then we know why :)

Sound ok @khintz and @sadamov?

khintz commented 1 month ago

That would be a bug in flake with python<3.12 no? :)

That would be my assumption until I am proved wrong 😄

Your suggestion sounds good to me.

leifdenby commented 1 month ago

thanks for your help @sadamov and @khintz, could you give this a last glance-over please and see if you're happy to accept the changes?

leifdenby commented 1 month ago

thank you both!

joeloskarsson commented 1 month ago

Pylint is indeed very slow. If ruff is working well and is fast I think it sounds great to switch to that.

Just for my understanding: @leifdenby do I understand correctly that this removes the use of pylint? Does that relate to not checking imports to external packages?