Open rgibbard opened 2 years ago
Hey @rgibbard,
Can you share exactly what are you passing into filter_policy
? thanks
@peterwoodworth an instance of S3EventBusFilterPolicy
. It implements Mapping
so according to the type declaration for LambdaSubscription's filter_policy argument it should work.
copy_file_filter = S3EventBusFilterPolicy()
file_copier = S3EventBusObjectCopier(
self,
"TestFileCopier",
topic=some_topic,
filter_policy=copy_file_filter,
target_bucket=some_bucket,
target_prefix="foo"
)
I'll create a simple example here for what you need to input:
policy1 = sns.SubscriptionFilter()
policy2 = sns.SubscriptionFilter()
topic.add_subscription(subs.LambdaSubscription(fn,
filter_policy={
"policy1": policy1,
"policy2": policy2
}
))
S3EventBusFilterPolicy()
should return something in the format of
{
"string": SubscriptionFilter
"string": SubscriptionFilter
...
}
@peterwoodworth yeah that works, but the issue here is really that the type hint for subs.LambdaSubscription
is wrong. It cannot simply take an object that isinstance(obj, typing.Mapping)
. It seems to only work if a built-in dict with type Dict[str, SubscriptionFilter]
is passed like in your example.
.env/lib/python3.8/site-packages/aws_cdk/aws_sns_subscriptions/init.py
@jsii.implements(aws_cdk.aws_sns.ITopicSubscription)
class LambdaSubscription(
metaclass=jsii.JSIIMeta,
jsii_type="@aws-cdk/aws-sns-subscriptions.LambdaSubscription",
):
'''Use a Lambda function as a subscription target.'''
def __init__(
self,
fn: aws_cdk.aws_lambda.IFunction,
*,
dead_letter_queue: typing.Optional[aws_cdk.aws_sqs.IQueue] = None,
filter_policy: typing.Optional[typing.Mapping[builtins.str, aws_cdk.aws_sns.SubscriptionFilter]] = None,
) -> None:
I want to pass an object that implements typing.Mapping
containing Dict[str, SubscriptionFilter]
.
I want to pass an object that implements typing.Mapping containing Dict[str, SubscriptionFilter]
I'm not sure why you would want to do that here. Is there a reason you want to do this instead of passing in a dictionary?
When looking at our docs, it tells us it wants [Mapping[str, SubscriptionFilter]]
My IDE tells me the same thing filter_policy: Mapping[str, SubscriptionFilter]
What exactly is the type hint you're seeing that's causing the confusion? And also where are you seeing it?
@peterwoodworth I want to be able to pass a fully typed object to LambdaSubscription that wraps the SubscriptionFilter
objects, rather than a dictionary of magic string keys and SubscriptionFilter
object values. If says it takes [Mapping[str, SubscriptionFilter]]
then the S3EventBusFilterPolicy
object in my example should satisfy that requirement.
As it stands, the type hint for filter_policy should probably be typing.Optional[typing.Dict[builtins.str, aws_cdk.aws_sns.SubscriptionFilter]]
and not typing.Optional[typing.Mapping[builtins.str, aws_cdk.aws_sns.SubscriptionFilter]]
because if you pass it anything other than a Python built-in dictionary it throws the error in the description.
Edit: In the original example, I left out some additional fields that are present in S3EventBusFilterPolicy
so the object seems a bit useless, but it works as an example.
@RomainMuller your take here would be much appreciated
What is the problem?
Calling' has no attribute '__jsii_type__'
sns.Topic.add_subscription(sns_subscriptions.LambdaSubscription(lambda_, filter_policy=filter_policy))
and passing a filter_policy object that implementsMapping
throws: AttributeError: type object 'Reproduction Steps
What did you expect to happen?
LambdaSusscription
has a constructer argument type hint forfilter_policy
indicating it takes atype.Mapping
but it seems to throw this error if you pass it anything other than a Python built-in dictionary.What actually happened?
File "/.env/lib/python3.8/site-packages/jsii/_kernel/init.py", line 292, in create fqn=klass.jsii_type or "Object", AttributeError: type object 'S3EventBusFilterPolicy' has no attribute 'jsii_type' Subprocess exited with error 1
CDK CLI Version
1.124.0 (build 65761fe)
Framework Version
No response
Node.js Version
7.19.1
OS
MacOS 11.6
Language
Python
Language Version
3.8.9
Other information
No response