aws / aws/jsii

@jsii.implements should error when class doesn't implement interface

Open
#3,344 1 comment 0 reactions 0 assignees View on GitHub
feature-request language/python module/jsii p1
Dominant language
TypeScript
Stars
2.9k
Forks
267
Avg merge
1d 25m
Merged PRs (30d)
14

Description

## :rocket: Feature Request

### Affected Languages

- [ ] `TypeScript` or `Javascript`
- [x] `Python`
- [ ] `Java`
- [ ] .NET (`C#`, `F#`, ...)
- [ ] `Go`

### Description

`@jsii.implements` marks a class as implementing an interface (maybe?), but it doesn't check for completeness.

Example:

```py
from aws_cdk import(
pipelines,
aws_codepipeline_actions as cpactions,
aws_codepipeline as codepipeline
)
import jsii

@jsii.implements(pipelines.ICodePipelineActionFactory)
class LambdaApprovalStep(pipelines.Step):
def __init__(self, id_, some_lambda):
super().__init__(id_)

@jsii.member(jsii_name="produceAction")
def produce_action(
self, stage: codepipeline.IStage,
options: pipelines.ProduceActionOptions
) -> pipelines.CodePipelineActionFactoryResult:
stage.add_action(
cpactions.LambdaInvokeAction(
lambda_ = some_lambda
)
)
return pipelines.CodePipelineActionFactoryResult(run_orders_consumed=1)
```

This code has a mistake because the `def produce_action` occurs at the wrong level. It's nested inside the constructor, instead of on the class.

`@jsii.implements()` could have noticed the class isn't completely correctly that interface, and thrown an error. That would have allowed catching the error early, and at class definition time, instead of what's happening now:

```
jsii.errors.JSIIError: Deployment step 'Step(SomeLambdaStep)' is not supported for CodePipeline-backed pipelines
```

That error is caused because the Step object fails the following check:

```ts
function isCodePipelineActionFactory(x: any): x is ICodePipelineActionFactory {
return !!(x as ICodePipelineActionFactory).produceAction;
}
```

Which means there is no `produceAction` method on the object... so what is `@jsii.implements` even doing?

### Proposed Solution

Have `@jsii.implements` check members that ought to be implemented and throw an error if they aren't.

Contributor guide

Open the contributing guide

Research direction

Start at the @jsii.implements decorator and use the Python example as the reproduction, tracing how interface members are represented and validated. Done means a class-definition-time error identifies the missing produce_action implementation, while a correctly placed member continues to work.

Written by the indexing model from the issue text.

Assessment

Tech stack
python, typescript
Domain
developer-experience, tooling
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.