Skip to content

Added mypy enable_error_code sp check guideline #4891

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 10 commits into
base: develop
Choose a base branch
from

Conversation

Rishab87
Copy link
Contributor

@Rishab87 Rishab87 commented Mar 4, 2025

Description

Added mypy enable_error_code sp check guideline

Related to #3489, #4887

Type of change

Please add a line in the relevant section of CHANGELOG.md to document the change (include PR #)

Important checks:

Please confirm the following before marking the PR as ready for review:

  • No style issues: nox -s pre-commit
  • All tests pass: nox -s tests
  • The documentation builds: nox -s doctests
  • Code is commented for hard-to-understand areas
  • Tests added that prove fix is effective or that feature works

@Rishab87 Rishab87 requested a review from a team as a code owner March 4, 2025 13:08
@Rishab87 Rishab87 mentioned this pull request Mar 4, 2025
5 tasks
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, @Rishab87! Could you please merge develop and fix the tests?

@Rishab87
Copy link
Contributor Author

Rishab87 commented Mar 10, 2025

@Saransh-cpp I've merged the develop branch and all the tests pass locally, its weird that I haven't changed anything related to serialization still it fails, can you try re-running it? From what I'm able to interpret is failure is most likely due to a timing issue with how the filename is generated

Copy link

codecov bot commented Mar 10, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 98.70%. Comparing base (8f91615) to head (81f74bf).
Report is 4 commits behind head on develop.

Additional details and impacted files
@@             Coverage Diff             @@
##           develop    #4891      +/-   ##
===========================================
- Coverage    98.71%   98.70%   -0.01%     
===========================================
  Files          304      304              
  Lines        23509    23540      +31     
===========================================
+ Hits         23207    23236      +29     
- Misses         302      304       +2     

☔ 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.

@Saransh-cpp Saransh-cpp self-requested a review March 11, 2025 14:31
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, @Rishab87! The tests pass now. Could you please comment why each of the change was carried out?

@@ -255,7 +255,7 @@ def test_copy_with_computed_variables(self):

assert (
sol1._variables[k] == sol2._variables[k] for k in sol1._variables.keys()
)
) is not None
Copy link
Member

Choose a reason for hiding this comment

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

Nice! mypy caught a bad test. Your implementation changes the logic of the test and makes it not do anything useful. It should instead be changed into:

assert all(
     sol1._variables[k] == sol2._variables[k] for k in sol1._variables.keys()
 )

Copy link
Contributor Author

Choose a reason for hiding this comment

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

aahh sorry I missed that, changed it now

@@ -34,12 +34,12 @@ def process_2D(name, data):
D_s_n_data = process_2D("Negative particle diffusivity [m2.s-1]", df)


def D_s_n(sto, T):
def D_s_n_func(sto, T):
Copy link
Member

Choose a reason for hiding this comment

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

Why was this changed?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

because their was a variable in this file with name D_s_n so mypy gave error function and variable both have same name

Copy link
Member

Choose a reason for hiding this comment

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

I think the motive behind redefining the variable here was to show how one can pass both a constant value and a function as a parameter value. I would say we should revert this and not run mypy on examples at all (see my comment below).

@@ -114,6 +113,9 @@ def __str__(self):
right_str = f"{self.right!s}"
return f"{left_str} {self.name} {right_str}"

def _new_instance(self, left: pybamm.Symbol, right: pybamm.Symbol) -> pybamm.Symbol:
Copy link
Member

Choose a reason for hiding this comment

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

Could you please add some information on why this was added?

Copy link
Contributor Author

@Rishab87 Rishab87 Mar 11, 2025

Choose a reason for hiding this comment

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

I've replaced self.__ class __ with new_instance because earlier when we were using self.__ class __ it showed a third arg was not getting passed:

error: Missing positional argument "right_child" in call to "BinaryOperator"  [call-arg]

but this function was always getting called from instance of its child classes which don't need to pass 3 arguments, so i thought it was better to make a new_instance method which can be overrided in child classes

I've already added this in the PR description of previous sp-check-guidelines PR, should I add it here too? Or follow some different approach

@Rishab87
Copy link
Contributor Author

@Saransh-cpp Thanks for the review, I've replied to the comments and changed the test

@Rishab87 Rishab87 requested a review from Saransh-cpp March 11, 2025 16:01
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, @Rishab87! I think I wasn't clear before. I wanted you to comment on each of your change that edits the code logic with an explanation of why it is required. See my comments below -

@@ -34,12 +34,12 @@ def process_2D(name, data):
D_s_n_data = process_2D("Negative particle diffusivity [m2.s-1]", df)


def D_s_n(sto, T):
def D_s_n_func(sto, T):
Copy link
Member

Choose a reason for hiding this comment

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

I think the motive behind redefining the variable here was to show how one can pass both a constant value and a function as a parameter value. I would say we should revert this and not run mypy on examples at all (see my comment below).

@@ -79,6 +79,7 @@ def test_symbol_create_copy_new_children(self):
a * b,
a / b,
a**b,
b % a,
Copy link
Member

Choose a reason for hiding this comment

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

Why was this added?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

to ensure coverage, as told in my earlier comment too above:

I've replaced self.__ class __ with new_instance because earlier when we were using self.__ class __ it showed a third arg was not getting passed:
error: Missing positional argument "right_child" in call to "BinaryOperator" [call-arg]
but this function was always getting called from instance of its child classes which don't need to pass 3 arguments, so i thought it was better to make a new_instance method which can be overided in child classes

due to this many new_instance functions have been added for modulo and other operations, we can use pragma no cover too instead of adding this in test

Comment on lines +155 to 156
if not isinstance(keys, (str, list)) or not all( # type: ignore[redundant-expr]
isinstance(k, str) for k in keys
Copy link
Member

Choose a reason for hiding this comment

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

Can we simplify this expression instead of ignoring the warning?

Copy link
Contributor Author

@Rishab87 Rishab87 Apr 9, 2025

Choose a reason for hiding this comment

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

The problem here is that keys type is str | list[str], so we if we remove this ignore, it will throw an error reduntant expression as according to the types, there is no point of checking whether keys list[str] but if we remove this if statement, few test cases fail as in those test cases we're intentionally passing other types of values, so I thought it would be best to ignore it.

@@ -77,6 +77,8 @@ def __init__(self, name="Unnamed model"):
self.use_jacobian = True
self.convert_to_format = "casadi"

self.calculate_sensitivities = []
Copy link
Member

Choose a reason for hiding this comment

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

Where is this variable coming from?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

In base solver got this error:

src/pybamm/solvers/base_solver.py:1124: error: "BaseModel" has no attribute "calculate_sensitivities"  [attr-defined]

that's why I added this

@@ -1122,7 +1122,7 @@ def _set_sens_initial_conditions_from(
"""

ninputs = len(model.calculate_sensitivities)
initial_conditions = tuple([] for _ in range(ninputs))
initial_conditions: tuple = tuple([] for _ in range(ninputs))
Copy link
Member

Choose a reason for hiding this comment

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

Why do we need the explicit tuple?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Because mypy was unable to infer it:

src/pybamm/solvers/base_solver.py:1125: error: Need type annotation for "initial_conditions"  [var-annotated]

I think mypy has difficulty inferring that the tuple contains lists of a particular type

@Rishab87
Copy link
Contributor Author

@Saransh-cpp , thanks for the review and sorry for the late reply, I was busy with my mid sem exams along with my proposal, I will go through the comments and fix these issues.

@Rishab87
Copy link
Contributor Author

Rishab87 commented Apr 9, 2025

@Saransh-cpp , I've made the changes and replied to the comments, can you have a look?

@Rishab87 Rishab87 requested a review from Saransh-cpp April 9, 2025 17:59
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.

2 participants