-
Notifications
You must be signed in to change notification settings - Fork 246
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(python): unknown type when submodule is not loaded (#3049)
Callbacks may make references to types from submodules of loaded packages, however due to how the type registration works in the Python runtime, if the submodule has not been loaded, then the types it contains are not registered and will fail resolving. The simplest possible fix is to preemtively load all submodules upfront at the end of the root module's `__init__.py` file, which guarantees that all types have correctly been registered in the runtime (at the expense of some time and memory). Fixes aws/aws-cdk#16625
- Loading branch information
Romain Marcadier
authored
Oct 11, 2021
1 parent
72b9b98
commit da55a1e
Showing
19 changed files
with
988 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
packages/@jsii/go-runtime-test/project/internal/cdk16625/cdk16625.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package cdk16625 | ||
|
||
import ( | ||
"github.com/aws/jsii/jsii-calc/go/jsiicalc/v3" | ||
abc "github.com/aws/jsii/jsii-calc/go/jsiicalc/v3/cdk16625" | ||
) | ||
|
||
func New() abc.Cdk16625 { | ||
c := &cdk16625{} | ||
abc.NewCdk16625_Override(c) | ||
return c | ||
} | ||
|
||
type cdk16625 struct{ | ||
abc.Cdk16625 | ||
} | ||
|
||
func (c *cdk16625) Unwrap(rng jsiicalc.IRandomNumberGenerator) *float64 { | ||
return rng.Next() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import type { IRandomNumberGenerator } from '../../calculator'; | ||
|
||
/** | ||
* This type demonstrates the ability to receive a callback argument that has a | ||
* type from a submodule not explicitly imported in the user's code. This checks | ||
* that all types available in the assembly can be resolved by the runtime | ||
* library, regardless of whether they were explicitly referenced or not. | ||
* | ||
* @see https://github.com/aws/aws-cdk/issues/16625 | ||
*/ | ||
export class UnimportedSubmoduleType implements IRandomNumberGenerator { | ||
public constructor(private readonly value: number) {} | ||
|
||
/** | ||
* Not quite random, but it'll do. | ||
* | ||
* @returns 1337 | ||
*/ | ||
public next(): number { | ||
return this.value; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import * as assert from 'assert'; | ||
|
||
import { IRandomNumberGenerator } from '../calculator'; | ||
import { UnimportedSubmoduleType } from './donotimport'; | ||
|
||
export * as donotimport from './donotimport'; | ||
|
||
export abstract class Cdk16625 { | ||
/** | ||
* Implement this functin to return `gen.next()`. It is extremely important | ||
* that the `donotimport` submodule is NEVER explicitly loaded in the testing | ||
* application (otherwise this test is void). | ||
* | ||
* @param gen a VERY pseudo random number generator. | ||
*/ | ||
protected abstract unwrap(gen: IRandomNumberGenerator): number; | ||
|
||
/** | ||
* Run this function to verify that everything is working as it should. | ||
*/ | ||
public test(): void { | ||
const value = 1337; | ||
const rng = new UnimportedSubmoduleType(value); | ||
assert(this.unwrap(rng) === value); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.