Adding python typehints to Klipper?

For any large codebase in Python I like working with typehints as they really assist by forcing bugs to surface when code is being written vs. getting runtime errors in the field. Additionally they are good at finding more subtle corner cases in the code where maybe a little used codepath returns the wrong type.

Typehints were made to be incrementally added to existing code, and currently you can run mypy with Python 3 to typecheck Python 2.7 w. typehints in the following style:

Python 3:

def embezzle(self, account: str, funds: int = 1000000, *fake_receipts: str) -> None:
    """Embezzle funds from account using fake receipts."""
    <code goes here>

is equivalent to the following in Python 2:

def embezzle(self, account, funds=1000000, *fake_receipts):
    # type: (str, int, *str) -> None
    """Embezzle funds from account using fake receipts."""
    <code goes here>

Do you folks see any value in typehints in general (I know I do), and whether adding them to Python 2 code in the style above, or delay until Python 3 is the norm? If there is enough interest I could stand up a branch with one module done for people to look at.

Matthew

1 Like

Moved to the Developers category

I feel such a fool, thanks for moving it over.

I also think could be helpful. Is there anyone more involved in this project that could share their opinion ?

I’m willing to contribute to this if it’s something the klipper devs would accept.

Since klipper does also support python2 which does not allow typehints, its difficult.

I think I read somewhere that klipper will drop python2 support soon, but I can’t find it anymore. @koconnor am I right, or was it just a promising dream of mine?

I will add typehints to the part of the code I created as soon as possible :slight_smile: