-
Notifications
You must be signed in to change notification settings - Fork 95
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
Incorporate type hints when possible #704
Comments
Just FYI, the types aren't actually checked at runtime unless we add Thanks for opening @tsalo ! |
I did not know that. That's somewhat depressing and simultaneously pretty cool!
My concern about this approach (along with the same approach to reformatting with EDIT: Another relevant task is #448, which could be done piecemeal, but hopefully won't be. |
Intriguingly that's my relationship with Python in a nutshell!
I think that's fair. That said, I think that we have enough things going on that adding a major refactor of style is not up at the top of my list. If other developers feel differently, that's worth a discussion, however. |
I may be optimistic, but, IF types are added to the BIDS outputs branch #691, then modularize metrics #591, and then decision tree modularization #592, we'll have covered a substantial portion of the code base. At that point, systematic addition of types to the rest of the code will be annoying, but should be do-able. |
To clarify my 👎 : |
Would those additional dependencies have to be general dependencies that all users have to install/import or will they be test dependencies? (... and does that matter in swaying opinions?) |
It's pretty tricky to make them dependencies for only "some" users, though pretty straightforward to make them dependencies for developers only. I personally think that's a recipe for confusion and should be avoided, however. |
I think the main potential benefits would be (1) automatic type checking and (2) automatic type documentation, right? @emdupre I personally like the idea of automatic type checking, but I'm fine not using it. Unfortunately, as far as (2), it doesn't seem like |
I am obviously biased but (1) but it helps me when doing things on CLI and (2) coming from strongly typed languages it makes things a little more familiar to me. I doubt (2) is as much of a consideration for others, though. Other users may be using servers where it's difficult to set up some sort of IDE, however, and in that case it's nice to have the hints inline with the parameters. |
The Of course, we would still need to add EDIT: Also, given the rather limited impact this would have (short of using |
This might be a naive question, but could you clarify what nptying adds over the the numpy typing that's native to Python 3.8 and can be backported to 3.6? https://numpy.org/devdocs/reference/typing.html Can we get most of the benefits of type hints for now without adding another dependency? |
I didn't know that numpy types were native to 3.8, so that's good news. How can this be backported to 3.6/3.7 though? It looks like we'd need to install |
I overlooked that |
I think that's a reasonable solution. Does anyone have any issues with adding the |
I'm still a bit concerned about this. Just to confirm, this can't be done as a dev-only dependency ? |
I guess we could have a fake module like we do for |
Not sure if this changes things, but |
Thanks @notZaki! I think we can add |
Our minimum Python version is now 3.8, so I don't think we need any extra dependencies. I will open a PR adding a checklist to the PR template with things like "If I edited functions, I included type hints". |
Summary
Should we start using type hints, when possible, in our functions?
I believe that it would reduce the amount of argument validation that we do at the beginning of each function. Plus it would make our code more transparent.
One drawback is that, in order to support type hints for numpy arrays (one of our most common argument types), we would probably need an additional dependency like
nptyping
.Yea: 👍
Nay: 👎
Additional Detail
@jbteves has started using type hints in #692, which I quite like. He also brought up style guidelines in this month's developers meeting (see #701), so I thought I'd get the ball rolling with this issue.
Next Steps
The text was updated successfully, but these errors were encountered: