-
Notifications
You must be signed in to change notification settings - Fork 99
Adding a type hint to the process method of the BasePlugin class
#2224
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: master
Are you sure you want to change the base?
Conversation
this will allow other child classes to type hint without their type checker demanding that they return None from process. The type hint is set to `Any` as there are multiple process methods that return different types.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #2224 +/- ##
==========================================
- Coverage 98.39% 95.09% -3.31%
==========================================
Files 124 147 +23
Lines 12212 14853 +2641
==========================================
+ Hits 12016 14124 +2108
- Misses 196 729 +533 ☔ 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.
To review this, I did a bit of googling and came across the Liskov Substitution Principle. This describes the problem that you're trying to fix - child classes of BasePlugin should override def process with a method that accepts and returns objects of the same type.
This abstract base class was written before type-hinting was adopted in IMPROVER and the default behaviour is that inputs are Any and outputs are None unless stated otherwise, which is why this PR is useful, we do NOT expect the outputs to be None. We could be more specific than Any, because we expect all IMPROVER plugins to return Union[Cube, List[Cube]], I think.
I also think that the __call__ method above should match whatever goes on the process output, because it returns whatever process does.
I'll ask for a second opinion.
There are a few examples of it returning other things, I've included these 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.
I agree with Stephen that the __call__ method could have the same return type. I agree with Phil that we cannot limit this to cubes and lists of cubes now we are returning e.g. numpy arrays from PyGam. -> Any seems fine; it includes None and I could find no solid guidance on whether there was any benefit / difference using an Optional[Any] approach.
|
I agree that we need to allow While we're at it, the same |
We could specify all the desired types in a
I can also do this, however it would likely slow down the entire package somewhat as we would have to do an *EDIT: I should add, that yes we probably should do this. But it causes problems (and it doesn't affect me so much as I have yet to ever write anything importing this class) so I'm less inclined to fix it. |
Yes, I had forgotten about that. It impacts the response time for the IMPROVER CLI if you only want the help string. Ok. Just add |
This will allow other child classes to type-hint without their type-checker demanding that they return
Nonefromprocess. The type hint is set toAnyas there are multipleprocessmethods that return different types.Description
Testing: