timecode icon indicating copy to clipboard operation
timecode copied to clipboard

Add type hints, and restrict package to 3.7+

Open austinwitherspoon opened this issue 1 year ago • 0 comments

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.Literal isn't available until python 3.8, but it's a very useful type hint in this case since it allows us to overload to_systemtime() and to_realtime() to be specific about whether they output float or str! The workaround here is to handle the typing.Literal import 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 returning None, 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.

austinwitherspoon avatar Mar 18 '24 17:03 austinwitherspoon