Skip to content
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

experiment with plugin typing #8433

Draft
wants to merge 3 commits 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
56 changes: 56 additions & 0 deletions src/inmanta/execute/proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -270,3 +270,59 @@ def __iter__(self):
def __next__(self):
i = self._get_instance()
return DynamicProxy.return_value(next(i))


# TODO: pick proper place for this. Might be inmanta.plugins or simply higher up in this file
import typing
import inmanta.ast.type
from inmanta import plugin_typing


# general idea for approach: try to evaluate (typing.get_type_hints equivalent) args and pass to to_dsl_type(). If it fails,
# call parse_dsl_type() instead.


# TODO: name
def parse_dsl_type(dsl_type: str) -> inmanta.ast.type.Type:
"""
Parse a dsl type expression into the corresponding Type object.

Used for the legacy plugin annotations, as well as native Python annotations with typing.Annotated[..., InmantaType(...)]
for complex types that are not (yet) natively supported on the plugin interface.
"""


# TODO: remove comment
def to_dsl_type(python_type: type[object]) -> inmanta.ast.type.Type:
"""
:param python_type: The evaluated python type as provided in the Python type annotation.
"""
# TODO: support implicit Any for each of these typing.get_args
if typing.get_origin(python_type)_is typing.Union:
bases: Sequence[inmanta.ast.type.Type] = [to_dsl_type(arg) for arg in typing.get_args(python_type)]
# TODO: make NullableType if any is Optional
return inmanta.ast.type.Union(bases)
if typing.get_origin(t) is Sequence:
# TODO: more robust implementation
base: inmanta.ast.type.Type = typing.get_args(python_type)[0]
return inmanta.ast.type.TypedList(base)
if typing.get_origin(t) is typing.Annotated:
args: Sequence[object] = typing.get_args(python_type)
inmanta_types: Sequence[plugin_typing.InmantaType] = [arg if isinstance(arg, plugin_typing.InmantaType) for arg in args]
if inmanta_types:
if len(inmanta_types) > 1:
# TODO
raise Exception()
# TODO
return parse_dsl_type(inmanta_types[0].dsl_type)
# the annotation doesn't concern us => use base type
return to_dsl_type(args[0])
primitive_mapping: Mapping[type, str] = {
int: "int",
float: "float",
str: "string",
bool: "bool",
}
if python_type in primitive_mapping:
return inmanta.ast.type.TYPES[primitive_mapping[python_type]]
# TODO: continue implementation
26 changes: 26 additions & 0 deletions src/inmanta/plugin_typing.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import dataclasses
import typing
from dataclasses import dataclass

# TODO: move this module to inmanta.plugins.typing? Probably not because that would import the whole `plugins` namespace?


@dataclass(frozen=True)
class InmantaType:
"""
Declaration of an inmanta type for use with typing.Annotated.

When a plugin type is declared as typing.Annotated with an `InmantaType` as annotation, the Python type is completely
ignored for type validation and conversion to and from the DSL. Instead the string provided to the `InmantaType` is
evaluated as a DSL type, extended with "any".

For maximum static type coverage, it is recommended to use these only when absolutely necessary, and to use them as deeply
in the type as possible, e.g. prefer `Sequence[Annotated[MyEntity, InmantaType("std::Entity")]]` over
`Annotated[Sequence[Entity], InmantaType("std::Entity[]")]`.
"""
dsl_type: str


# TODO: how to do Entity? "object" is appropriate but raises too many errors for practical use. Any is Any
Entity: typing.TypeAlias = typing.Annotated[object, InmantaType("std::Entity")]
string: typing.TypeAlias = typing.Annotated[str, InmantaType("string")]