Skip to content

feat: Add type hinting - #791

Merged
StephenSorriaux merged 1 commit into
python-zk:masterfrom
ThosRTanner:add-typing-hints
Aug 23, 2026
Merged

feat: Add type hinting#791
StephenSorriaux merged 1 commit into
python-zk:masterfrom
ThosRTanner:add-typing-hints

Conversation

@ThosRTanner

@ThosRTanner ThosRTanner commented May 16, 2026

Copy link
Copy Markdown
Contributor

This adds nearly full type hinting, and enables all but two of the checks that were enabled in the mypy section in pyproject.toml. I hope to address the remaining checks, but it was proving tricky to address some of the typing.Any uses without code changes.

Also if you enable deprecation checks with --enable-error-code=deprecated, you'll get warnings if you use deprecated options (at least as far as I was able to spot them!)

This addresses #647 but there's not entirely strict checking as yet.

Although I've tried very hard not to change any code for this, some return None statements have been added, which has caused the code coverage to drop as apparently those return paths were not covered by the testing.

Also I've been less polite with the testing, and have changed the code in a number of places, including dropping test classes that did nothing, and adding asserts here and there to keep mypy happy as it sometimes doesn't spot a value could have been changed.

I've added FIXMEs to indicate some of the more egregious type warning suppression that I did to avoid doing code changes, and intend to address those in another PR.

I've also added tests for the serialization.py module as some of it is a little unexpected and at one point I'd managed to break it but the resultant errors were very very unclear.

A note: I've also had to suppress some errors in flake8 and use more general #type: ignore suppressions than I'd like because hound CI does something very very odd and tries to parse the text in the [] of the #type: ignore[] comments as python, and generates some very peculiar messages.

Why is this needed?

Type hinting makes it much easier for users to ensure they have the right parameters for functions.

Proposed Changes

  • Type hints for all the public APIs
  • Type hints for all the private APIs
  • Updated tests to use appropriate type hints or in one or two cases fix tests that were doing something peculiar.

Does this PR introduce any breaking change?

Well, it might break people who're already using mypy. And it's entirely possible I've got the wrong type hints (though hopefully actually using the type hinting in the tests has pulled all those out - it certainly picked up a couple of places where I'd got it wrong).

Codecov notes:

  1. eventlet.py - this says it's missing coverage for the eventlet event_object, lock_object and rlock_object.
    • There are only 2 places in the tests that use the SequentialEventletHandler
      • test_eventlet_handler which doesn't have enough in it to trigger locking
      • test_cache.py which will use SequentialEventletHander if it can't find SequentialGeventHandler - which as far as I can see it always will.
    • so I'm not sure how it can have achieved any coverage before.
  2. connection.py - Apparently it doesn't drop out of the main loop in next_server. However, the only way I can see it doing that is if the hosts list is empty, which I don't think is a reasonable situation. Additionally, codecov on earlier code was showing the clearly unreachable code before the end of the loop as being covered.
  3. utils.py - Caused by an explicit return None at the end of a function instead of the implicit one. Cannot tell if this path was ever covered.
  4. lock.py - Caused by an explicit return None at the end of a function instead of the implicit one. Cannot tell if this path was ever covered.

@ThosRTanner
ThosRTanner marked this pull request as draft May 17, 2026 12:46
@codecov

codecov Bot commented May 17, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 99.10615% with 8 lines in your changes missing coverage. Please review.
✅ Project coverage is 95.49%. Comparing base (e894342) to head (37458f6).
⚠️ Report is 1 commits behind head on master.

Files with missing lines Patch % Lines
kazoo/handlers/eventlet.py 92.10% 3 Missing ⚠️
kazoo/protocol/connection.py 95.89% 3 Missing ⚠️
kazoo/handlers/utils.py 97.82% 1 Missing ⚠️
kazoo/recipe/lock.py 97.87% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master     #791      +/-   ##
==========================================
- Coverage   96.65%   95.49%   -1.16%     
==========================================
  Files          27       27              
  Lines        3554     3865     +311     
==========================================
+ Hits         3435     3691     +256     
- Misses        119      174      +55     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@ThosRTanner
ThosRTanner marked this pull request as ready for review May 18, 2026 21:29
@ThosRTanner
ThosRTanner force-pushed the add-typing-hints branch 2 times, most recently from 2a23a5a to 3a14bc1 Compare May 19, 2026 20:41
@ThosRTanner ThosRTanner mentioned this pull request May 20, 2026
@StephenSorriaux

Copy link
Copy Markdown
Member

Hello,

Thanks a lot for the PR and the issues you tagged along the way, it's really appreciated to have some fresh eyes on the project.

This PR is quite big so I tagged the @python-zk/maintainers team to have feedback. As for me, I am not really a use-typing-in-Python guy. When I need typing, I use another language, but I know those changes are very valuable for the community. I will review it in the next days as I have some time off.

Special pings to @a-ungurianu and @bringhurst: since you worked on typing features in the past, it would be really appreciated if you can chime in and add a review.

@ThosRTanner
ThosRTanner force-pushed the add-typing-hints branch 2 times, most recently from 872accb to 1a3456c Compare May 28, 2026 21:37
@ThosRTanner

Copy link
Copy Markdown
Contributor Author

@bbangert @jeffwidman @ceache @a-ungurianu @StephenSorriaux

It's been a while - any feedback on this?

@StephenSorriaux StephenSorriaux left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After 2+ hours, I just finished going over most of the files. This one is huge. I still need to go over the "project" (constraints.txt, pyproject.toml`, etc.) files. I will do that later this week.

Comment thread kazoo/handlers/eventlet.py Outdated
Comment thread kazoo/handlers/utils.py Outdated
Comment thread kazoo/handlers/gevent.py Outdated
Comment thread kazoo/protocol/connection.py Outdated
Comment thread kazoo/protocol/serialization.py
Comment thread kazoo/recipe/counter.py Outdated
Comment thread kazoo/recipe/lock.py
Comment thread kazoo/tests/test_sasl.py Outdated
@ThosRTanner

ThosRTanner commented Jul 14, 2026

Copy link
Copy Markdown
Contributor Author

@StephenSorriaux yeah, sorry about the size. the settings that were there already were quite strict, and it was hard finding a root place to unpick from :-( Thanks for looking though.

@ThosRTanner

ThosRTanner commented Jul 18, 2026

Copy link
Copy Markdown
Contributor Author

Hopefully addressed comments. Not entirely sure why hound broke, as I can't see any messages from it.

Comment thread .flake8
Comment thread setup.cfg
zip_safe = false
include_package_data = true
packages = find:
install_requires =

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems to force every user that pip install kazoo to install those 2 deps. Shouldn't those 2 only be part of typing extra (ie. user needs to pip install kazoo[typing]?

@ThosRTanner ThosRTanner Jul 19, 2026

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Checking.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately, we need typing-extensions as that's where several types that are used outside of 'if TYPE_CHECKING' blocks and importing will fail. However, types-gevent was a mistake and I've removed that as it was already in the typing block.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that's because some of the types used for types checking are not available in all versions of Python that kazoo supports (ParamSpec, Unpack, etc.). Bumping the min Python version we support to like 3.13 would make this dep useless. Probably something we would like to keep trace of.

Comment thread pyproject.toml
@StephenSorriaux

Copy link
Copy Markdown
Member

Thank you again for this PR! I finalized my review with the files I did not check yet. Not sure what is going on with Hound too, I will merge this PR whether it's happy or not anyway.

@ceache

ceache commented Jul 19, 2026 via email

Copy link
Copy Markdown
Contributor

@ThosRTanner
ThosRTanner force-pushed the add-typing-hints branch 3 times, most recently from 922b232 to 247db8c Compare July 24, 2026 17:34
@ThosRTanner

ThosRTanner commented Jul 24, 2026

Copy link
Copy Markdown
Contributor Author

@StephenSorriaux I think that's everything addressed. I've also added a fix for when none of the builds working in .github/workflows/testing.yml because the mirrors had changed (which happened to me just now). Fortunately the message when that happens is quite helpful.

The py3.9/ZK3.6.4 failure seems to be one of those random failures that happens from time to time (cf #775)

Somewhat weirdly, hound decided to work this time. I'm going to refrain from making comments about it being barking mad...

@StephenSorriaux StephenSorriaux left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me, thank you again for this.

@StephenSorriaux

Copy link
Copy Markdown
Member

Please rebase with master and I will merge this

This adds nearly full type hinting, and enables all but two of the mypy
checks that were enabled in the mypy section in pyproject.toml. I hope
to address the remaining checks, but it was proving tricky to address
some of the 'typing.Any' uses.

Also if you enable deprecation checks with

--enable-error-code=deprecated

You'll get warnings if you use deprecated options (at least as far as I
was able to spot them!)

This addresses python-zk#647 but there's not entirely strict checking as yet.

Although I've tried very hard not to change any code for this, some
`return None` statements have been added, which has caused the code
coverage to drop as apparently those return paths were not covered
by the testing.

Also I've been less polite with the testing, and have changed the code
in a number of places, including dropping test classes that did nothing,
and adding asserts here and there to keep `mypy` happy as it sometimes
doesn't spot a value could have been changed.

I've added FIXMEs to address some of the more egregious type warning
suppression that I did to avoid doing code changes, and intend to
address those in another PR.

A note: I've also had to suppress some errors in flake8 and use more
general 'type: ignore' suppressions than I'd like because hound CI
does something very very odd and tries to parse the text in the []
of the type: ignore[] comments as python, and generates some very
peculiar messages.
@ThosRTanner

ThosRTanner commented Aug 23, 2026

Copy link
Copy Markdown
Contributor Author

@StephenSorriaux rebased!

@StephenSorriaux
StephenSorriaux merged commit a1265bf into python-zk:master Aug 23, 2026
37 of 38 checks passed
@ThosRTanner
ThosRTanner deleted the add-typing-hints branch August 23, 2026 18:53
@ThosRTanner

Copy link
Copy Markdown
Contributor Author

woot! thank you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants