Simon Kirby, March 2023
This is a quick project to go along with a small type hinting presentation. Examples have been written from scratch.
Poetryhas been used for virtual environment and dependency management. EnsurePoetryhas been installed locally, and then navigate to the project root. Once at the project root, open a terminal instance, and runpoetry install. More concisely, to get up and running, installPoetryand follow the commands shown below:
git clone https://github.com/UniExeterRSE/python-type-hinting
cd python-type-hinting
poetry install- Type hinting for this project has been managed with the VSCode Python extensions
PylanceandPyright. These extensions have been listed inextensions.jsonfile. These can be installed manually through the VSCode extensions tab/GUI. - A
settings.jsonVSCode file has been also been created, indicating the type hinting setting that can be altered."python.analysis.typeCheckingMode": "strict"can be set tooff,basicorstrict. - Other static type checkers exist. These have not been use here, but please explore them. An obvious choice is
mypy.
- The accompanying presentation can be found in
.pptxand.pdfformats in thepresentationsfolder. - A great crib sheet containing information on the basic Python types can be found on the MyPy site here.
- Additional information on type hinting in NumPy can be found here. However, type hint support is not mature in NumPy, and discussions are ongoing (see PEP 646).
- Even so, a generic type
np.typing.NDArrayhas been created which can be used by type checkers. An example is shown below:
>>> import numpy as np
>>> import numpy.typing as npt
>>> print(npt.NDArray)
numpy.ndarray[typing.Any, numpy.dtype[+ScalarType]]
>>> print(npt.NDArray[np.float64])
numpy.ndarray[typing.Any, numpy.dtype[numpy.float64]]
>>> NDArrayInt = npt.NDArray[np.int_]
>>> a: NDArrayInt = np.arange(10)
>>> def func(a: npt.ArrayLike) -> npt.NDArray[Any]:
... return np.array(a)Note that shape and dtype support are in development, and liable to change.
- As well as the above, additional libraries have appeared to help the situation, such as nptyping. This has not been tested, so your mileage may vary.