Better Typing support for (Cell)agents #2072
Open
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.
I recently enabled Pyright for my editor, its a python type checker which is much faster then mypy and therefore actually usable. At least thats my experience.
Anyway, my experience with Mesa was very humbling. Take for example this code snippet from the schelling benchmark model
Lets start from bottom to top. The first typechecking error comes from
self.model.happy
. Since we hardcoded the model to be of type mesa.Model, it doesn't know that model.happy exists and is an integer. This was rather easy to fix with my first commit, where the Agent/CellAgent now can take a generic parameter for the model class. So you just have to define your Agent asThe next issue was that the ´neighbor.type` was unknown. This was much harder to fix and occupied me for quite a while. There are now two hoops you have to jump through to make it work (if you are determined to enable type checking).
First you have to again add this information to the CellAgent as before
and you have to define the cell and agent class when you define the OrthogonalGrid like so
I think this is not really optimal, but so far the only solution I could find. Maybe someone who knows the Python type system better then me can help improve this PR