Skip to content

Commit

Permalink
chore(release): 1.63.1 (#3684)
Browse files Browse the repository at this point in the history
  • Loading branch information
RomainMuller authored Jul 27, 2022
2 parents 7c24e36 + 44a97d1 commit 185bb34
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 28 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@

All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.

## [1.63.1](https://github.com/aws/jsii/compare/v1.63.0...v1.63.1) (2022-07-26)


### Features

* **python:** formally allow dicts to be passed in lieu of structs ([#3683](https://github.com/aws/jsii/pull/3683))

## [1.63.0](https://github.com/aws/jsii/compare/v1.62.0...v1.63.0) (2022-07-26)


Expand Down
2 changes: 1 addition & 1 deletion lerna.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@
"rejectCycles": true
}
},
"version": "1.63.0"
"version": "1.63.1"
}
1 change: 1 addition & 0 deletions packages/jsii-pacmak/lib/targets/python.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2530,6 +2530,7 @@ class PythonGenerator extends Generator {
emittedTypes: new Set(),
resolver,
submodule: assm.name,
typeResolver: (fqn) => resolver.dereference(fqn),
});
}

Expand Down
27 changes: 23 additions & 4 deletions packages/jsii-pacmak/lib/targets/python/type-name.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
PrimitiveTypeReference,
isUnionTypeReference,
Type,
isInterfaceType,
} from '@jsii/spec';
import { toSnakeCase } from 'codemaker';
import { createHash } from 'crypto';
Expand Down Expand Up @@ -37,6 +38,9 @@ export interface NamingContext {
/** The assembly in which the PythonType is expressed. */
readonly assembly: Assembly;

/** A resolver to obtain complete information about a type. */
readonly typeResolver: (fqn: string) => Type;

/** The submodule of the assembly in which the PythonType is expressed (could be the module root) */
readonly submodule: string;

Expand Down Expand Up @@ -293,14 +297,27 @@ class UserType implements TypeName {
submodule,
surroundingTypeFqns,
typeAnnotation = true,
parameterType,
typeResolver,
}: NamingContext) {
const { assemblyName, packageName, pythonFqn } = toPythonFqn(
this.#fqn,
assembly,
);

// If this is a type annotation for a parameter, allow dicts to be passed where structs are expected.
const type = typeResolver(this.#fqn);
const isStruct = isInterfaceType(type) && !!type.datatype;
const wrapType =
typeAnnotation && parameterType && isStruct
? (pyType: string) =>
`typing.Union[${pyType}, typing.Dict[str, typing.Any]]`
: (pyType: string) => pyType;

if (assemblyName !== assembly.name) {
return {
pythonType: pythonFqn,
// If it's a struct, then we allow passing as a dict, too...
pythonType: wrapType(pythonFqn),
requiredImport: {
sourcePackage: packageName,
item: '',
Expand Down Expand Up @@ -330,8 +347,8 @@ class UserType implements TypeName {
) {
// Possibly a forward reference, outputting the stringifierd python FQN
return {
pythonType: JSON.stringify(
pythonFqn.substring(submodulePythonName.length + 1),
pythonType: wrapType(
JSON.stringify(pythonFqn.substring(submodulePythonName.length + 1)),
),
};
}
Expand All @@ -345,7 +362,9 @@ class UserType implements TypeName {

// We'll just make a module-qualified reference at this point.
return {
pythonType: pythonFqn.substring(submodulePythonName.length + 1),
pythonType: wrapType(
pythonFqn.substring(submodulePythonName.length + 1),
),
};
}

Expand Down

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

32 changes: 32 additions & 0 deletions packages/jsii-pacmak/test/targets/python/type-name.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import {
NamedTypeReference,
PrimitiveType,
SchemaVersion,
Type,
TypeKind,
TypeReference,
} from '@jsii/spec';

Expand Down Expand Up @@ -57,6 +59,11 @@ const assembly: Assembly = {
fqn: `@foo/bar.other.${OTHER_SUBMODULE_TYPE}`,
namespace: 'other',
},
[`@foo/bar.Struct`]: {
datatype: true,
fqn: `@foo/bar.Struct`,
kind: TypeKind.Interface,
},
},
} as any;

Expand All @@ -78,6 +85,15 @@ describe(toTypeName, () => {
readonly inSubmodule?: string;
/** The nesting context in which to generate names (if not provided, none) */
readonly inNestingContext?: readonly string[];
/** Additional context keys to register */
readonly context?: Omit<
NamingContext,
| 'assembly'
| 'emittedTypes'
| 'surroundingTypeFqns'
| 'submodule'
| 'typeResolver'
>;
};

const examples: readonly Example[] = [
Expand Down Expand Up @@ -221,14 +237,30 @@ describe(toTypeName, () => {
},
inSubmodule: `${assembly.name}.submodule`,
},
// ############################# SPECIAL CASES##############################
{
name: 'Struct parameter type annotation',
input: { fqn: `${assembly.name}.Struct` },
forwardPythonType: `typing.Union["Struct", typing.Dict[str, typing.Any]]`,
pythonType: `typing.Union[Struct, typing.Dict[str, typing.Any]]`,
context: {
typeAnnotation: true,
parameterType: true,
},
},
];

for (const example of examples) {
const context: NamingContext = {
...example.context,
assembly,
emittedTypes: new Set(),
surroundingTypeFqns: example.inNestingContext,
submodule: example.inSubmodule ?? assembly.name,
typeResolver: (fqn) => {
const type = assembly.types?.[fqn];
return type ?? ({ fqn } as any as Type);
},
};
const contextWithEmittedType: NamingContext = {
...context,
Expand Down

0 comments on commit 185bb34

Please sign in to comment.