Skip to content

Watch: add typing information - #2744

Open
mr-c wants to merge 2 commits into
kubernetes-client:masterfrom
mr-c:watch-typing
Open

mr-c wants to merge 2 commits into
kubernetes-client:masterfrom
mr-c:watch-typing

Conversation

@mr-c

@mr-c mr-c commented Oct 15, 2026

Copy link
Copy Markdown

What type of PR is this?

/kind cleanup

What this PR does / why we need it:

The Kubernetes Python client advertises itself as being typed, but kubernetes.watch.watch was untyped.

Special notes for your reviewer:

Does this PR introduce a user-facing change?

NONE

Additional documentation e.g., KEPs (Kubernetes Enhancement Proposals), usage docs, etc.:


@kubernetes-prow kubernetes-prow Bot added release-note-none Denotes a PR that doesn't merit a release note. kind/cleanup Categorizes issue or PR as related to cleaning up code, process, or technical debt. labels Oct 15, 2026
@linux-foundation-easycla

linux-foundation-easycla Bot commented Oct 15, 2026 •

Copy link
Copy Markdown

CLA Signed
The committers listed above are authorized under a signed CLA.

  • ✅ login: mr-c / name: Michael R. Crusoe (2373c29)

@kubernetes-prow
kubernetes-prow Bot requested review from roycaihw and yliaog October 15, 2026 09:16
@kubernetes-prow

Copy link
Copy Markdown
Contributor

Welcome @mr-c!

It looks like this is your first PR to kubernetes-client/python 🎉. Please refer to our pull request process documentation to help your PR have a smooth ride to approval.

You will be prompted by a bot to use commands during the review process. Do not be afraid to follow the prompts! It is okay to experiment. Here is the bot commands documentation.

You can also check if kubernetes-client/python has its own contribution guidelines.

You may want to refer to our testing guide if you run into trouble with your tests not passing.

If you are having difficulty getting your pull request seen, please follow the recommended escalation practices. Also, for tips and tricks in the contribution process you may want to read the Kubernetes contributor cheat sheet. We want to make sure your contribution gets all the attention it needs!

Thank you, and welcome to Kubernetes. 😃

@kubernetes-prow kubernetes-prow Bot added cncf-cla: no Indicates the PR's author has not signed the CNCF CLA. size/M Denotes a PR that changes 30-99 lines, ignoring generated files. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. and removed cncf-cla: no Indicates the PR's author has not signed the CNCF CLA. labels Oct 15, 2026
@yliaog yliaog self-assigned this Oct 22, 2026
@yliaog

yliaog commented Oct 22, 2026

Copy link
Copy Markdown
Contributor

/assign @yliaog

@yliaog

yliaog commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

@mr-c could you please rebase the PR?

@kubernetes-prow

Copy link
Copy Markdown
Contributor

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: mr-c
Once this PR has been reviewed and has the lgtm label, please ask for approval from yliaog. For more information see the Code Review Process.

The full list of commands accepted by this bot can be found here.

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@mr-c
mr-c force-pushed the watch-typing branch 2 times, most recently from 47d1ac2 to c84a3ba Compare November 23, 2026 08:14
@yliaog

yliaog commented Nov 26, 2026

Copy link
Copy Markdown
Contributor

Thanks for working on typing kubernetes/watch/watch.py! A few annotations here will cause false-positive mypy/pyright errors for valid callers and need to be adjusted:

  1. Watch.stream() signature rejects several valid use cases:

    • Pod log streaming (follow=True): CoreV1Api.read_namespaced_pod_log returns str (not ListResponse[_T]), and stream() yields str lines rather than WatchEvent[_T].
    • CustomObjectsApi / DynamicClient: list_namespaced_custom_object and resource.get return object/dict/Any, not ListResponse[_T].
    • deserialize= kwarg: stream() pops deserialize from kwargs, which is not a parameter of func (_P.kwargs), so Watch().stream(..., deserialize=False) will be flagged as an unexpected keyword argument.
    • Adding @overloads (for ListResponse[_T], str log functions, and a fallback Callable[..., Any]) and accounting for deserialize would handle these cleanly.
  2. WatchEvent is missing fields and event types:

    • type should also include "BOOKMARK" (and "ERROR"), since allow_watch_bookmarks=True yields "BOOKMARK" events.
    • raw_object (NotRequired[dict[str, Any]]) is documented in stream()'s docstring and accessed by SharedInformer and user code when deserialize=True.
  3. Watch.__init__(return_type=...) is too narrow:

    • Callers (including watch_test.py, e.g., Watch(float)) can pass a type as well as a str, so this should be return_type: type[Any] | str | None = None.
  4. unmarshal_event() data parameter:

    • data is always a str (raw JSON line), not type[Any] | str.
  5. self._resp: ListResponse[_T] | None = resp:

    • With _preload_content=False, resp is the raw streaming HTTP response (with .stream(), .close(), .release_conn()), not ListResponse[_T].

Minor nits:

  • self.resource_version could be str | int | None = None (or str | None) instead of object | None.
  • Literal['follow'] | Literal['watch'] can be simplified to Literal['follow', 'watch'].

@mr-c

mr-c commented Nov 27, 2026

Copy link
Copy Markdown
Author

Thank you @yliaog for the detailed review; I've marked this PR to allow commits from maintainers, or you can send a PR to my fork targetting the watch-typing branch

@kubernetes-prow kubernetes-prow Bot added the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Nov 30, 2026
TypedDict from typing_extenions is needed for Python3.10 support for
using multiple inheritance with typing.Generic
@mr-c

mr-c commented Dec 1, 2026

Copy link
Copy Markdown
Author

@yliaog I was able to incorporate some of your review comments, but I'm unclear on how to proceed further

@kubernetes-prow kubernetes-prow Bot removed the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Dec 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. kind/cleanup Categorizes issue or PR as related to cleaning up code, process, or technical debt. release-note-none Denotes a PR that doesn't merit a release note. size/M Denotes a PR that changes 30-99 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants