Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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
Adds typing to expression tree #3578
Adds typing to expression tree #3578
Changes from 29 commits
0f28844
ea172e0
f5dd303
8854bdc
e41a924
79aefb8
ad27152
9b281e2
f2faa8a
da3b072
1dfb1b4
d9e3ae2
77f00a6
69f1124
44e5161
811cf8e
bb75a85
07f48d9
e9a77a8
b0234cb
4d6fb32
64f3fc9
c9a0ff1
df6501f
ed2288b
062bd00
5333d42
f7ff456
9cef34b
21e4107
d1ae819
fa5d27d
d80c981
8b0a2aa
102c2d6
38a3713
ea03b4f
eeceaa7
File filter
Filter by extension
Conversations
Jump to
There are no files selected for viewing
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.
Is it possible to avoid these type of ignore comments for the typing?
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.
These comments are used by mypy when static type checking. I've done my best to use them as little as possible, but in some cases the amount of rewriting required to make mypy happy would make the code less readable than having the comment (in my opinion). If we decide not to use a type checker, they can be removed.
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.
Yeah I would say this is probably a symptom of not enforcing types. What does the output say if you neglect this comment? If MyPy is not strict, is it something that we can leave as a warning to remind us to deal with it?
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.
This instance isn't actually a brilliant example as I've managed to get rid of the type error; however there are ~ 10 ignore statements I've had to leave on a line-by-line basis. Mypy raises an error for each
# type: ignore[]
comment even with strict mode turned off, but if we don't enforce mypy as having to pass in the CI then we could chose to use it as a 'todo' list of sorts and remove the# type: ignore
comments.The remaining errors are split about 50/50 between difficulties integrating mypy into heavily dynamically-typed areas (e.g. it won't let you re-define a variable with a different type before you've used it, even with the --allow-redefinition flag https://mypy.readthedocs.io/en/latest/command_line.html#miscellaneous-strictness-flags), and areas where pybamm would require more significant re-writing. For example in
serialise.py
, where mypy complains thatBaseModel
doesn't have abounds
attribute. This is true at the point where BaseModel is defined, but the attribute is added once the model is discretised; as that particular function only takes a discretised model, the code works properly but I haven't found a way to flag to mypy that this is the case. There are multiple ways to fix this, including creating aDiscretisedModel
class or editing the__init__
ofBaseModel
to include attributes that are added later, but that's a lot of work & discussion that shouldn't be done here.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 to enable type checking with a global variable?
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.
Related to above - this is a statement used by mypy. In this case sympy is used in the file only as a type hint, not during runtime.
TYPE_CHECKING
is a constant assumed to be True by static type checkers, but is false at runtime; so sympy will only be imported if a type checker like mypy is being run. https://docs.python.org/3/library/typing.html#typing.TYPE_CHECKINGThere 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.
Personally I would prefer to just have sympy always imported. If it is used in the type annotations, then why not just always have it there?
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.
That would make sympy a required dependency. Sympy is heavy and I would avoid making it a required dependency (only a subset of features in PyBaMM require sympy)
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 would say that if expression tree is using sympy types, then it is really a dependency. I have not downloaded this branch locally to double check, but I would guess that my IDE would complain if I did not have sympy installed too.
As we add type checking it is going to expose more things that should probably be real dependencies rather than optional ones.
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.
sympy is only used in the expression tree for
to_equation()
to use with LaTeX I believe, rather than the solvers, hence the optional dependency.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.
Yeah my point was more that we might have to rethink our optional dependencies if we are using static types. Expression tree depends on sympy if it is using sympy types, but by putting this check here we are pretending that it doesn't depend on sympy.
It is too much for this task, but if we want a typed library and not have all the dependencies for types then we might need to think about how to better separate our subcomponents. Maybe things like the latexify need to go into a separate module if we want it's dependencies to be truly optional