-
Notifications
You must be signed in to change notification settings - Fork 273
Enable mypy analysis for all files. #593
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
Conversation
Codecov Report
@@ Coverage Diff @@
## master #593 +/- ##
=======================================
Coverage 97.46% 97.47%
=======================================
Files 85 85
Lines 8203 8233 +30
=======================================
+ Hits 7995 8025 +30
Misses 208 208
📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more |
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.
Largely looks good, only some minor changes needed.
src/imitation/algorithms/mce_irl.py
Outdated
@@ -45,7 +56,7 @@ def mce_partition_fh( | |||
\pi is a 3d array, indexed \pi[t,s,a]. | |||
""" | |||
# shorthand | |||
horizon = env.horizon | |||
horizon = int(env.horizon) |
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 add a TODO here so we remember to fix this before merge (if the plan is to fix it in a seals PR first), or open a GH issue to track it (if we want to merge this PR before fixing it upstream)?
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 opened a PR and added an extra check that should still be relevant even once the upstream has been changed, but can still add an extra comment if that would be helpful
@@ -183,7 +185,7 @@ def test_that_bc_improves_rewards( | |||
): | |||
# GIVEN | |||
novice_rewards, _ = evaluation.evaluate_policy( | |||
cartpole_bc_trainer.policy, | |||
cartpole_bc_trainer.policy, # type: ignore[arg-type] |
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.
Similar to with the seals upstream issue, let's add a TODO or GH issue for this.
On upstream: I don't think we're going to have time to turnaround the SB3 change before you wrap up. The seals one should be a short fix, and I can review and merge same-day, so let's push for fixing that one upstream though. |
Co-authored-by: Daniel Pandori <[email protected]>
Co-authored-by: Daniel Pandori <[email protected]>
d713d93
to
b40046a
Compare
There's more places that are missing type annotations, but since they don't cause errors in mypy, I will leave them be, so as not to add any more noise to this PR. |
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.
LGTM.
@Rocamonde, I made some commits to this PR, can you please review the diff: https://github.com/HumanCompatibleAI/imitation/pull/593/files/f3805f8c7e2af8dd4e3803325ae586dee9e6f7ef..dc76fb8323eb9458e2cd23dd53b72f1e6ed5d7fa
New changes look good to me, at least |
Apologies, this did not show up on my GitHub PR notifications. Will review tomorrow. |
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.
Looks good, just one thing I noticed that might be a bug, do take a look.
Yay! Thanks for doing this @ianyfan , nice to see what we started being nicely completed. This will certainly make our codebase more robust to random bugs and errors. |
Thanks for the review @Rocamonde, issues should be resolved now |
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 is excellent work! LGTM. I left some comments with suggestions and questions, worth looking over them before merging to make sure we're happy with the direction of things.
@@ -343,7 +343,7 @@ def __call__( | |||
rew_array = np.asarray(rew_list, dtype="float32") | |||
return rew_array | |||
|
|||
def train_policy(self, n_timesteps: int = int(1e6), **kwargs): | |||
def train_policy(self, n_timesteps: int = int(1e6), **kwargs: Any) -> 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.
We could try to be more specific than this, but ParamSpec is not (yet) a very well supported feature by most type checkers, and it might not be worth the effort of typing 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.
I've not used ParamSpec
much so may be missing something, but how would it help us here? It seems designed in cases where the P = ParamSpec("P")
variable is reused, like decorators or other higher-order functions. But if we're returning None
I don't think it makes a difference.
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 meant for Any, not for None haha. The problem with this is that any subtype has to take any kwarg in the signature, but you only really want it to take the kwargs that .learn()
can take. But not sure how you can use ParamSpec for this now I think about it. I think it was designed as a generic that is filled in implicitly at the callsite, not for extracting the type signature of an explicit method (which tbh should be a valid use case).
@@ -182,7 +192,7 @@ def forward( | |||
self, | |||
observation: th.Tensor, | |||
deterministic: bool = False, | |||
): | |||
) -> NoReturn: |
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.
Interesting . Is this type not incompatible with the superclass? How does that work?
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 it works because NoReturn
is the bottom type: i.e. a type that is a subtype of all other types. There's a good discussion at python/mypy#4116
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.
Type theory is quite cool. I remember attending a talk in Cambridge recently on some people working on re-constructing the foundations of mathematics using a flavor of type theory.
@Rocamonde I believe I've addressed all outstanding comments on this, please do re-review when you have a moment. Note we'll need to merge this one before #603 because that removes the mypy excludes. |
Sorry about this. I reviewed the PRs sequentially from my inbox and only saw this should have been merged before now. |
Actually it was fine, the merge was trivial. Will wait for tests to run and then merge. |
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.
LGTM
Description
EDIT: remaining issues have been resolved. The issues with
env.horizon
andevaluate_policy
are to be taken upstream, and then depending on how quickly the turnaround is on those, either wait for them to be merged before merging this, or ignore the issues for now and un-ignore them once the fixes are done.ORIGINAL:
Fixes #573 by adding the remaining type annotations. Some of the issues in
mce_irl.py
is resolved by changingenv.horizon
toint(env.horizon)
which indicates that there would be a problem ifenv.horizon
was not an integer (I think it can benp.inf
?). Does this occur in practice (since this should necessitate a runtime check)?One other issue in
mce_irl.py
I fixed by using acast
, which I guarded behindif TYPE_CHECKING:
. I don't think there is a better way of doing this, since there is no other way of restricting the type to what we want.Remaining issues:
The ones in
preference_comparisons.py
appear to be due to an over-conservative annotation innumpy
though I may have missed something. If it is due to this, then I'm not sure if there is a better way to deal with the issue than totype: ignore
it.The issues in
test_bc.py
are also weird and may just be worth ignoring.pytestconfig.cache
is typed asOptional[Cache]
so I'm not sure whether this implies that it could actually beNone
sometimes, or whether it is just typed that way. If the latter, either an extraassert is not None
may be sufficient; if the former, some extra logic may be required to run the tests properly.Testing
Ran
mypy
over relevant files, as well asci/code_checks.sh
.