DIRACGrid / DIRAC

DIRAC Grid
http://diracgrid.org
GNU General Public License v3.0
113 stars 176 forks source link

Making use of Python typing #6315

Open chrisburr opened 2 years ago

chrisburr commented 2 years ago

Now that DIRAC is Python 3 only we can think about making use of Python's typing fuctionality. This was added in Python 3.5 however over the past few years it has grown to be really powerful and is even capable of understanding DIRAC's S_OK/S_ERROR convention (#6309).

A few ideas:

To expand on the last point, we could have a function like:

def export_something(dict[int, int]) -> dict[int, str]:

Currently the keys would become str due to the use of JSON however if the RPC call took advanage of the type hints we could easily cast to the correct type and remove the need to do type validate in every handler method. pydantic would likely be the best way to implement this, similarly to how fastapi works.

fstagni commented 2 years ago

If we do down this way, should we consider running https://github.com/Instagram/MonkeyType or https://github.com/dropbox/pyannotate?

chrisburr commented 2 years ago

I hadn't considered that would even be a possibility. For bits with non-trivial DIRAC semantics (S_OK/S_ERROR/CFG/executeRPC/dict) we'll need hand crafted types (like https://github.com/DIRACGrid/DIRAC/pull/6309) but for the long tail of "easy" cases that looks like a good idea. Once mypy is updated (eta late August) and we have CI for it I'll take a look.

There is also the option of running tests with runtime type checking via tools like typeguard or beartype which might be interesting.

fstagni commented 1 year ago

I'd move this to diracx