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): accept Sequence[T] for array parameters #2606

Merged
merged 5 commits into from
Mar 31, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
15 changes: 12 additions & 3 deletions packages/jsii-pacmak/lib/targets/python.ts
Original file line number Diff line number Diff line change
Expand Up @@ -475,7 +475,10 @@ abstract class BaseMethod implements PythonBase {
liftedPropNames,
);

const paramType = toTypeName(param).pythonType(context);
const paramType = toTypeName(param).pythonType({
...context,
parameterType: true,
});
const paramDefault = param.optional ? ' = None' : '';

pythonParams.push(`${paramName}: ${paramType}${paramDefault}`);
Expand Down Expand Up @@ -507,7 +510,10 @@ abstract class BaseMethod implements PythonBase {
// Iterate over all of our props, and reflect them into our params.
for (const prop of liftedProperties) {
const paramName = toPythonParameterName(prop.name);
const paramType = toTypeName(prop).pythonType(context);
const paramType = toTypeName(prop).pythonType({
...context,
parameterType: true,
});
const paramDefault = prop.optional ? ' = None' : '';

pythonParams.push(`${paramName}: ${paramType}${paramDefault}`);
Expand Down Expand Up @@ -1133,7 +1139,10 @@ class StructField implements PythonBase {

public constructorDecl(context: EmitContext) {
const opt = this.optional ? ' = None' : '';
return `${this.pythonName}: ${this.typeAnnotation(context)}${opt}`;
return `${this.pythonName}: ${this.typeAnnotation({
...context,
parameterType: true,
})}${opt}`;
}

/**
Expand Down
10 changes: 9 additions & 1 deletion packages/jsii-pacmak/lib/targets/python/type-name.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,13 @@ export interface NamingContext {
* or not when emitting type signatures.
*/
readonly emittedTypes: Set<string>;

/**
* Whether the type is emitted for a parameter or not. This may change the
* exact type signature being emitted (e.g: Arrays are typing.Sequence[T] for
* parameters, and typing.List[T] otherwise).
*/
readonly parameterType?: boolean;
}

export function toTypeName(ref?: OptionalValue | TypeReference): TypeName {
Expand Down Expand Up @@ -160,7 +167,8 @@ class List implements TypeName {
}

public pythonType(context: NamingContext) {
return `typing.List[${this.#element.pythonType(context)}]`;
const type = context.parameterType ? 'Sequence' : 'List';
return `typing.${type}[${this.#element.pythonType(context)}]`;
}

public requiredImports(context: NamingContext) {
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/jsii-pacmak/test/generated-code/harness.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ export function verifyGeneratedCodeFor(

expect({ [TREE]: checkTree(outDir) }).toMatchSnapshot('<outDir>/');

if (targetName !== TargetName.PYTHON) {
if (targetName !== TargetName.PYTHON || process.env.SKIP_MYPY_CHECK) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Mypy check adds between 40 and 100 seconds to the execution... Disabling it temporarily can help iterating much faster...

return Promise.resolve();
}
return runMypy(path.join(outDir, targetName));
Expand Down