From c4cffa30f1d3c34346fc313a6a4e8bffca36d6a4 Mon Sep 17 00:00:00 2001 From: Giuseppe Tribulato Date: Tue, 6 Feb 2024 12:29:35 +0100 Subject: [PATCH] Fix autocompletion support for VSCode --- .github/workflows/python-package.yml | 4 ++-- lightkube/core/dataclasses_dict.py | 14 +++++++++++++- lightkube/core/schema.py | 8 ++++++++ setup.py | 2 +- 4 files changed, 24 insertions(+), 4 deletions(-) create mode 100644 lightkube/core/schema.py diff --git a/.github/workflows/python-package.yml b/.github/workflows/python-package.yml index a4e4f8a..ec0f906 100644 --- a/.github/workflows/python-package.yml +++ b/.github/workflows/python-package.yml @@ -54,7 +54,7 @@ jobs: strategy: fail-fast: false matrix: - k8s: [ 1.23, 1.27 ] + k8s: [ '1.23', '1.27' ] name: E2E test in K8s ${{ matrix.k8s }} runs-on: ubuntu-20.04 steps: @@ -62,7 +62,7 @@ jobs: - name: Set up Python uses: actions/setup-python@v2 with: - python-version: 3.9 + python-version: '3.9' - name: Install dependencies run: | python -m pip install --upgrade pip diff --git a/lightkube/core/dataclasses_dict.py b/lightkube/core/dataclasses_dict.py index 996ba89..f66e375 100644 --- a/lightkube/core/dataclasses_dict.py +++ b/lightkube/core/dataclasses_dict.py @@ -1,6 +1,6 @@ import sys import typing -from typing import get_type_hints +from typing import Union from datetime import datetime import dataclasses as dc @@ -51,6 +51,18 @@ def nohop(x, kw): def is_dataclass_json(cls): return dc.is_dataclass(cls) and issubclass(cls, DataclassDictMixIn) +NoneType = type(None) + +def _remove_optional(tp): + if get_origin(tp) is Union: + args = get_args(tp) + if args[1] is NoneType: + return args[0] + return tp + +def get_type_hints(cl): + types = typing.get_type_hints(cl) + return {k: _remove_optional(v) for k, v in types.items()} def extract_types(cls, is_to=True): func_name = "to_json_type" if is_to else "from_json_type" diff --git a/lightkube/core/schema.py b/lightkube/core/schema.py new file mode 100644 index 0000000..a260518 --- /dev/null +++ b/lightkube/core/schema.py @@ -0,0 +1,8 @@ +""" +This module exposes dependencies used by lightkube-models + +These dependencies are here because we may decide to replace dataclasses with something else in the future +""" +from dataclasses import dataclass, field + +from .dataclasses_dict import DataclassDictMixIn as DictMixin diff --git a/setup.py b/setup.py index d41e75a..f24fa64 100644 --- a/setup.py +++ b/setup.py @@ -3,7 +3,7 @@ setup( name='lightkube', - version="0.15.0", + version="0.15.1", description='Lightweight kubernetes client library', long_description=Path("README.md").read_text(), long_description_content_type="text/markdown",