Skip to content
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

Add the ruff linter to the pre-commit check #214

Merged
merged 5 commits into from
Mar 13, 2024
Merged

Conversation

SamRWest
Copy link
Collaborator

@SamRWest SamRWest commented Mar 12, 2024

I've been finding the ruff linter really useful in some other projects, so thought I'd see if you guys were interested in adding it here.

My motivation was mainly to get the imports formatting standardised (which black doesn't do) for easier diffing/merging.
But it's got a bunch of other really useful checks, especially when working with pandas (though I've turned these off for now so as not to confuse the PR diff).

It's also found and fixed a bunch of style/safety things and I've flagged a couple of logic errors to be fixed also.

What do you guys think?

"NPY", # numpy style
"PL", # pylint
# "PD", # pandas style # TODO enable later
# "C90", # code complexity # TODO enable later
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

these last two will require a reasonable number of (simple) changes, so I've left them off for now so this diff isn't too polluted.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice idea, thanks. Looking forward to the next linting PR. :)

_count_comm_group_vectorised,
_match_wildcards,
_process_comm_groups_vectorised,
commodity_map,
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

^ This was the original motivation - standard import sorting/formatting

@@ -124,8 +123,8 @@ def parse_parameter_values_from_file(


def save_data_with_headers(
param_data_dict: Dict[str, Union[pd.DataFrame, List[str]]],
headers_data: Dict[str, List[str]],
param_data_dict: dict[str, pd.DataFrame | list[str]],
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What python versions are you planning to support?
This was done with the py11 setting (to match CI), but I'll revert and re-run for py39 if that's the minimum target. (I think the | type hint only works from 3.10 onwards)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We haven't had that discussion actually... How about py12?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Works for me, type hints are a lot cleaner in 3.12 for instance.
I just thought someone mentioned a use-case for running with an older version is all.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we use the match-case statement somewhere in transforms.py which is >= 3.11. I feel perhaps we should try to keep the min version as low as possible, just to make it easier for users? Not sure how much of a pain it is to install a specific Python version in Windows (in linux I use pyenv)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd say it is easy! :-) But I am also fine with sticking to 3.11.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I use pyenv on windows also, works very well.
Less savvy users can just download and run the official installers etc.
Ok, so we'll target >=3.11, cool.

@@ -65,6 +65,7 @@ def run_gams_gdxdiff(
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
text=True,
check=False,
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@olejandro
Copy link
Member

I generally like linters, but I don't have that much experience using them with python. I've used ESLint with javascript.

@SamRWest SamRWest marked this pull request as ready for review March 12, 2024 03:52
Copy link
Collaborator

@siddharth-krishna siddharth-krishna left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great idea, thanks! I've been thinking of adding a linter for a while, though I haven't used ruff myself, it looks neat (and fast!).

I still have an open branch somewhere to upgrade the version of pyright -- this should also lead to a bunch of pandas-related changes, perhaps ruff will help us fix many of those?

pyproject.toml Outdated Show resolved Hide resolved
"NPY", # numpy style
"PL", # pylint
# "PD", # pandas style # TODO enable later
# "C90", # code complexity # TODO enable later
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice idea, thanks. Looking forward to the next linting PR. :)

utils/dd_to_csv.py Show resolved Hide resolved
@@ -124,8 +123,8 @@ def parse_parameter_values_from_file(


def save_data_with_headers(
param_data_dict: Dict[str, Union[pd.DataFrame, List[str]]],
headers_data: Dict[str, List[str]],
param_data_dict: dict[str, pd.DataFrame | list[str]],
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we use the match-case statement somewhere in transforms.py which is >= 3.11. I feel perhaps we should try to keep the min version as low as possible, just to make it easier for users? Not sure how much of a pain it is to install a specific Python version in Windows (in linux I use pyenv)

utils/dd_to_csv.py Outdated Show resolved Hide resolved
xl2times/utils.py Outdated Show resolved Hide resolved
@siddharth-krishna siddharth-krishna changed the title Samw/ruff linting Add the ruff linter to the pre-commit check Mar 12, 2024
fixed loop lint errors
@olejandro
Copy link
Member

olejandro commented Mar 13, 2024

@SamRWest I guess this one can be merged already? Just so we don't have to resolve too many conflicts at a later stage...

@SamRWest
Copy link
Collaborator Author

@SamRWest I guess this one can be merged already? Just so we don't have to resolve too many conflicts at a later stage...

Yep, I'll merge it shortly, then tackle #215

@SamRWest SamRWest merged commit 8acd004 into main Mar 13, 2024
1 check passed
@SamRWest SamRWest deleted the samw/ruff_linting branch March 13, 2024 22:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants