aws / aws/jsii

(python): mypy complains about class not implementing optional interface member

Open
#3,555 0 comments 2 reactions 0 assignees View on GitHub
bug language/python module/compiler p2
Dominant language
TypeScript
Stars
2.9k
Forks
267
Avg merge
1d 25m
Merged PRs (30d)
14

Description

### Describe the bug

(Originally reported in https://github.com/aws/aws-cdk/issues/20219)

The following is valid in TypeScript/JavaScript (and by extension and no definition to the contrary, in jsii):

```ts
interface MyInterface {
readonly optional?: string;
}

class MyClass implements MyInterface {
}
```

Even though `MyClass` doesn't have an implementation for `optional`, the above is allowed by structural typing. Whether or not `MyClass` has `optional` explicitly defined, in both cases:

```ts
const myObj = new MyClass();
console.log(myObj.optional); // <-- undefined
```

However, when these types are translated to Python, mypy disagrees with the missing definition on `MyClass`:

```
monitoring/ses_stack.py: note: In member "__init__" of class "SESStack":
monitoring/ses_stack.py:146:19: error: List item 0 has incompatible type "Metric"; expected "IMetric" [list-item]
monitoring/ses_stack.py:146:19: note: "Metric" is missing following "IMetric" protocol member:
monitoring/ses_stack.py:146:19: note: warnings
```

### Expected Behavior

No errors produces by mypy

### Current Behavior

Mypy complains

### Reproduction Steps

See example above

### Possible Solution

Solution: also generate definitions for inherited interface members on a class.

Future mitigation: run `mypy` on all the Python code in the compliance tests.

### Additional Information/Context

_No response_

### SDK version used

(any)

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

(any)

Contributor guide

Open the contributing guide

Research direction

Start with the Python type-generation path that translates TypeScript interfaces and classes, using the provided MyClass/IMetric example as the reproduction. Check the generated Python definitions and the compliance tests; done means mypy accepts a class that omits an optional inherited interface member.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.