-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Improve some type hints to bump mypy pin #6294
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
Improve some type hints to bump mypy pin #6294
Conversation
Codecov Report
Additional details and impacted files@@ Coverage Diff @@
## main #6294 +/- ##
===========================================
- Coverage 94.12% 77.34% -16.78%
===========================================
Files 111 111
Lines 23861 23901 +40
===========================================
- Hits 22458 18486 -3972
- Misses 1403 5415 +4012
|
62e195a to
165b4ca
Compare
|
Pinging @cluhmann since this came out of yesterday's PyMC Study Group. |
|
That looks like a tremendous effort. I know that mypy, like lots of the "housekeeping" bits of CI can be kind of annoying and that there is a constant temptation to just work around them (#6282). So thank you for putting the effort into trying to set things right (and going beyond just what was needed to prevent mypy from failing). |
|
This looks nice! I added a suggestion about the state in integration.py |
Co-authored-by: Adrian Seyboldt <[email protected]>
|
Thanks so much @cluhmann for the kind words. It's nice to be appreciated. My current effort here just scratches the surface of the remaining work to be done. Thanks a lot for the type hints @aseyboldt, that's really helpful! There seem to be a few further issues as a result, I'll see if I can fix those now... |
|
Ok, I think I got it. @aseyboldt, could you please check if 8b127a6 is safe? The |
|
Good question. There are some differences between float and the numpy types: But I can't see anything like this happening with the energy, we only want to add, compare and store it in the stats, so I think this is safe. |
|
Ooh, that's really interesting! Thanks for sharing. And thanks for confirming the safety. Any further suggestions? Not sure what's up with codecov, but that's usually flaky, right? |
michaelosthege
left a comment
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.
Great work!
I think we could (should?) keep mypy pinned, because we have a dependabot that will open PRs for the bumps.
This would reduce the chance of contributors suddenly running into errors with the dev environment..
Not sure what's up with codecov, but that's usually flaky, right?
Yes, it's flaky
| dtype: np.dtype | ||
|
|
||
| @overload | ||
| def velocity(self, x: np.ndarray, out: None) -> np.ndarray: |
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.
Shouldn't out get a real type here? Because None is not a 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.
You're technically correct. Although the singleton None has type NoneType, I believe that type checkers automatically do an implicit conversion here. (Who really wants to remember to type the extra letters and be so pedantic? There's no potential for ambiguity since it's a singleton type.)
If you check the mypy docs, they always use None, so it's not a problem.
Co-authored-by: Michael Osthege <[email protected]>
Sounds good. I just repinned mypy to the latest v0.990. |
Closes #6282
What is this PR about?
As explained in #6282, we were failing with the latest version of mypy for the pre-commit check, so we pinned to an older version of mypy where we were passing. This is my attempt to fix enough type issues so that we can unpin mypy and close #6282.
mypy=0.982in a separate PR, have all my commits squashed, or for me to squash all the typing commits (leaving two commits: "improve type hints" and "unpin mypy")I put in some medium effort to infer the correct types (but without a debugger), and probably I have a few incorrect types which should ideally be corrected. In the process of trying to infer types, I recursed in various places while adding type hints along the way. Thus this PR isn't minimal for closing #6282, and there are also files where I have updated some annotations without updating other similar annotations.
I'd like to think that things are better than they were before, and this is just about type hints which should be irrelevant at runtime, so probably this doesn't require an extremely thorough review. Nevertheless, here are several technical details to explain what I'm thinking:
-> None.)from __future__ import annotations, but I have noticed that there are currently no such imports inmain. Is there any particular reason for this? (Just in case, I installed Python 3.8.0 and was successfully able to import all the relevant modules, so it seems to me like it should be fine.)collections.namedtupleis out-of-favor. It's preferable to define named tuples as classes inheriting fromtyping.NamedTuple, and then type annotations are provided with a nice syntax.__future__.annotationsallows for replacingOptional[SomeType]withSomeType | NoneandList[SomeType]withlist[SomeType], and indeed pyupgrade enforces this as soon as__future__.annotationsis imported.# pylint: disable=unused-import. But then mypy began complaining about the assertionassert Distributionwhich was meant to "use" the import. Thus I replaced the assert with a null assignment_ = Distributionto achieve the same effect without the warning.State, so I sloppily set these types toAnyso that I didn't have to deal with them. I'm also not very confident aboutq,p, andv, as I'm not so sure when what should be aRaveledVars, a numpy array, an Aesara tensor, or potentially unions of these. Any insights @aseyboldt?integratorattribute for_Treehas typeintegration.CpuLeapfrogIntegrator, since it seems to be the only available implementation of an integrator, and there doesn't yet seem to exist an abstractIntegratorclass.@overloads. Note that@overloads are type annotations and not real definitions. They are telling type checkers that when the parameteroutisNone, thenvelocityreturns a numpy array, and whenoutis a numpy array, thenvelocityreturnsNone. What doesn't seem to work is type inference from PyRight in VS Code based on the default parameter valueout=None, presumably because this default may be overridden in implementing subclasses. Thus when callingvelocity()I setout=Noneexplicitly, so that VS Code infers the right types.Checklist
Major / Breaking Changes
Bugfixes / New features
Docs / Maintenance