gtsystem / lightkube

Modern lightweight kubernetes module for python
https://lightkube.readthedocs.io
MIT License
96 stars 11 forks source link

Pyright struggles with typing #63

Closed marcoppenheimer closed 5 days ago

marcoppenheimer commented 3 weeks ago

Issue

I've had a lot of issues with the typing inference with pyright. I want to confirm I'm not 'doing it wrong'. How can I type-hint Pod from Client.get(), and not the generic NamespacedResource or GlobalResource?

Example File

"""Docstring."""

from functools import cached_property

from lightkube.config.kubeconfig import DEFAULT_KUBECONFIG, KubeConfig
from lightkube.core.client import Client
from lightkube.resources.core_v1 import Pod

class K8s:
    """Docstring."""

    def __init__(self):
        pass

    @cached_property
    def client(self) -> Client:
        """Docstring."""
        return Client(
            field_manager="app",
            namespace="namespace",
            config=KubeConfig.from_env(DEFAULT_KUBECONFIG),
        )

    @property
    def pod(self) -> Pod:
        """Docstring."""
        return self.client.get(
            res=Pod,
            name="pod",
        )

Then you can run pyright --verbose file.py and you'll get:

/home/marc/file.py
  /home/marc/file.py:28:16 - error: Expression of type "NamespacedResource" is incompatible with return type "Pod"
    "NamespacedResource" is incompatible with "Pod" (reportReturnType)
gtsystem commented 2 weeks ago

Hi, I hope this is fixed in lightkube==0.15.3. Can you check if everything is fine?