boisgera / pandoc

Pandoc (Python Library)
https://boisgera.github.io/pandoc/
MIT License
134 stars 16 forks source link

Consider Python Type Hints #32

Open boisgera opened 3 years ago

boisgera commented 3 years ago

General thread : can / should python type hints be used intead of the existing micro-language used in the _def class attribute ? And maybe in the class representation, instead of the Python-Haskell hybrid language that is used today ?

Need some study here, but the general gist is that:

boisgera commented 3 years ago

To begin with, I probably would have to create a few typical instance of type hints relevant for the description of pandoc elements and then see how these type hints can be introspected. For example: how can I get Meta and list[Block] from tuple[Meta, list[Block]] ? Block from list[Block] ? etc.

boisgera commented 3 years ago

ATM I find the type hints hard to introspect. What I need i basically to be able to identify the components of things like tuple[Meta, list[Block]] and Union[Str, Space, ...]. Afaict ATM I would have to query some undocumented attributes (__name__, __args__ and or make some isinstance tests wrt to some typing classes).

It is also not clear how i would attached this info to pandoc classes ; somehow the type hint is for the class itself (typedefs), sometimes it applies only to the class arguments (constructors for sure ; and what would be the status for the union associated to an abstract data type ? It is the union itself ? Pretty much, yes). Unless I make sure that constructor ARE also type hints ?

Maybe I need to explore the librairies that do some runtime time-checking to see how they deal with this (that would be useful anyway).

boisgera commented 3 years ago

A minimally invasive idea that could be worth considering: what would it take to have (optionally?) the constructors of all pandoc types check the type of their arguments ? No need to change the _def micro-language or anything built on top of it (at least at the moment), just a runtime mecanism to pinpoint a type issue as soon as it is encountered.

Consider giving the intell that some external runtime validation tool is expecting (typeguard, beartype, enforce, pytypes, pyteen, etc.)

The "optional" part is related to the possible performance hint for the creation of a large collection of new objects. OTOH, validation would be mostly shallow (right?), so maybe this would not be as bad as expected.

boisgera commented 3 years ago

Nota: probably hard to use external validation tools and have typedefs by checked unless they are inlined into the type signature (?). Mmm unless I have already overriden isinstance for them and the type checker relies on this (?). Experiments needed ...