Skip to content

Annotate config and utils public functions - #2826

Open
r4victor wants to merge 2 commits into
kubernetes-client:masterfrom
r4victor:annotate-config-utils
Open

r4victor wants to merge 2 commits into
kubernetes-client:masterfrom
r4victor:annotate-config-utils

Conversation

@r4victor

@r4victor r4victor commented Dec 2, 2026 •

Copy link
Copy Markdown
Contributor

What type of PR is this?

/kind cleanup

What this PR does / why we need it:

The package ships py.typed since 37.0.0, but the hand-written config and utils modules are unannotated, so type checkers see their public functions as returning Any (mypy) or inferred partial types (pyright). This PR adds annotations to them without changing behavior.

Annotated:

  • kubernetes.config and kubernetes.aio.config: load_config, load_kube_config, load_kube_config_from_dict, load_incluster_config, list_kube_config_contexts, new_client_from_config, new_client_from_config_dict, and aio refresh_token.
  • kubernetes.utils and kubernetes.aio.utils: create_from_directory, create_from_yaml, create_from_dict, create_from_yaml_single_item, FailToCreateError, parse_quantity, get_nodes_metrics, get_pods_metrics and get_pods_metrics_in_all_namespaces.

Non-obvious decisions:

  • The sync config_file is str | IO[str] | IO[bytes] | None, since KubeConfigMerger reads text and binary file-like objects; the aio config_file is str | None because the aio merger only accepts paths.
  • load_config keyword arguments are typed with Unpack of a private TypedDict listing the keys it forwards, so misspelled keywords are reported.
  • temp_file_path accepts os.PathLike, since it is only passed to os.makedirs and tempfile.mkstemp.
  • config_dict stays optional where it already defaulted to or accepted None; the loaders raise ConfigException for it at runtime.
  • The aio loaders are typed to return the KubeConfigLoader they already return, which refresh_token takes.
  • The metrics helpers return dicts (dict[str, dict[str, Any]] per namespace for get_pods_metrics_in_all_namespaces) to match their documented shape; the generated custom objects API is typed object only because the endpoint has no response schema.

The annotations follow what the code accepts at runtime rather than narrowing it, so code that works today keeps type-checking. The exceptions are inputs that work only incidentally:

  • parse_quantity accepts str | int | float | Decimal, although any object is converted with str().
  • create_from_yaml(yaml_objects=...) accepts a list, although any non-empty iterable works and None entries are skipped.
  • FailToCreateError takes list[ApiException], although any objects with reason and body work.

Where kubernetes-stubs covers these functions (sync config only), the annotations are consistent with it, with these differences:

  • Parameters missing from the stubs are typed: temp_file_path on load_kube_config, client_configuration on new_client_from_config, and all of new_client_from_config_dict.
  • config_file also accepts file-like objects, and load_kube_config_from_dict accepts None as described above.
  • list_kube_config_contexts returns dict[str, Any] entries instead of the stubs' Context TypedDict; see the follow-ups below.

Which issue(s) this PR fixes:

Part of #2825

Special notes for your reviewer:

Left for follow-ups to keep this PR annotation-only and reviewable:

  • watch is covered by Watch: add typing information #2744.
  • stream, dynamic, informer and leaderelection have return types that depend on arguments (for example _preload_content in stream), which needs overloads and a separate review.
  • list_kube_config_contexts could return TypedDicts for kubeconfig context entries, which needs agreeing on how much of the kubeconfig schema to model.
  • mypy now reports that aio create_from_yaml passes FailToCreateErrors into a new FailToCreateError, whose __str__ then fails with AttributeError; sync collects the underlying ApiExceptions instead. Fixing it changes behavior, so it belongs in its own PR.

Does this PR introduce a user-facing change?

Added type annotations to the public functions in `kubernetes.config`, `kubernetes.aio.config`, `kubernetes.utils` and `kubernetes.aio.utils`.

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


@kubernetes-prow kubernetes-prow Bot added release-note Denotes a PR that will be considered when it comes time to generate release notes. kind/cleanup Categorizes issue or PR as related to cleaning up code, process, or technical debt. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. labels Dec 2, 2026
@kubernetes-prow
kubernetes-prow Bot requested review from roycaihw and yliaog December 2, 2026 10:27
@kubernetes-prow

Copy link
Copy Markdown
Contributor

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: r4victor
Once this PR has been reviewed and has the lgtm label, please assign yliaog for approval. 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

@kubernetes-prow kubernetes-prow Bot added the size/L Denotes a PR that changes 100-499 lines, ignoring generated files. label Dec 2, 2026
@roycaihw

roycaihw commented Dec 3, 2026

Copy link
Copy Markdown
Member

/assign @yliaog

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 Denotes a PR that will be considered when it comes time to generate release notes. size/L Denotes a PR that changes 100-499 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants