Skip to content

Commit

Permalink
fix(pyhon): under-qualified types used by dynamic type checking (#3688)
Browse files Browse the repository at this point in the history
The types must be referenced from the current module's root, including
nesting class names, or the names will not be defined.
  • Loading branch information
RomainMuller authored Jul 29, 2022
1 parent f372e93 commit 833a717
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 20 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import pytest
import re

from scope.jsii_calc_lib.custom_submodule_name import NestingClass
import jsii_calc


Expand Down Expand Up @@ -114,3 +115,7 @@ def test_setter_to_union(self):
),
):
subject.union_property = jsii_calc.StringEnum.B # type:ignore

def test_nested_struct(self):
# None of these should throw...
NestingClass.NestedStruct(name="Queen B")
28 changes: 13 additions & 15 deletions packages/jsii-pacmak/lib/targets/python.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {
PythonImports,
mergePythonImports,
toPackageName,
toPythonFullName,
} from './python/type-name';
import { die, toPythonIdentifier } from './python/util';
import { toPythonVersionRange, toReleaseVersion } from './version-utils';
Expand Down Expand Up @@ -466,7 +467,6 @@ abstract class BaseMethod implements PythonBase {
private readonly returns: spec.OptionalValue | undefined,
public readonly docs: spec.Docs | undefined,
public readonly isStatic: boolean,
private readonly pythonParent: PythonType,
opts: BaseMethodOpts,
) {
this.abstract = !!opts.abstract;
Expand Down Expand Up @@ -674,7 +674,9 @@ abstract class BaseMethod implements PythonBase {
emitParameterTypeChecks(
code,
pythonParams.slice(1),
`${this.pythonParent.pythonName}.${this.pythonName}`,
`${toPythonFullName(this.parent.fqn, context.assembly)}.${
this.pythonName
}`,
);
}
this.emitBody(
Expand Down Expand Up @@ -867,7 +869,6 @@ abstract class BaseProperty implements PythonBase {
private readonly jsName: string,
private readonly type: spec.OptionalValue,
public readonly docs: spec.Docs | undefined,
private readonly pythonParent: PythonType,
opts: BasePropertyOpts,
) {
const { abstract = false, immutable = false, isStatic = false } = opts;
Expand Down Expand Up @@ -952,9 +953,10 @@ abstract class BaseProperty implements PythonBase {
// In order to get a property accessor, we must resort to getting the
// attribute on the type, instead of the value (where the getter would
// be implicitly invoked for us...)
`getattr(${this.pythonParent.pythonName}, ${JSON.stringify(
this.pythonName,
)}).fset`,
`getattr(${toPythonFullName(
this.parent.fqn,
context.assembly,
)}, ${JSON.stringify(this.pythonName)}).fset`,
);
code.line(
`jsii.${this.jsiiSetMethod}(${this.implicitParameter}, "${this.jsName}", value)`,
Expand Down Expand Up @@ -1141,7 +1143,11 @@ class Struct extends BasePythonClassType {
code.line(`${member.pythonName} = ${typeName}(**${member.pythonName})`);
code.closeBlock();
}
emitParameterTypeChecks(code, kwargs, `${this.pythonName}.__init__`);
emitParameterTypeChecks(
code,
kwargs,
`${toPythonFullName(this.spec.fqn, context.assembly)}.__init__`,
);

// Required properties, those will always be put into the dict
assignDictionary(
Expand Down Expand Up @@ -2605,7 +2611,6 @@ class PythonGenerator extends Generator {
undefined,
cls.initializer.docs,
false, // Never static
klass,
{ liftedProp: this.getliftedProp(cls.initializer), parent: cls },
),
);
Expand All @@ -2628,7 +2633,6 @@ class PythonGenerator extends Generator {
method.returns,
method.docs,
true, // Always static
klass,
{
abstract: method.abstract,
liftedProp: this.getliftedProp(method),
Expand All @@ -2647,7 +2651,6 @@ class PythonGenerator extends Generator {
prop.name,
prop,
prop.docs,
klass,
{
abstract: prop.abstract,
immutable: prop.immutable,
Expand All @@ -2673,7 +2676,6 @@ class PythonGenerator extends Generator {
method.returns,
method.docs,
!!method.static,
klass,
{
abstract: method.abstract,
liftedProp: this.getliftedProp(method),
Expand All @@ -2691,7 +2693,6 @@ class PythonGenerator extends Generator {
method.returns,
method.docs,
!!method.static,
klass,
{
abstract: method.abstract,
liftedProp: this.getliftedProp(method),
Expand All @@ -2711,7 +2712,6 @@ class PythonGenerator extends Generator {
prop.name,
prop,
prop.docs,
klass,
{
abstract: prop.abstract,
immutable: prop.immutable,
Expand Down Expand Up @@ -2773,7 +2773,6 @@ class PythonGenerator extends Generator {
method.returns,
method.docs,
!!method.static,
klass,
{ liftedProp: this.getliftedProp(method), parent: ifc },
),
);
Expand All @@ -2793,7 +2792,6 @@ class PythonGenerator extends Generator {
prop.name,
prop,
prop.docs,
klass,
{ immutable: prop.immutable, isStatic: prop.static, parent: ifc },
);
}
Expand Down
14 changes: 14 additions & 0 deletions packages/jsii-pacmak/lib/targets/python/type-name.ts
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,20 @@ export function toPythonFqn(fqn: string, rootAssm: Assembly) {
return { assemblyName, packageName, pythonFqn: fqnParts.join('.') };
}

/**
* Computes the nesting-qualified name of a type.
*
* @param fqn the fully qualified jsii name of the type.
* @param rootAssm the root assembly for the project.
*
* @returns the nesting-qualified python type name (the name of the class,
* qualified with all nesting parent classes).
*/
export function toPythonFullName(fqn: string, rootAssm: Assembly): string {
const { packageName, pythonFqn } = toPythonFqn(fqn, rootAssm);
return pythonFqn.slice(packageName.length + 1);
}

/**
* Computes the python relative import path from `fromModule` to `toModule`.
*
Expand Down

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

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

3 changes: 2 additions & 1 deletion packages/jsii-rosetta/jest.config.mjs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { createRequire } from 'node:module';
import { overriddenConfig } from '../../jest.config.mjs';
import { default as defaultConfig, overriddenConfig } from '../../jest.config.mjs';

export default overriddenConfig({
setupFiles: [createRequire(import.meta.url).resolve('./jestsetup.js')],
testTimeout: process.env.CI ? 30_000 : defaultConfig.testTimeout,
});

0 comments on commit 833a717

Please sign in to comment.