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): reference to type does not use alias #3728

Merged
merged 1 commit into from Aug 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 5 additions & 4 deletions packages/jsii-pacmak/lib/targets/python.ts
Expand Up @@ -593,6 +593,7 @@ abstract class BaseMethod implements PythonBase {
const paramType = toTypeName(prop.prop).pythonType({
...context,
parameterType: true,
typeAnnotation: true,
});
const paramDefault = prop.prop.optional ? ' = None' : '';

Expand Down Expand Up @@ -1133,10 +1134,10 @@ class Struct extends BasePythonClassType {
// Re-type struct arguments that were passed as "dict". Do this before validating argument types...
for (const member of members.filter((m) => m.isStruct(this.generator))) {
// Note that "None" is NOT an instance of dict (that's convenient!)
const typeName = toPythonFullName(
(member.type.type as spec.NamedTypeReference).fqn,
context.assembly,
);
const typeName = toTypeName(member.type.type).pythonType({
...context,
typeAnnotation: false,
});
code.openBlock(`if isinstance(${member.pythonName}, dict)`);
code.line(`${member.pythonName} = ${typeName}(**${member.pythonName})`);
code.closeBlock();
Expand Down
6 changes: 3 additions & 3 deletions packages/jsii-pacmak/lib/targets/python/type-name.ts
Expand Up @@ -335,7 +335,7 @@ class UserType implements TypeName {
// Identify declarations that are not yet initialized and hence cannot be
// used as part of a type qualification. Since this is not a forward
// reference, the type was already emitted and its un-qualified name must
// be used instead of it's locally qualified name.
// be used instead of its locally qualified name.
const nestingParent = surroundingTypeFqns
?.map((fqn) => toPythonFqn(fqn, assembly).pythonFqn)
?.reverse()
Expand All @@ -357,7 +357,7 @@ class UserType implements TypeName {
// This is not for a type annotation, so we should be at a point in time
// where the surrounding symbol has been defined entirely, so we can
// refer to it "normally" now.
return { pythonType: pythonFqn.substring(nestingParent.length + 1) };
return { pythonType: pythonFqn.slice(packageName.length + 1) };
}

// We'll just make a module-qualified reference at this point.
Expand All @@ -380,7 +380,7 @@ class UserType implements TypeName {
const alias = `_${toImport}_${aliasSuffix}`;

return {
pythonType: [alias, ...nested].join('.'),
pythonType: wrapType([alias, ...nested].join('.')),
requiredImport: {
sourcePackage: relativeImportPath(
submodulePythonName,
Expand Down