Skip to content

Commit

Permalink
chore(python): cosmetic improvements on generated code (#1554)
Browse files Browse the repository at this point in the history
Add an explicit `-> None` type annotation to the `__init__` function of struct constructors, to be consistent with the recommendations of [PEP-484](https://www.python.org/dev/peps/pep-0484/#the-meaning-of-annotations).

Consistently put a space before the comma in `typing.Mapping[]` arguments. Previously the space was missing in the case of Maps, but present in the case of JSON objects.

The increased consistency of generated code would facilitate future refactorings, as less special cases have to be accounted for.
  • Loading branch information
RomainMuller committed Apr 16, 2020
1 parent 1d9f734 commit 7de2d00
Show file tree
Hide file tree
Showing 9 changed files with 67 additions and 67 deletions.
4 changes: 2 additions & 2 deletions packages/jsii-pacmak/lib/targets/python.ts
Original file line number Diff line number Diff line change
Expand Up @@ -756,7 +756,7 @@ class Struct extends BasePythonClassType {

const constructorArguments = kwargs.length > 0 ? ['self', '*', ...kwargs] : ['self'];

code.openBlock(`def __init__(${constructorArguments.join(', ')})`);
code.openBlock(`def __init__(${constructorArguments.join(', ')}) -> None`);
this.emitConstructorDocstring(code);

// Re-type struct arguments that were passed as "dict"
Expand Down Expand Up @@ -1610,7 +1610,7 @@ class TypeResolver {
const elementPythonType = this.toPythonType(ref.collection.elementtype, { optional: false });
switch (ref.collection.kind) {
case spec.CollectionKind.Array: return `typing.List[${elementPythonType}]`;
case spec.CollectionKind.Map: return `typing.Mapping[str,${elementPythonType}]`;
case spec.CollectionKind.Map: return `typing.Mapping[str, ${elementPythonType}]`;
default:
throw new Error(`Unsupported collection kind: ${ref.collection.kind}`);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def hey(self) -> jsii.Number:

@jsii.data_type(jsii_type="@scope/jsii-calc-base-of-base.VeryBaseProps", jsii_struct_bases=[], name_mapping={'foo': 'foo'})
class VeryBaseProps():
def __init__(self, *, foo: "Very"):
def __init__(self, *, foo: "Very") -> None:
"""
:param foo: -
"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class _BaseProxy(Base):

@jsii.data_type(jsii_type="@scope/jsii-calc-base.BaseProps", jsii_struct_bases=[scope.jsii_calc_base_of_base.VeryBaseProps], name_mapping={'foo': 'foo', 'bar': 'bar'})
class BaseProps(scope.jsii_calc_base_of_base.VeryBaseProps):
def __init__(self, *, foo: scope.jsii_calc_base_of_base.Very, bar: str):
def __init__(self, *, foo: scope.jsii_calc_base_of_base.Very, bar: str) -> None:
"""
:param foo: -
:param bar: -
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ def baz(self) -> None:

@jsii.data_type(jsii_type="@scope/jsii-calc-lib.MyFirstStruct", jsii_struct_bases=[], name_mapping={'anumber': 'anumber', 'astring': 'astring', 'first_optional': 'firstOptional'})
class MyFirstStruct():
def __init__(self, *, anumber: jsii.Number, astring: str, first_optional: typing.Optional[typing.List[str]]=None):
def __init__(self, *, anumber: jsii.Number, astring: str, first_optional: typing.Optional[typing.List[str]]=None) -> None:
"""This is the first struct we have created in jsii.
:param anumber: An awesome number value.
Expand Down Expand Up @@ -214,7 +214,7 @@ def __repr__(self) -> str:

@jsii.data_type(jsii_type="@scope/jsii-calc-lib.StructWithOnlyOptionals", jsii_struct_bases=[], name_mapping={'optional1': 'optional1', 'optional2': 'optional2', 'optional3': 'optional3'})
class StructWithOnlyOptionals():
def __init__(self, *, optional1: typing.Optional[str]=None, optional2: typing.Optional[jsii.Number]=None, optional3: typing.Optional[bool]=None):
def __init__(self, *, optional1: typing.Optional[str]=None, optional2: typing.Optional[jsii.Number]=None, optional3: typing.Optional[bool]=None) -> None:
"""This is a struct with only optional properties.
:param optional1: The first optional!
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def bar(self, value: typing.Optional[str]):

@jsii.data_type(jsii_type="jsii-calc.InterfaceInNamespaceIncludesClasses.Hello", jsii_struct_bases=[], name_mapping={'foo': 'foo'})
class Hello():
def __init__(self, *, foo: jsii.Number):
def __init__(self, *, foo: jsii.Number) -> None:
"""
:param foo:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

@jsii.data_type(jsii_type="jsii-calc.InterfaceInNamespaceOnlyInterface.Hello", jsii_struct_bases=[], name_mapping={'foo': 'foo'})
class Hello():
def __init__(self, *, foo: jsii.Number):
def __init__(self, *, foo: jsii.Number) -> None:
"""
:param foo:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

@jsii.data_type(jsii_type="jsii-calc.submodule.back_references.MyClassReference", jsii_struct_bases=[], name_mapping={'reference': 'reference'})
class MyClassReference():
def __init__(self, *, reference: jsii_calc.submodule.MyClass):
def __init__(self, *, reference: jsii_calc.submodule.MyClass) -> None:
"""
:param reference:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ class Goodness(enum.Enum):

@jsii.data_type(jsii_type="jsii-calc.submodule.child.Structure", jsii_struct_bases=[], name_mapping={'bool': 'bool'})
class Structure():
def __init__(self, *, bool: bool):
def __init__(self, *, bool: bool) -> None:
"""
:param bool:
Expand Down

0 comments on commit 7de2d00

Please sign in to comment.