gkeepapi
gkeepapi copied to clipboard
Add initial type hints
Closes #118
- Rebased deprecate_py2 onto master before adding type hints, so the diff and/or commit order might be wrong on GitHub until deprecate_py2 is also rebased (I think)
- Added type hints to every public Keep method, except:
- Any
sync=Trueor similar bools, it's not needed findandfindLabel- I don't understand what we're doing with the regexp types ¯\_(ツ)_/¯ , I'd like to add typing to those too but I'd need some help understanding what's going on
- Any
This doesn't address the update method shadowing on line 621, or the unused resync arguments
Thanks for adding hints and cleaning up all the docstrings.
- I can resolve any conflicts, so no need to worry about that
- Could you elaborate on this? Are
bools implicitly typed in some way? - IIRC, we can just reference
re.Patternnow that we don't care about Python 2 (if you're specifically referring to the docstrings - I'll take a look at
resync
Let me know if/once you're done with this PR and I can merge and do any cleanup.
Sorry for the delay, I must've missed the notification.
- Are
bools implicitly typed in some way?
The (IDE/linter/type checker) should be able to already correctly guess the type for that:

I didn't add : bool because the method signature is already pretty messy and didn't want to make it worse
- IIRC, we can just reference
re.Patternnow that we don't care about Python 2 (if you're specifically referring to the docstrings
Basically I don't understand what we're doing here, is it for Python 2 compat? https://github.com/kiwiz/gkeepapi/blob/fc21aad6f33b26f4f8c78117d2fca71f675763b6/gkeepapi/init.py#L23-L26
Which leads to not being sure what type to annotate query here and here:
https://github.com/kiwiz/gkeepapi/blob/fc21aad6f33b26f4f8c78117d2fca71f675763b6/gkeepapi/init.py#L933
https://github.com/kiwiz/gkeepapi/blob/fc21aad6f33b26f4f8c78117d2fca71f675763b6/gkeepapi/init.py#L811-L820
I think it should be Union[re.Pattern, str] but I'm not sure
Got it. That sounds fine then. For re, that block of code is indeed for Py2 support - Union[re.Pattern, str] should do the trick with its removal.
I just realized typing.Optional exists, so I'll be replacing every Union[X, None] -> Optional[X]
In some places the args are optional but it's not mentioned in the docstrings, so I'll fix that too:
https://github.com/kiwiz/gkeepapi/blob/f0d066f8eda3492e9c884510265c3a2588123242/gkeepapi/init.py#L866-L871
Question: is there a standardized docstring format we're using here? If not (in the docstrings only, not in the typing hints), since they're comments, we could replace Unions with with X | Y and Optionals with X | None
Yup, the repo uses Google-style docstrings (https://sphinxcontrib-napoleon.readthedocs.io/en/latest/example_google.html). From a quick look at that page, it seems that Sphinx supports/knows about native type hints now. As such, I think we could just remove the typing info from the comments.
If you want to investigate, the docs can be built via the Makefile in docs/
- Force pushed to use
typing.Dictinstead ofdictfor earlier Python version support - Added regular expression query typing, removed Py2 support
- Removed types in docstrings
Fixed all my mistakes, Sphinx builds the docs successfully. There's a lot of warnings but none of them are about typing, almost all are WARNING: duplicate object description of X...
Should be ready for review
Thanks for your work! I've merged the changes into deprecate_py2. I'll do a review of the library and then merge into main.