Skip to content

chore: enable pytest ruff rule and apply fixes #4928

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

Open
wants to merge 6 commits into
base: develop
Choose a base branch
from

Conversation

Aswinr24
Copy link
Contributor

Description

This PR enables additional Ruff linting rules and applies fixes to the codebase as suggested by Ruff. The changes include:

Enabling PT (pytest-style) rules in pyproject.toml.

fixes issues raised by ruff linter which included:

  • PT006: Update @pytest.mark.parametrize to use a tuple for multiple parameters instead of a list or comma-separated strings.
  • PT011: Ensure pytest.raises() is called with a match parameter to avoid catching overly broad exceptions.
  • PT012: Ensure pytest.raises() context managers contains only a single statement.

Related to #4925

Type of change

  • New feature (non-breaking change which adds functionality)
  • Optimization (back-end change that speeds up the code)
  • Bug fix (non-breaking change which fixes an issue)

Key checklist:

  • No style issues: $ pre-commit run (or $ nox -s pre-commit) (see CONTRIBUTING.md for how to set this up to run automatically when committing locally, in just two lines of code)
  • All tests pass: $ python -m pytest (or $ nox -s tests)
  • The documentation builds: $ python -m pytest --doctest-plus src (or $ nox -s doctests)

You can run integration tests, unit tests, and doctests together at once, using $ nox -s quick.

Further checks:

  • Code is commented, particularly in hard-to-understand areas
  • Tests added that prove fix is effective or that feature works

@Aswinr24 Aswinr24 requested a review from a team as a code owner March 23, 2025 14:59
@Aswinr24 Aswinr24 changed the title chore: enable pytest ruff rules and apply fixes chore: enable pytest ruff rule and apply fixes Mar 23, 2025
Copy link
Member

@Saransh-cpp Saransh-cpp left a comment

Choose a reason for hiding this comment

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

Thanks, @Aswinr24! Where did you get the match="string" strings from, as most of them are wrong? Is it from an older version of PyBaMM?

Please see my comments below to fix the tests.

@Saransh-cpp Saransh-cpp marked this pull request as draft March 27, 2025 20:55
@Aswinr24 Aswinr24 requested a review from Saransh-cpp March 29, 2025 13:52
@Aswinr24 Aswinr24 marked this pull request as ready for review March 29, 2025 18:34
Copy link
Member

@Saransh-cpp Saransh-cpp left a comment

Choose a reason for hiding this comment

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

Thanks, @Aswinr24! This looks good now. Could you please resolve the conflicts?

@Aswinr24
Copy link
Contributor Author

Yes thanks!, would resolve the conflicts and push it

@Aswinr24
Copy link
Contributor Author

Was thinking to open the PR to enable isort rule along with its suggested fixes as well
Though it does suggest changes in a lot of files, most of them are at the top of the files which shouldn't cause any issues, Do you suggest to go ahead with this @Saransh-cpp ?

Copy link

codecov bot commented Apr 25, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 98.58%. Comparing base (54f6e21) to head (5dc454b).

Additional details and impacted files
@@           Coverage Diff            @@
##           develop    #4928   +/-   ##
========================================
  Coverage    98.58%   98.58%           
========================================
  Files          304      304           
  Lines        23689    23689           
========================================
  Hits         23353    23353           
  Misses         336      336           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copy link
Member

@Saransh-cpp Saransh-cpp left a comment

Choose a reason for hiding this comment

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

Thanks, @Aswinr24! Please see my comments below -

@@ -689,7 +689,7 @@ def test_failures(self):
solver = pybamm.IDAKLUSolver()

t_eval = [0, 3]
with pytest.raises(ValueError):
with pytest.raises(ValueError, match="std::exception"):
Copy link
Member

Choose a reason for hiding this comment

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

The error raised is different on Windows and UNIX. Can we not have match= here (ignore the rule) and add a comment for future reference -

# raises `std::exception` on UNIX and `IDA failed with flag -22` on Windows
with pytest.raises(ValueError)  # noqa: ...
    ...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure, Will add this comment and ignore the rule here

@@ -894,7 +894,7 @@ def test_solver_options(self):
options = {option: options_fail[option]}
solver = pybamm.IDAKLUSolver(options=options)

with pytest.raises(ValueError):
with pytest.raises(ValueError, match="std::exception"):
Copy link
Member

Choose a reason for hiding this comment

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

Ditto!

@Saransh-cpp
Copy link
Member

Do you suggest to go ahead with this

Yes, sounds good to me.

@Saransh-cpp
Copy link
Member

pre-commit.ci run

Copy link
Member

@Saransh-cpp Saransh-cpp left a comment

Choose a reason for hiding this comment

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

Looks good now, thanks, @Aswinr24!

@Aswinr24
Copy link
Contributor Author

Thanks!

Comment on lines 207 to 209
interp = pybamm.Interpolant(x4_, data4, y4, interpolator="linear")
interp_casadi = interp.to_casadi(y=casadi_y)
interp_casadi = interp.to_casadi(y=casadi_y)

Copy link
Contributor

Choose a reason for hiding this comment

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

This part looks off to me. If the raise value error happens in the pybamm.Interpolant() line then the to_casadi() line does nothing

Copy link
Contributor

Choose a reason for hiding this comment

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

There are a few other places with similar changes

with pytest.raises(pybamm.OptionError, match="natural numbers"):
options = {"number of rc elements": -1}
options = {"number of rc elements": -1}
with pytest.raises(pybamm.OptionError, match="natural numbers"): # noqa: PT012
Copy link
Contributor

Choose a reason for hiding this comment

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

What is being suppressed here?

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