SpiNNakerManchester / SpiNNUtils

Utility classes and functions for SpiNNaker projects
Apache License 2.0
1 stars 5 forks source link

Convert typing to use Standard types where possible. #280

Open Christian-B opened 1 week ago

Christian-B commented 1 week ago

See: https://peps.python.org/pep-0585/

Starting with python 3.9 where applicable Standard classes

So instead of doing List[int] we can type as list[int]


As readthedocs is able to use type hint in the def to document param types we will remove ALL tying from the method documentation.


While doing this I will apply the rule at all (OK almost all) methods must be typed.

Exception will be (I reserve the right to add to this list)


All public methods must be documented

A protected (single underscore) should be documented.

Private need not be documented.


WILL not work with Python 3.8!


Features only available in Python 3.10 will not be used so we will keep

Christian-B commented 1 week ago

tests only need typing enough to pass

Christian-B commented 1 week ago

mypy is smart enough to know list[str] and list[str] are the same

so mixing then especially why developing this chain of prs is not a problem.

Christian-B commented 1 week ago

Speed testing between the various way to handle use of Optional values a: Optional[int] = 1 b = 0

Over 10,000,000 iterations

b+= a  

0.72 but will fail mypy

assert a is not None
b+= a

0.86

b+= cast(int, a)

1.34

assert isinstance(a, int)
b+= a

1.50

Christian-B commented 1 week ago

Therefor where mypy can not distinguish between None or an Object pick assert None but where mypy can not distinguish between type A or B use cast,

Remember of course incorrect cast will not save you at runtime! Assert will unless turned off