Open rix0rrr opened 3 weeks ago
Also related to this issue:
I'm also still seeing this issue (https://github.com/aws/jsii/issues/2877) in a JetBrains IDE (Intellij IDEA Ultimate with Python plugin). A comment in the issue near the end says it's fixed, but I see the same Expected type 'str | str | None', got '() -> str | str' instead
error in my IDE today using CDK v2.144.0.
These two typing issues combined make Python experience really rough because your IDE thinks you have a lot of invalid types and sometimes its hard to see which typing issue is an actual problem unless you run cdk synth/deploy.
I took a look at apigw-http-api-lambda-dynamodb-python-cdk and was able to root cause at least one issue causing these type checker errors.
When you implement an interface in TS, the method names have to match, but the method argument names do not, as long as the argument types are in the correct order. In Python however, when you implement a Protocol (which is how we do interfaces), the method argument names do have to match. So when our TS library has things like IPrincipal.addToPrincipalPolicy(statement: PolicyStatement)
and an implementation of PrincipalBase.addToPrincipalPolicy(_statement: PolicyStatement)
the former generates an IPrincipal
Protocol in python, and the latter generates a class that's incompatible, and we get the PyRight error:
...
"AnyPrincipal" is incompatible with protocol "IPrincipal"
"IPrincipal" is incompatible with "AnyPrincipal"
"IPrincipal" is incompatible with "AnyPrincipal"
"IPrincipal" is incompatible with "AnyPrincipal"
"add_to_principal_policy" is an incompatible type
The suggested fix here is to always use the method argument names in the base (Protocol) class, when generating subclasses. In the example above, PrincipalBase.addToPrincipalPolicy()
would use the method argument statement
instead of what's in the TS code, _statement
. I didn't get as far as implementation but hopefully this context is useful.
I have this issue too in Intellij IDEA Ultimate w/ Python. Super annoying. I don't get the error in VS Code.
Reported in another issue
Indeed it is still a problem. For example try loading up apigw-http-api-lambda-dynamodb-python-cdk in vscode, then pyright reports for
typecheck error
as shown in this screenshot
That project uses aws-cdk-lib==2.77.0 however the same issue exists after updating to recent aws-cdk-lib==2.143.1. On my machine it happens with the following configuration
Originally posted by @mariogalic in https://github.com/aws/jsii/issues/1919#issuecomment-2142047331