Replies: 9 comments 18 replies
-
I'm strongly for type annotations for the reasons you have provided, and also because they make it easier to understand what unfamiliar code is doing. For that same reason, I'm strongly for annotation via type signatures. Code is at its best when it is self describing - you look at it and you can see what it does. Type annotations within the code itself provide a great opportunity to improve this. I'm very uncomfortable with annotations via stubs; there is already a lot of complexity around Iris to make the development experience smoother - CI, linting, setup scripts... I would much rather provide type annotations via the existing code than add a new element of complexity in the form of stubs. |
Beta Was this translation helpful? Give feedback.
-
From my playing with them, I do think that it would be useful to slowly add annotations into the source code. They can be incrementally added as code is touched and there's no need to add all of them in immediately. I agree that stubs would be extra complexity to maintain and they're not immediately obvious to users looking at the code because they are in a separate file. The only minor downside is that code starts to look more complex to beginners, for example:
Could look scarier and more confusing to a beginner than the old style without annotations:
Although I guess that it can also be argued that the extra information conveyed should help beginners. However the benefits of using them outweigh the negative of this complexity, but it is worth bearing the additional complexity for beginners in mind to see if it can be minimised. |
Beta Was this translation helpful? Give feedback.
-
For info, Matplotlib just put type stubs into |
Beta Was this translation helpful? Give feedback.
-
Ping @hdyson We've started the discussion on typing here, so wade in to the conversation with your opinion. I think the extra context would be super useful, and it would help us understand the general benefits to the wider community. Personally, I'd love to bump up the priority on this piece of work and make it happen. |
Beta Was this translation helpful? Give feedback.
-
Just on a point of fact.. I'm afraid this isn't possible as we have it. We didn't see a need to subclass the cube, but have just extended properties and behaviours so that a cubes "may" have a mesh + related properties. Likewise with the masked array thing : I think you are hoping for too much here, as virtually anything in Iris (or numpy) that returns an array might return a masked one, and vice versa. I think there's also a general point here about what typing can do for you + the way it works regarding subclassing... |
Beta Was this translation helpful? Give feedback.
-
I've recently set up a new codebase built on top of iris, with the opportunity to set new coding standards. We're implementing comprehensive coding standards checks as part of the integration testing. We've had to delay introducing type annotations to the testing for the new project due to the lack of type annotations in iris. From my point of view of wanting to create a robust and easy to maintain codebase, the selling point for type annotations is that it enables the usage of tools like mypy to check that the type information provided to the end user is accurate. Because iris does not have type annotations, mypy falls back to assuming anything that touches iris is the This creates the awkward scenario where developers would see useful error messages when giving incorrect type information for methods that worked purely on numpy arrays, but would not get errors for methods that used an iris data stucture. This leads into the case where developers assume the type information provided is accurate, because there's no error message generated. Given we're leaning very heavily on iris, this effectively bars us from taking advantage of type annotations. The iris code base is large. Annotating everything would be a gargantuan undertaking - instead, could I propose breaking the task into smaller pieces? Type annotations for the public methods of a Cube would be a more manageable task, and would be a substantial benefit for end users. I will also flag that Napoleon support for combining numpydoc style docstrings and type annotations isn't in a great state at the moment. So even though I'm advocating for more type annotations in iris, there is definitely a pragmatic argument that it makes sense to wait for the infrastructure to catch up first. |
Beta Was this translation helpful? Give feedback.
-
Just to cross-link ideas, we previously concluded that we couldn't make good use of type hinting in API docs, but I think that the ground may have shifted on that : see comment for a suggestion that now seems to work a lot better As such, I wonder about now reversing #4510 . Support for that, anyone ? |
Beta Was this translation helpful? Give feedback.
-
I did a quick search for advice on the motivation + utility of type hinting generally. Here's come links I found which I thought were useful on the type hinting question : |
Beta Was this translation helpful? Give feedback.
-
Closing following the creation of #5924 - we do want to do this one way or another |
Beta Was this translation helpful? Give feedback.
-
Should we add static types to iris?
Python has had support for "annotations" since 3.0, but it has taken a number of years for the use of the annotations to shake out. PEP 484 and PEP 560 standardised the use of annotations for static typing. See this towards data science article for a good high-level overview of the state of static typing in Python up to 3.10.
Now that the community has agreed(ish!) on a standard form of type annotation, and the tooling has been developed to make use of it, it may be the right time to start adding type information to iris.
There are two possible approaches to adding type information:
*.pyi
stub files that only contain type definitions of functions. These are a bit like header files in other compiled languages.Both have pros and cons. \1 requires changes to all files across the codebase. \2 means keeping additional files in sync with the codebase.
Using the monkeytype and pyannotate libraries recommended by mypy I have made a first pass at creating stub files for the public API of iris, to give a sense of how it could be useful. These libraries infer as much as they can from the existing untyped code e.g. if a class is created and then returned, mypy is clever enough to work out the signature of that function and monkeytype will create the appropriate definition stub.
iris-stubs can be pip-installed in an environment alongside your iris code, either a release version or development version. Tools such as mypy, which is built into the Python extensions of common editors such as vscode and pycharm, will know how to use these stubs to provide richer intelligence in the editor.
I find this especially useful for two main reasons when editing code:
a.strip().split()..etc
as it understands the return type at each stage, your editor can provide better autocomplete for the next possible calls.your editor will warn you that this is a possible error. As the function could also return
None
, but your definition as written here guarantees the function will return a string.Take a look at the stubs - they are very likely to be incomplete and possibly wrong so please edit if you find a problem. If anyone wants a tutorial in reading and writing stub files let me know.
Beta Was this translation helpful? Give feedback.
All reactions