Added mypy enable_error_code sp check guideline#4891
Added mypy enable_error_code sp check guideline#4891Rishab87 wants to merge 10 commits intopybamm-team:mainfrom
enable_error_code sp check guideline#4891Conversation
Saransh-cpp
left a comment
There was a problem hiding this comment.
Thanks, @Rishab87! Could you please merge develop and fix the tests?
…enable-error-code-1
|
@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 |
Codecov ReportAll modified and coverable lines are covered by tests ✅
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. 🚀 New features to boost your workflow:
|
Saransh-cpp
left a comment
There was a problem hiding this comment.
Thanks, @Rishab87! The tests pass now. Could you please comment why each of the change was carried out?
| assert ( | ||
| sol1._variables[k] == sol2._variables[k] for k in sol1._variables.keys() | ||
| ) | ||
| ) is not None |
There was a problem hiding this comment.
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()
)There was a problem hiding this comment.
aahh sorry I missed that, changed it now
|
|
||
|
|
||
| def D_s_n(sto, T): | ||
| def D_s_n_func(sto, T): |
There was a problem hiding this comment.
because their was a variable in this file with name D_s_n so mypy gave error function and variable both have same name
There was a problem hiding this comment.
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).
| 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: |
There was a problem hiding this comment.
Could you please add some information on why this was added?
There was a problem hiding this comment.
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
|
@Saransh-cpp Thanks for the review, I've replied to the comments and changed the test |
Saransh-cpp
left a comment
There was a problem hiding this comment.
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 -
|
|
||
|
|
||
| def D_s_n(sto, T): | ||
| def D_s_n_func(sto, T): |
There was a problem hiding this comment.
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).
| a * b, | ||
| a / b, | ||
| a**b, | ||
| b % a, |
There was a problem hiding this comment.
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
| if not isinstance(keys, (str, list)) or not all( # type: ignore[redundant-expr] | ||
| isinstance(k, str) for k in keys |
There was a problem hiding this comment.
Can we simplify this expression instead of ignoring the warning?
There was a problem hiding this comment.
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.
src/pybamm/models/base_model.py
Outdated
| self.use_jacobian = True | ||
| self.convert_to_format = "casadi" | ||
|
|
||
| self.calculate_sensitivities = [] |
There was a problem hiding this comment.
Where is this variable coming from?
There was a problem hiding this comment.
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
src/pybamm/solvers/base_solver.py
Outdated
|
|
||
| ninputs = len(model.calculate_sensitivities) | ||
| initial_conditions = tuple([] for _ in range(ninputs)) | ||
| initial_conditions: tuple = tuple([] for _ in range(ninputs)) |
There was a problem hiding this comment.
Why do we need the explicit tuple?
There was a problem hiding this comment.
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
|
@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. |
Co-authored-by: Saransh Chopra <saransh0701@gmail.com>
Co-authored-by: Saransh Chopra <saransh0701@gmail.com>
…ble-error-code-1
…enable-error-code-1
|
@Saransh-cpp , I've made the changes and replied to the comments, can you have a look? |
Description
Added mypy
enable_error_codesp check guidelineRelated 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:
nox -s pre-commitnox -s testsnox -s doctests