Skip to content

WIP: Dynamic hook for CustomResourceDict #13

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 38 additions & 1 deletion crd_typed/__init__.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,45 @@
from typing import Any, Dict
from typing import Any, Callable, Dict, List

__all__ = ["CustomResource", "CustomResourceDict"]

_U = TypeVar("_U")


class CustomResource(Dict[Any, Any]):
"""Placeholder for Custom Resource body TypedDict."""

def __class_getitem__(cls: Any, item: Any) -> Any:
return dict


def CustomResourceDict(
name: str, definition_path: str, property_path: List[str], group: str, version: str, kind: str
) -> Callable[[_U], _U]:
"""
CustomResourceDict creates TypedDict type representing Custom Resource

For example:
from crd_typed import CustomResourceDict

MyResource = CustomResourceDict(
"MyResource"
definition_path = "/path/to/crd.yaml",
property_path = ["spec", "some_property"]
group = "myapi.io",
version = "v1",
kind = "MyResource"
)
"""

def new_type(x: _U):
return x

new_type.__name__ = str(name)
new_type.__supertype__ = Dict[Any, Any]
new_type._definition_path = definition_path # type: ignore
new_type._property_path = property_path # type: ignore
new_type._group = group # type: ignore
new_type._version = version # type: ignore
new_type._kind = kind # type: ignore

return new_type
21 changes: 19 additions & 2 deletions crd_typed/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

import yaml
from jsonschema_typed.plugin import APIv4, TypeMaker
from mypy.nodes import MypyFile
from mypy.plugin import AnalyzeTypeContext, Plugin
from mypy.nodes import GDEF, MypyFile, SymbolTableNode
from mypy.plugin import AnalyzeTypeContext, DynamicClassDefContext, Plugin
from mypy.types import AnyType, RawExpressionType, Type, TypeOfAny, UnboundType


Expand All @@ -12,6 +12,11 @@ class CRDPlugin(Plugin):

CustomResource = "crd_typed.CustomResource"

def get_dynamic_class_hook(self, fullname: str) -> Optional[Callable[[DynamicClassDefContext], None]]:
if fullname == self.CustomResource:
return custom_resource_dict_callback
return None

def get_type_analyze_hook(self, fullname: str) -> Optional[Callable[[AnalyzeTypeContext], Type]]:
if fullname == self.CustomResource:
return custom_resource_callback
Expand All @@ -23,6 +28,18 @@ def get_additional_deps(self, file: MypyFile) -> List[Tuple[int, str, int]]:
return [(10, "mypy_extensions", -1)]


def custom_resource_dict_callback(ctx: DynamicClassDefContext) -> None:
pass

# api = APIv4()

# create our own AnalyzeTypeContext from DynamicClassDefContext and pass to TypeMaker

# use resulting type to create TypeInfo.typeddict_type and pass it to table

# ctx.api.add_symbol_table_node(ctx.name, SymbolTableNode(GDEF, info))


def custom_resource_callback(ctx: AnalyzeTypeContext) -> Type:
if not ctx.type.args:
return ctx.type
Expand Down