PyROOT type hinting


ROOT Version: 6.32.11
Python Version: 3.9.14
Platform: AlmaLinux 9 (el9_amd64_gcc12)
Compiler: g++ (GCC) 12.3.1 20230527


Hi all!

I’ve been using pyROOT for some time now and I was too accustomed to the Python type hinting as to not wanting it to be available for ROOT, since I found myself checking the docs every now and then because I would forget the name of the class methods.

I found a previous issue that touched on this, but the conclusion was that it was not possible, since pyROOT doesn’t really exist, and it’s just Python calling on C++.

However, doing a bit of research, I found out about .pyi files, which only have the implementation of the classes and methods, and I’ve built a small typings package for ROOT with the classes and functions I needed. It is certainly incomplete, and most likely buggy, but I wanted to leave the GitHub repo here in case someone wants to use it or document it further (:

I’ve personally used it with VSCode, so the instructions are only available for that. But I guess it could be used for any language analysis.

Also, I wanted to ask if there will be a central, official typing package in the future from the ROOT devs?

Thanks to all for reading, have a nice one! (:

2 Likes

Hi @martialc,

Thank you very much for your work and for providing the link to your GitHub repo. I will add @jonas who will be able to comment on this.

Cheers,

Dev

Hi! That’s awesome, thank you very much for sharing!

We don’t have any plans on a typing package or type hints for the ROOT Python interface from the ROOT core dev side.

But the subject pops up quite often anyway :smiley:

Of course, handcoding the the .pyi files like you did is possible, but it’s not sustainable for the whole codebase. I think the way to go is to automatize the generation of these files from the C++ reflection that we already have in cppyy. E.g. you only put all the classes and functions, and enums that you want to get crawled into some yaml file, and then you have a Python script that inspects the corresponding C++ classes in ROOT and automatically generates the pyi files. The repo that you have is already a great reference for how the pyi files should look like, and it can help to validate the generate code.

What are your thoughts on this automatic approach?

Hi! Sorry for the late response, I was on holiday (:

I think the automated solution could be kinder with updates to the code and the docs, however I’ve found there are some things that need to be handled by hand, such as converting the code snippets from C++ to Python (which is not complicated, but should be done properly). In my opinion, rather than a script based approach, I think a ML approach could do a better work in these cases, although this requires training a model on the docs and the .pyi files.

At the end of the day, I think any kind of support for typing pyROOT would be welcome and very useful already, since there is none right now (:

Related: Python bindings need to work better with pyright, ruff, etc · Issue #19754 · root-project/root · GitHub

Thanks for the comment @martialc! Yes, examples that were manually written in C++ should be manually written in Python as well. This would be very hard to automatize