fix(jsii): some submodules are not exported from aws-cdk-lib
#3491
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This bug is due to a crazy interaction between the
publication
module, the
__all__
array, and imports we might generate to dotype checking in a package that uses submodules.
Context
Here's what you need to know:
__all__
is normally used to define the symbols and submodulesthat should be imported when a consumer does
from mymod import *
.publication
is used to hide non-public symbols from libraryconsumers. It will move all symbols that are NOT in
__all__
toa
_private
submodule at runtime (it's overloading__all__
alittle here, but this seems generally okay).
imports
at the top of the file for the modules that arereferenced in a module, so that we can use them in type annotations.
Example
This is what the currently generated code for the
aws_cdk
module(of CDK's v2 library) currently (roughly) looks like:
Given this setup, the following happens:
Explanation
Why does this happen?
__all__
does not contain any submodulespublication.publish()
is called, all symbols in the callingnamespace that are missing from
__all__
are hidden.cloud_assembly_schema
(because weimported that submodule to import some symbols for type checking
purposes) but it doesn't include modules that haven't been imported
yet.
publication.publish()
is called,and so are not subject to hiding anymore. This is why we never noticed
this problem before.
second
from . import cloud_assembly_schema
line underneathpublication.publish()
, at that point the line doesn't do anythinganymore.
Solution
Potential solutions are:
__all__
.publication.publish()
.publication
that doesn't hide submodules.This PR picks the first solution, as it seems simplest. The only
downside would have been that
import *
would then start importing allof the CDKv2 library, but that's happening already anyway because of all
the
import
s we generate ourselves.By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.