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

fix(python): generate type-checking code #1881

Merged
merged 10 commits into from
Aug 21, 2020
4 changes: 2 additions & 2 deletions packages/@jsii/python-runtime/pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
[build-system]
requires = [
"pip~=20.2",
"setuptools~=46.1.3",
"wheel~=0.34.2",
"setuptools~=46.1",
"wheel~=0.34",
]

[tool.black]
Expand Down
27 changes: 10 additions & 17 deletions packages/@jsii/python-runtime/src/jsii/_kernel/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
from jsii import _reference_map
from jsii._utils import Singleton
from jsii._kernel.providers import BaseProvider, ProcessProvider
from jsii._kernel.types import JSClass, Referenceable
from jsii._kernel.types import Callback
from jsii._kernel.types import (
EnumRef,
Expand Down Expand Up @@ -51,10 +50,10 @@ class Object:
__jsii_type__ = "Object"


def _get_overides(klass: JSClass, obj: Any) -> List[Override]:
def _get_overides(klass: Type, obj: Any) -> List[Override]:
overrides: List[Override] = []

# We need to inspect each item in the MRO, until we get to our JSClass, at that
# We need to inspect each item in the MRO, until we get to our Type, at that
# point we'll bail, because those methods are not the overriden methods, but the
# "real" methods.
jsii_classes = [klass] + list(
Expand Down Expand Up @@ -240,9 +239,7 @@ def load(self, name: str, version: str, tarball: str) -> None:
self.provider.load(LoadRequest(name=name, version=version, tarball=tarball))

# TODO: Is there a way to say that obj has to be an instance of klass?
def create(
self, klass: JSClass, obj: Any, args: Optional[List[Any]] = None
) -> ObjRef:
def create(self, klass: Type, obj: Any, args: Optional[List[Any]] = None) -> ObjRef:
if args is None:
args = []

Expand Down Expand Up @@ -271,7 +268,7 @@ def delete(self, ref: ObjRef) -> None:
self.provider.delete(DeleteRequest(objref=ref))

@_dereferenced
def get(self, obj: Referenceable, property: str) -> Any:
def get(self, obj: Any, property: str) -> Any:
response = self.provider.get(
GetRequest(objref=obj.__jsii_ref__, property=property)
)
Expand All @@ -280,7 +277,7 @@ def get(self, obj: Referenceable, property: str) -> Any:
else:
return response.value

def set(self, obj: Referenceable, property: str, value: Any) -> None:
def set(self, obj: Any, property: str, value: Any) -> None:
response = self.provider.set(
SetRequest(
objref=obj.__jsii_ref__,
Expand All @@ -292,12 +289,12 @@ def set(self, obj: Referenceable, property: str, value: Any) -> None:
_callback_till_result(self, response, SetResponse)

@_dereferenced
def sget(self, klass: JSClass, property: str) -> Any:
def sget(self, klass: Type, property: str) -> Any:
return self.provider.sget(
StaticGetRequest(fqn=klass.__jsii_type__, property=property)
).value

def sset(self, klass: JSClass, property: str, value: Any) -> None:
def sset(self, klass: Type, property: str, value: Any) -> None:
self.provider.sset(
StaticSetRequest(
fqn=klass.__jsii_type__,
Expand All @@ -307,9 +304,7 @@ def sset(self, klass: JSClass, property: str, value: Any) -> None:
)

@_dereferenced
def invoke(
self, obj: Referenceable, method: str, args: Optional[List[Any]] = None
) -> Any:
def invoke(self, obj: Any, method: str, args: Optional[List[Any]] = None) -> Any:
if args is None:
args = []

Expand All @@ -327,7 +322,7 @@ def invoke(

@_dereferenced
def sinvoke(
self, klass: JSClass, method: str, args: Optional[List[Any]] = None
self, klass: Type, method: str, args: Optional[List[Any]] = None
) -> Any:
if args is None:
args = []
Expand Down Expand Up @@ -366,9 +361,7 @@ def sync_complete(
response_type=response_type,
)

def ainvoke(
self, obj: Referenceable, method: str, args: Optional[List[Any]] = None
) -> Any:
def ainvoke(self, obj: Any, method: str, args: Optional[List[Any]] = None) -> Any:
if args is None:
args = []

Expand Down
19 changes: 1 addition & 18 deletions packages/@jsii/python-runtime/src/jsii/_kernel/types.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
from typing import Union, List, Any, Optional, Mapping
from typing_extensions import Protocol

import attr

from jsii.compat import Protocol


# TODO:
# - HelloResponse
Expand Down Expand Up @@ -242,19 +241,3 @@ class StatsResponse:
StatsResponse,
Callback,
]


class JSClass(Protocol):
@property
def __jsii_type__(self) -> str:
"""
Returns a str that points to this class inside of the Javascript runtime.
"""


class Referenceable(Protocol):
@property
def __jsii_ref__(self) -> ObjRef:
"""
Returns an ObjRef that points to this object on the JS side.
"""
8 changes: 3 additions & 5 deletions packages/@jsii/python-runtime/src/jsii/_reference_map.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
# This module exists to break an import cycle between jsii.runtime and jsii.kernel
import inspect

from typing import Any, MutableMapping

from ._kernel.types import JSClass, Referenceable
from typing import Any, MutableMapping, Type


_types = {}
Expand All @@ -12,7 +10,7 @@
_interfaces: MutableMapping[str, Any] = {}


def register_type(klass: JSClass):
def register_type(klass: Type):
_types[klass.__jsii_type__] = klass


Expand Down Expand Up @@ -42,7 +40,7 @@ def __init__(self, types):
self._refs = {}
self._types = types

def register(self, inst: Referenceable):
def register(self, inst: Any):
self._refs[inst.__jsii_ref__.ref] = inst

def resolve(self, kernel, ref):
Expand Down
11 changes: 7 additions & 4 deletions packages/@jsii/python-runtime/src/jsii/_runtime.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@

import attr

from jsii import _reference_map
from jsii._compat import importlib_resources
from jsii._kernel import Kernel
from jsii.python import _ClassPropertyMeta
from typing import ClassVar

from . import _reference_map
from ._compat import importlib_resources
from ._kernel import Kernel
from .python import _ClassPropertyMeta
from ._kernel.types import ObjRef


# Yea, a global here is kind of gross, however, there's not really a better way of
Expand Down
2 changes: 2 additions & 0 deletions packages/jsii-pacmak/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ lib/version.ts
.settings
target/

.venv/

*.js
*.d.ts
node_modules/
Expand Down
Loading