(IAM): Role incorrectly implements interface IRole (Python) #21099
Labels
@aws-cdk/aws-iam
Related to AWS Identity and Access Management
bug
This issue is a bug.
needs-triage
This issue or PR still needs to be triaged.
Describe the bug
The Pylance VSCode type checker is complaining that
iam.Role
does not properly implementiam.IRole
Seems that this has been reported a few times, but brushed off as a version mismatch error: #20278, #16214
Hopefully i can help get to the bottom of this.
Expected Behavior
Code shouldn't throw any type errors, Role should correctly implement IRole.
Current Behavior
The following type error is thrown:
Reproduction Steps
A stack as simple as this will throw the error:
I have created an example repository with the issue:
https://github.com/diesal11/cdk-role-bug-example
You'll need to open in VSCode & install python reqirements to see Pylance linting errors. Note that VSCode needs to be setup to use
.venv
&"python.analysis.typeCheckingMode"
is set to"strict"
insettings.json
Possible Solution
It appears that
Role
does in fact incorrectly implementIRole
. Specifically thegrantPassRole
&grantAssumeRole
methods have the same issue.IRole signature:
aws-cdk/packages/@aws-cdk/aws-iam/lib/role.ts
Lines 620 to 623 in bf35048
Role signature:
aws-cdk/packages/@aws-cdk/aws-iam/lib/role.ts
Lines 505 to 510 in bf35048
As noted in the original error message, the first argument names do not match
identity
vsgrantee
. In other languages like Typescript, this is not an issue as all arguments are positional. But in Python JSII converts these to keyword arguments causing the mismatch.I think the simplest solution is to rename the argument on IRole to
identity
Additional Information/Context
By adding
#type: ignore
to the line & force casting theRole
toIRole
, the stack successfully synthesises. So this seems to be a typing issue only.Also worth noting, seems that if the role is cast to an
IRole
thenrole.grant_pass_role(grantee=iam.ServicePrincipal("batch.amazonaws.com"))
is called.cdk synth
will throw the following, despite Pylance reporting it as valid:CDK CLI Version
2.28.1
Framework Version
2.31.1
Node.js Version
16.9.0
OS
MacOS 12.3.1
Language
Python
Language Version
Python 3.10
The text was updated successfully, but these errors were encountered: