timecode
timecode copied to clipboard
Add type hints, and restrict package to 3.7+
Fix for #53
I've added type hints to the package, including a py.typed marker file for mypy. This requires python 3.7+ since type hints aren't available in python 2, but python 2 was end of life in 2020!
A few places to look closely at:
-
typing.Literalisn't available until python 3.8, but it's a very useful type hint in this case since it allows us to overloadto_systemtime()andto_realtime()to be specific about whether they outputfloatorstr! The workaround here is to handle thetyping.Literalimport error gracefully, and only attempt to do the overload if we're in python 3.8+. You'll see an if statement doing this at line 394 and 425. - The sorting magic methods (
__gt__(),__ge__(),__lt__(), and__le__()) now raise an exception when you try to compare to a non-compatible type. The old behavior was simply returningNone, which doesn't seem to be in line with standard python types.
I've tested this code on Python 3.7 and Python 3.11. All unit tests pass in both, and mypy works well in both. Pyright/Pylance in VS code also seems happy with these type hints.