Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

IAM: Python show error of type ServicePrincipal #30431

Closed
canle1404 opened this issue Jun 3, 2024 · 4 comments
Closed

IAM: Python show error of type ServicePrincipal #30431

canle1404 opened this issue Jun 3, 2024 · 4 comments
Assignees
Labels
@aws-cdk/aws-iam Related to AWS Identity and Access Management bug This issue is a bug. closed-for-staleness This issue was automatically closed because it hadn't received any attention in a while. p3 response-requested Waiting on additional info and feedback. Will move to "closing-soon" in 7 days.

Comments

@canle1404
Copy link

Describe the bug

I'm using pyright and it shows this error message. However, my stack is still able to be deployed.

Argument of type "ServicePrincipal" cannot be assigned to parameter "assumed_by" of type "IPrincipal" in function "__init__" Pyright (reportArgumentType) [33, 24]
     "ServicePrincipal" is incompatible with protocol "IPrincipal" 
       "IPrincipal" is incompatible with "ServicePrincipal" 
       "IPrincipal" is incompatible with "ServicePrincipal" 
       "IPrincipal" is incompatible with "ServicePrincipal" 
       "add_to_principal_policy" is an incompatible type 
         Type "(_statement: PolicyStatement) -> AddToPrincipalPolicyResult" is incompatible with type "(statement: PolicyStatement) -> AddToPrincipalPolicyResult" 
           Parameter name mismatch: "statement" versus "_statement" 
       "IGrantable" is incompatible with "ServicePrincipal" 

Expected Behavior

No error message

Current Behavior

error message

Reproduction Steps

  • Install pyright
  • Init a Python CDK project
  • Create a role, e.g
from aws_cdk import Stack, aws_iam as iam
from constructs import Construct

class PycdkStack(Stack):

    def __init__(self, scope: Construct, construct_id: str, **kwargs) -> None:
        super().__init__(scope, construct_id, **kwargs)

        role = iam.Role(
            self, "Role", assumed_by=iam.ServicePrincipal("ec2.amazonaws.com")
        )
  • Run pyright ./stack.py

Possible Solution

No response

Additional Information/Context

No response

CDK CLI Version

2.144.0 (build 5fb15bc)

Framework Version

No response

Node.js Version

v20.12.1

OS

Ubuntu 22.04

Language

Python

Language Version

3.10.12

Other information

pyright 1.1.365

@canle1404 canle1404 added bug This issue is a bug. needs-triage This issue or PR still needs to be triaged. labels Jun 3, 2024
@github-actions github-actions bot added the @aws-cdk/aws-iam Related to AWS Identity and Access Management label Jun 3, 2024
@ashishdhingra ashishdhingra self-assigned this Jun 3, 2024
@ashishdhingra ashishdhingra added needs-reproduction This issue needs reproduction. and removed needs-triage This issue or PR still needs to be triaged. labels Jun 3, 2024
@ashishdhingra
Copy link
Contributor

@canle1404 Good morning. The above code synthesizes successfully using cdk synth giving below output:

Resources:
  Role1ABCC5F0:
    Type: AWS::IAM::Role
    Properties:
      AssumeRolePolicyDocument:
        Statement:
          - Action: sts:AssumeRole
            Effect: Allow
            Principal:
              Service: ec2.amazonaws.com
        Version: "2012-10-17"
    Metadata:
      aws:cdk:path: PythonStack/Role/Resource
...
...

Running pyright <<stack.py>> returns the below error:

error: Argument of type "ServicePrincipal" cannot be assigned to parameter "assumed_by" of type "IPrincipal" in function "__init__"
    "ServicePrincipal" is incompatible with protocol "IPrincipal"
      "IPrincipal" is incompatible with "ServicePrincipal"
      "IPrincipal" is incompatible with "ServicePrincipal"
      "IPrincipal" is incompatible with "ServicePrincipal"
      "add_to_principal_policy" is an incompatible type
        Type "(_statement: PolicyStatement) -> AddToPrincipalPolicyResult" is incompatible with type "(statement: PolicyStatement) -> AddToPrincipalPolicyResult"
          Parameter name mismatch: "statement" versus "_statement"
      "IGrantable" is incompatible with "ServicePrincipal" (reportArgumentType)

However,

Per inheritance and polymorphism rules, it should be possible to assign ServicePrincipal concrete type to interface type IPrincipal (not vice versa).

The pyright static type checker is also checking the parameter names like Parameter name mismatch: "statement" versus "_statement". I'm unsure if it is possible to limit the scope of the pyright to the current code stack, rather than installed libraries. Perhaps, you could try opening discussion in https://github.com/microsoft/pyright repository for guidance.

Thanks,
Ashish

@ashishdhingra ashishdhingra added response-requested Waiting on additional info and feedback. Will move to "closing-soon" in 7 days. p3 and removed needs-reproduction This issue needs reproduction. labels Jun 3, 2024
Copy link

github-actions bot commented Jun 5, 2024

This issue has not received a response in a while. If you want to keep this issue open, please leave a comment below and auto-close will be canceled.

@github-actions github-actions bot added the closing-soon This issue will automatically close in 4 days unless further comments are made. label Jun 5, 2024
@github-actions github-actions bot added closed-for-staleness This issue was automatically closed because it hadn't received any attention in a while. and removed closing-soon This issue will automatically close in 4 days unless further comments are made. labels Jun 11, 2024
@shea-parkes
Copy link

I suppose it's too much to ask Amazon devs and Microsoft devs to work together to fix this one? (Amazon devs on this CDK and Microsoft devs on pyright...)

@aws-cdk-automation
Copy link
Collaborator

Comments on closed issues and PRs are hard for our team to see. If you need help, please open a new issue that references this one.

@aws aws locked as resolved and limited conversation to collaborators Jul 25, 2024
# for free to subscribe to this conversation on GitHub. Already have an account? #.
Labels
@aws-cdk/aws-iam Related to AWS Identity and Access Management bug This issue is a bug. closed-for-staleness This issue was automatically closed because it hadn't received any attention in a while. p3 response-requested Waiting on additional info and feedback. Will move to "closing-soon" in 7 days.
Projects
None yet
Development

No branches or pull requests

4 participants