-
Notifications
You must be signed in to change notification settings - Fork 74
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
Interest in adding type annotations? #330
Comments
Hi @rsokl! Sorry for the delayed reply. I'm really glad you opened this issue. We have been talking about type annotations but we haven't decided yet if this is something we want to implement. I know that introducing them offers some benefits when working inside IDEs, is there any other benefit that you can tell us about? On the other side I found a few drawbacks of type annotations:
I know that projects that I love, like xarray, are using type annotations. In particular, xarray implements them in some functions/methods, but not on every one of them. So that makes me wonder if I'm missing some extraordinary benefit of type annotations besides the ones that come with an IDE. And also if maybe having type annotations in only a portion of our functions/methods is sufficient to get all the benefits that they carry. I leave here two blogposts with oposite opinions on type annotations:
Would love to hear more about your experience and the benefits of type annotations. Even though I listed a few drawbacks I'm open to be convinced that they do worth the effort. |
Hi @santisoler thanks for getting back to me.
It should be noted that a static type checker like mypy has a CLI. It can be run on one or more .py files and it will provide detailed error/warning reports on those files, based on its static analysis of the code (e.g. if it sees that you called a string method on a variable that it infers to be a bool, it will report an error). The point being that these type checkers are not only to be used in an IDE; they are frequently used in a project's CI/CD pipeline. More on this later. For the average user, the main benefit would indeed that their IDE would be able to provide useful warnings and autocompletions for code that involves pooch's API. For me, as a user, this can be quite a boon to my productivity. Having type hints help guide my intuition so that I do not need to stop and refer back to the API docs ("okay when they say path, do they mean a For library a developer who wants to build off of pooch the benefit of type annotations can be far more dramatic. It makes code refactoring easier and much safer, and it makes it easier for me to test the parts of my code that touch pooch. Type annotations make it significantly easier and safer to refactor library code 1. For example, I might change some of my library's internals so that they pass around Also consider that it can be challenging to write tests for code that involves pooch. E.g., I may have a function that uses pooch to download several gigs of data, which I do not want to run every time that I exercise my test suite. How do I get at least some assurance that this code works, even as I upgrade to new versions of pooch? I could use monkey patching and mocks to write a basic smoke test for this code, but it is far simpler for me to just run mypy/pyright in my CI to get assurances that my code is using pooch in a way that is consistent with its type annotations. I can even run this static analysis against the main branch of pooch so that I see if there are any upcoming changes in pooch that will impacts the way my library uses it. This future-proofing is not possible with mocks.
Being able to get near instant feedback about code errors -- without having to run the code -- is incredibly useful. Depending on a project's scale, it is arguably essential. The initial push for static type checking in Python came from DropBox. They had a massive code base that was becoming increasingly hard to reason about, and they needed a way to get instant feedback when they modified their code. So they made mypy. Similarly, Microsoft acquired a ML company and inherited a huge mess of Python code. The team lead decided that, without static type checking, this code represented a big liability, so he wrote pyright 23. For these teams/projects, having static type checking is truly essential. I think that the addition of type checking in Python has been transformational for the language and that it has enabled companies to more sustainably depend on Python in production. I am at the point where using unannotated code feels like I am missing one of my senses, especially when I am writing a library. I am so used to relying on static type analysis to get feedback on my code, that it feels... really bad to use code that is missing annotations. This is how I ended up opening this issue! To be frank, I find that my workflow is disrupted whenever I begin interacting with pooch because it is missing annotations. Footnotes
|
Hi @rsokl thank you so much to the very detailed explanation! I honestly haven't bothered to look into type hints so far but you kind of convinced me to have a go. One final question about this: Could you implement this incrementally or would it need to be all or nothing for it to be useful? If you can implement this one class/function/module at a time, would you mind doing it for just the |
Sorry for not replying... I think this issue got buried in my notifications 🤦🏼 I agree with @leouieda that we could give it a try with incremental steps, if that's possible... I've started experiencing with type hints, and I found it a little bit cumbersome with complex types like Numpy and Xarray objects... but for simpler Python types (likes the ones we use in Pooch) I think working with them is much more straight forward. |
I just started using pooch and I think this is a great tool! +1 on adding type annotations, I think it would be a substantial help giving the additional information to my editor while coding, and I think it makes code arguably more readable if you're using a formatter like black or ruff. I've also added type hints to other packages after first release, and I think it has been a substantial improvement for the usability. To jump in on the other questions:
This can definitely be implemented incrementally. By default mypy doesn't check typecheck functions that don't have annotations, so you can make changes to one thing at a time. Also, mypy won't use the annotations when typechecking other packages unless there is a
I realize this was not directed at me, but I'm going to take a crack at it and put up a PR with some minimal changes and try to add the typechecking to the CI config. EDIT: Draft PR here #404 |
Hello! I am a big fan of pooch! 😄
Would you be interested in my adding type annotations to the library? I think that this can help improve the user experience -- via enhanced 'intellisense' in IDEs when working with pooch, and for instant feedback when a user makes a mistake. I have experience creating/maintaining hydra-zen which makes extensive use of advanced typing features, and I have helped Hypothesis adopt type annotations.
Some things that this would entail:
typing_extensions
as a dependency (this is already quite ubiquitous, and is practically part of the std-lib). This provides backports of nice typing features, such asLiteral
andProtocol
, so that these features can be leveraged without breaking compatibility with older versions of Python.Let me know what you think!
The text was updated successfully, but these errors were encountered: