pyshp icon indicating copy to clipboard operation
pyshp copied to clipboard

Add type annotations

Open schwehr opened this issue 1 year ago • 4 comments

Describe the feature request

Once the code on the main branch is only for python >= 3.9, it would be great from a user pov to add some type annotations. I find them really helpful when writing code using a library (both as a human and for autocomplete). Also, they tend for find weird corner case bugs in existing code.

I can do initial work on type annotations for the easy cases.

Contributions

  • [X] I am interested in implementing the described feature request and submit as a PR.

schwehr avatar Sep 01 '24 16:09 schwehr

This would be really good, especially given the frequency PyShp decodes bytes to str and encodes str to bytes.

Which type checker should we use?

JamesParrott avatar Sep 08 '24 15:09 JamesParrott

Which type checker should we use?

It's easy enough to have a GitHub action use the 3 major ones. And mypy would be a likely good default for the primary one.

earthengine-api has an action that checks all 3

https://github.com/google/earthengine-api/blob/master/.github%2Fworkflows%2Fci-type-verification.yml

schwehr avatar Sep 08 '24 19:09 schwehr

Sounds great. Kinda like checking your C++ compiles with clang, gcc and msvc.

I would just point out this is essentially a mature code base (albeit only a ~2000 line file), so don't torture yourself. I want to encourage yourself, and other new contributors, and PyShp is exactly the sort of library that will benefit from static typing.

But if you encounter a fiendish typing challenge, that feels like you're fighting the Typescript compiler to no avail, then it's fine with me to type it as Any, and perhaps add it as a Todo issue.

The Python typing system is still under development, and an algebraic typing system in particular is being worked on. So for some typing problems, it might be simply best to wait a year. After which there may be a much better way to solve it.

JamesParrott avatar Sep 08 '24 19:09 JamesParrott

@schwehr I've added some basic type hints, https://github.com/GeospatialPython/pyshp/blob/8015acc782e3ff6d4eee2b88005b4c1190d2a593/shapefile.py#L151 and have set up dmypy in CI via pre-commit. Ruff and Ruff-format too.

New contributors should be able to install pre-commit themselves, and run pre-commit install to install and run the hooks locally. Dmypy should be a bit faster on repeated runs, after the daemon spins up.

  • [x] Once all functions have type annotations, --strict can be added to the dmypy args

JamesParrott avatar Sep 15 '24 10:09 JamesParrott

Mostly completed by 19010df, except for 2 or 3 limited exceptions that I'll open specific issues for.

JamesParrott avatar Aug 05 '25 11:08 JamesParrott

Awesome! Thanks!

schwehr avatar Aug 05 '25 13:08 schwehr