-
-
Notifications
You must be signed in to change notification settings - Fork 622
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
base: develop
Are you sure you want to change the base?
Conversation
There was a problem hiding this 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?
…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:
|
There was a problem hiding this 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 |
There was a problem hiding this comment.
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()
)
There was a problem hiding this comment.
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): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why was this changed?
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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: |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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
@Saransh-cpp Thanks for the review, I've replied to the comments and changed the test |
There was a problem hiding this 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): |
There was a problem hiding this comment.
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, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why was this added?
There was a problem hiding this comment.
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
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.
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?
There was a problem hiding this comment.
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.
src/pybamm/models/base_model.py
Outdated
@@ -77,6 +77,8 @@ def __init__(self, name="Unnamed model"): | |||
self.use_jacobian = True | |||
self.convert_to_format = "casadi" | |||
|
|||
self.calculate_sensitivities = [] |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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
src/pybamm/solvers/base_solver.py
Outdated
@@ -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)) |
There was a problem hiding this comment.
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
?
There was a problem hiding this comment.
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
@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 <[email protected]>
Co-authored-by: Saransh Chopra <[email protected]>
…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_code
sp 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-commit
nox -s tests
nox -s doctests