Hello,
I'm looking to understand how to best use type hints in a mixed
C/Cython/Python2/Python3 code base (pygame). *[0]
Such that:
- Tools like mypy would work.
- Sphinx (the doc tool we use) would work for documentation of the types
(Perhaps using: https://github.com/agronholm/sphinx-autodoc-typehints)
Note it uses typing.get_type_hints (and therefore __annotations__).
- We could maintain it inside our code base (either eventually or from
the beginning).
- Python2 would be supported for type checking. (it's not a hard
requirement).
- typing.get_type_hints would work inside a python repl.
- There are not lots of extra files would be nice.
- No, or very little extra load time. This is important for end users
not doing typechecking or documentation generation. (ie, people using apps
made using pygame).
- Would prefer a clean way if possible, even if it's not implemented yet.
It seems that C extensions are currently only supported well by using type
stubs? *[1]
However typing.get_type_hints does not seem to use stubs.
Can we define types in stubs, and load them into python at runtime somehow?
Such that the types are stored in __annotations__. Failing that, being able
to load them somehow so that sphinx can use them for documentation would be
useful. I see retype *[3] uses typed_ast *[4] for parsing stub files. Using
typed_ast.ast3 on python 3, and typed_ast.ast27 on python2 seems
appropriate. This could perhaps be one path towards allowing annotations
defined in stub files to be used by sphinx.
Any pointers or ideas would be appreciated :)
cheers!
[0] Related pygame issue: https://github.com/pygame/pygame/issues/668
[1] https://www.python.org/dev/peps/pep-0561/
[2] issue that spawned 0561 https://github.com/python/typing/issues/84
[3] retype https://github.com/ambv/retype/blob/master/retype.py#L162
[4] typed_ast https://github.com/python/typed_ast