aws / aws/jsii

Argument re-typing code references possibly undefined imports (Python)

Open
#3,846 0 comments 1 reaction 0 assignees View on GitHub
bug p2
Dominant language
TypeScript
Stars
2.9k
Forks
267
Avg merge
1d 25m
Merged PRs (30d)
14

Description

### Describe the bug

Struct argument dependencies imported from other modules seem to be aliased in the generated Python code, eg.

```python
from ..bundler import ExampleBundlingProps as _ExampleBundlingProps_99400d43
```

If used as (optional) members of other property classes, they are being correctly referenced by alias in constructor parameters:

```python
class ExampleLayerProps(aws_cdk.aws_lambda.LayerVersionOptions):
def __init__(
self,
*,
bundling_props: typing.Optional[_ExampleBundlingProps_99400d43] = None,
) -> None:
```

but the `dict` re-typing code that's inside that same constructor, is using the original name instead of an alias:

```python
if isinstance(bundling_props, dict):
bundling_props = ExampleBundlingProps(**bundling_props)
```

which, once a value is actually passed in, results in an error:

```
NameError: name 'ExampleBundlingProps' is not defined
```

### Expected Behavior

The re-typing code should be aware of import aliasing:

```python
if isinstance(bundling_props, dict):
bundling_props = _ExampleBundlingProps_99400d43(**bundling_props)
```

### Current Behavior

This [bit of code](https://github.com/aws/jsii/blob/e9d4084d06cd2611e4ff25cc7533f823878d1281/packages/jsii-pacmak/lib/targets/python.ts#L1118-L1128) that generates re-typing logic, seems to be unaware of the alias and tries to instantiate the struct by its original name.

### Reproduction Steps

Import a Props struct from a sibling module and use it as an optional parameter in another Props interface, eg.:

```typescript
import { LayerVersionOptions } from "aws-cdk-lib/aws-lambda";
import { Construct } from "constructs";
import { ExampleBundlingProps } from "../bundler";

/**
* Constructor properties for the Lambda layer construct
*/
export interface ExampleLayerProps extends LayerVersionOptions {
/**
* Bundling options for the function.
* @default no bundling props
*/
readonly bundlingProps?: ExampleBundlingProps;
}

/**
* CDK construct for ExampleLayer
*/
export class ExampleLayer extends LayerVersion {
constructor(scope: Construct, id: string, props: ExampleLayerProps) {
...
}
}
```

and inspect the generated code.

### Possible Solution

_No response_

### Additional Information/Context

_No response_

### SDK version used

jsii 1.70.0

### Environment details (OS name and version, etc.)

OS independent

Contributor guide

Open the contributing guide

Research direction

Start in packages/jsii-pacmak/lib/targets/python.ts at the re-typing logic linked in the issue, and reproduce the sibling-module Props case to inspect the generated Python. Trace how the aliased import is represented, then verify that dictionary re-typing uses the same alias and no longer raises the shown NameError.

Written by the indexing model from the issue text.

Assessment

Tech stack
python, typescript
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.