rsgalloway / pyseq

Compressed sequence string module for Python
https://pyseq.rsgalloway.com/
Other
123 stars 36 forks source link

Add type hints #59

Open Emerentius opened 5 years ago

Emerentius commented 5 years ago

This adds type hints via the python2 compatible comment syntax. Optional static typing gives users much better autocompletion and the ability to use mypy for statically checking their code, if they so choose. There is no downside for users who don't want to make use of it and no runtime costs except for a single if False check during imports.

There are no code changes except for:

I've tried to make the type hints as general as possible so that there aren't any false positives. Two warnings remain, at least one of which is because of a bug in the code. I've also uncovered some weird cases.

Notes on type hints

Strange behaviour / inconsistencies encountered

Remaining warnings

rsgalloway commented 5 years ago

Interesting, thanks for sharing. I'm not familiar with mypy, can you elaborate on some of these benefits?

Optional static typing gives users much better autocompletion and the ability to use mypy for statically checking their code

Autocompletion in what context? And, statically checking pyseq code, or client code? Are pydocs insufficient here? This is not a very large project, and it doesn't seem like these changes will improve the performance, so I'm a little unclear what the benefits are yet.

Also, if static typing is the goal why not look into Cython, which will also improve performance?

Emerentius commented 5 years ago

Autocompletion is improved in IDEs like VS Code and PyCharm. When the editor knows that a variable is a str or a list for example, it can suggest methods of those types as soon as you type a dot. In my experience, this makes using libraries a much nicer experience.

A type checker like mypy or pyright can also warn when you're passing the wrong type to a function (like a str where an int is expected) or calling methods that the type doesn't support. The <variable> may be None warnings I quoted are also examples of this.

Both pyseq and client code can be statically checked. For clients, type checkers will assure that users are passing the right types arguments and inform them of what the exact types are that they are getting back. Documentation doesn't obviate types. It's more effort to consult it and it is also often outdated or incomplete. Case in point: uncompress claims to return a Sequence, but it can also return None or an empty list. The documentation for walk doesn't tell you the return type at all.

Type checkers help by automating correctness checks, their goal in Python isn't performance. Type annotations don't affect runtime behaviour at all. I don't know about Cython, but these types are a simple, fully compatible add-on onto regular Python. They have received special syntax in Python3 any many libraries are supporting them already.