1
1
""" SAM macro definitions """
2
2
import copy
3
- from typing import Any , cast , Dict , List , Optional , Tuple , Union
3
+ from typing import Any , cast , Dict , List , Optional , Tuple , Union , Callable
4
4
from samtranslator .intrinsics .resolver import IntrinsicsResolver
5
5
from samtranslator .feature_toggle .feature_toggle import FeatureToggle
6
6
from samtranslator .model .connector .connector import (
@@ -167,7 +167,7 @@ class SamFunction(SamResourceMacro):
167
167
SnapStart : Optional [Dict [str , Any ]]
168
168
FunctionUrlConfig : Optional [Dict [str , Any ]]
169
169
170
- event_resolver = ResourceTypeResolver ( # type: ignore[no-untyped-call]
170
+ event_resolver = ResourceTypeResolver (
171
171
samtranslator .model .eventsources ,
172
172
samtranslator .model .eventsources .pull ,
173
173
samtranslator .model .eventsources .push ,
@@ -191,9 +191,9 @@ class SamFunction(SamResourceMacro):
191
191
"DestinationQueue" : SQSQueue .resource_type ,
192
192
}
193
193
194
- def resources_to_link (self , resources ): # type: ignore[no-untyped-def]
194
+ def resources_to_link (self , resources : Dict [ str , Any ]) -> Dict [ str , Any ]:
195
195
try :
196
- return {"event_resources" : self ._event_resources_to_link (resources )} # type: ignore[no-untyped-call]
196
+ return {"event_resources" : self ._event_resources_to_link (resources )}
197
197
except InvalidEventException as e :
198
198
raise InvalidResourceException (self .logical_id , e .message )
199
199
@@ -260,7 +260,7 @@ def to_cloudformation(self, **kwargs): # type: ignore[no-untyped-def]
260
260
feature_toggle ,
261
261
)
262
262
263
- event_invoke_policies = []
263
+ event_invoke_policies : List [ Dict [ str , Any ]] = []
264
264
if self .EventInvokeConfig :
265
265
function_name = lambda_function .logical_id
266
266
event_invoke_resources , event_invoke_policies = self ._construct_event_invoke_config (
@@ -279,7 +279,7 @@ def to_cloudformation(self, **kwargs): # type: ignore[no-untyped-def]
279
279
resources .append (execution_role )
280
280
281
281
try :
282
- resources += self ._generate_event_resources ( # type: ignore[no-untyped-call]
282
+ resources += self ._generate_event_resources (
283
283
lambda_function ,
284
284
execution_role ,
285
285
kwargs ["event_resources" ],
@@ -291,7 +291,15 @@ def to_cloudformation(self, **kwargs): # type: ignore[no-untyped-def]
291
291
292
292
return resources
293
293
294
- def _construct_event_invoke_config (self , function_name , alias_name , lambda_alias , intrinsics_resolver , conditions , event_invoke_config : Dict [str , Any ]): # type: ignore[no-untyped-def]
294
+ def _construct_event_invoke_config (
295
+ self ,
296
+ function_name : str ,
297
+ alias_name : str ,
298
+ lambda_alias : Optional [LambdaAlias ],
299
+ intrinsics_resolver : IntrinsicsResolver ,
300
+ conditions : Any ,
301
+ event_invoke_config : Dict [str , Any ],
302
+ ) -> Tuple [List [Any ], List [Dict [str , Any ]]]:
295
303
"""
296
304
Create a `AWS::Lambda::EventInvokeConfig` based on the input dict `EventInvokeConfig`
297
305
"""
@@ -502,7 +510,7 @@ def _construct_lambda_function(self) -> LambdaFunction:
502
510
lambda_function .VpcConfig = self .VpcConfig
503
511
lambda_function .Role = self .Role
504
512
lambda_function .Environment = self .Environment
505
- lambda_function .Code = self ._construct_code_dict () # type: ignore[no-untyped-call]
513
+ lambda_function .Code = self ._construct_code_dict ()
506
514
lambda_function .KmsKeyArn = self .KmsKeyArn
507
515
lambda_function .ReservedConcurrentExecutions = self .ReservedConcurrentExecutions
508
516
lambda_function .Tags = self ._construct_tag_list (self .Tags )
@@ -694,7 +702,7 @@ def _validate_dlq(self, dead_letter_queue: Dict[str, Any]) -> None:
694
702
self .logical_id , "'DeadLetterQueue' requires Type of {}" .format (valid_dlq_types )
695
703
)
696
704
697
- def _event_resources_to_link (self , resources ): # type: ignore[no-untyped-def]
705
+ def _event_resources_to_link (self , resources : Dict [ str , Any ]) -> Dict [ str , Any ]:
698
706
event_resources = {}
699
707
if self .Events :
700
708
for logical_id , event_dict in self .Events .items ():
@@ -708,7 +716,7 @@ def _event_resources_to_link(self, resources): # type: ignore[no-untyped-def]
708
716
return event_resources
709
717
710
718
@staticmethod
711
- def order_events (event ): # type: ignore[no-untyped-def]
719
+ def order_events (event : Tuple [ str , Any ]) -> Any :
712
720
"""
713
721
Helper method for sorting Function Events. Returns a key to use in sorting this event
714
722
@@ -722,9 +730,14 @@ def order_events(event): # type: ignore[no-untyped-def]
722
730
return logical_id
723
731
return event_dict .get ("Properties" , {}).get ("Path" , logical_id )
724
732
725
- def _generate_event_resources ( # type: ignore[no-untyped-def]
726
- self , lambda_function , execution_role , event_resources , intrinsics_resolver , lambda_alias = None
727
- ):
733
+ def _generate_event_resources (
734
+ self ,
735
+ lambda_function : LambdaFunction ,
736
+ execution_role : Optional [IAMRole ],
737
+ event_resources : Any ,
738
+ intrinsics_resolver : IntrinsicsResolver ,
739
+ lambda_alias : Optional [LambdaAlias ] = None ,
740
+ ) -> List [Any ]:
728
741
"""Generates and returns the resources associated with this function's events.
729
742
730
743
:param model.lambda_.LambdaFunction lambda_function: generated Lambda function
@@ -761,7 +774,7 @@ def _generate_event_resources( # type: ignore[no-untyped-def]
761
774
762
775
return resources
763
776
764
- def _construct_code_dict (self ): # type: ignore[no-untyped-def]
777
+ def _construct_code_dict (self ) -> Dict [ str , Any ]:
765
778
"""Constructs Lambda Code Dictionary based on the accepted SAM artifact properties such
766
779
as `InlineCode`, `CodeUri` and `ImageUri` and also raises errors if more than one of them is
767
780
defined. `PackageType` determines which artifacts are considered.
@@ -782,11 +795,11 @@ def _construct_code_dict(self): # type: ignore[no-untyped-def]
782
795
783
796
# Inline function for transformation of inline code.
784
797
# It accepts arbitrary argumemnts, because the arguments do not matter for the result.
785
- def _construct_inline_code (* args , ** kwargs ): # type: ignore[no-untyped-def]
798
+ def _construct_inline_code (* args : Any , ** kwargs : Dict [ str , Any ]) -> Dict [ str , Any ]:
786
799
return {"ZipFile" : self .InlineCode }
787
800
788
801
# dispatch mechanism per artifact on how it needs to be transformed.
789
- artifact_dispatch = {
802
+ artifact_dispatch : Dict [ str , Callable [..., Dict [ str , Any ]]] = {
790
803
"InlineCode" : _construct_inline_code ,
791
804
"CodeUri" : construct_s3_location_object ,
792
805
"ImageUri" : construct_image_code_object ,
@@ -813,8 +826,8 @@ def _construct_inline_code(*args, **kwargs): # type: ignore[no-untyped-def]
813
826
filtered_key = "ImageUri"
814
827
else :
815
828
raise InvalidResourceException (self .logical_id , "Either 'InlineCode' or 'CodeUri' must be set." )
816
- dispatch_function = artifact_dispatch [filtered_key ]
817
- return dispatch_function (artifacts [filtered_key ], self .logical_id , filtered_key ) # type: ignore[operator]
829
+ dispatch_function : Callable [..., Dict [ str , Any ]] = artifact_dispatch [filtered_key ]
830
+ return dispatch_function (artifacts [filtered_key ], self .logical_id , filtered_key )
818
831
819
832
def _construct_version (
820
833
self , function : LambdaFunction , intrinsics_resolver : IntrinsicsResolver , code_sha256 : Optional [str ] = None
@@ -969,7 +982,7 @@ def _validate_deployment_preference_and_add_update_policy(
969
982
970
983
def _resolve_property_to_boolean (
971
984
self ,
972
- property_value : Union [bool , str , dict ], # type: ignore[type-arg]
985
+ property_value : Union [bool , str , Dict [ str , Any ]],
973
986
property_name : str ,
974
987
intrinsics_resolver : IntrinsicsResolver ,
975
988
mappings_resolver : IntrinsicsResolver ,
@@ -1557,12 +1570,12 @@ def to_cloudformation(self, **kwargs): # type: ignore[no-untyped-def]
1557
1570
resources = []
1558
1571
1559
1572
# Append any CFN resources:
1560
- intrinsics_resolver = kwargs ["intrinsics_resolver" ]
1561
- resources .append (self ._construct_lambda_layer (intrinsics_resolver )) # type: ignore[no-untyped-call]
1573
+ intrinsics_resolver : IntrinsicsResolver = kwargs ["intrinsics_resolver" ]
1574
+ resources .append (self ._construct_lambda_layer (intrinsics_resolver ))
1562
1575
1563
1576
return resources
1564
1577
1565
- def _construct_lambda_layer (self , intrinsics_resolver ): # type: ignore[no-untyped-def]
1578
+ def _construct_lambda_layer (self , intrinsics_resolver : IntrinsicsResolver ) -> LambdaLayerVersion :
1566
1579
"""Constructs and returns the Lambda function.
1567
1580
1568
1581
:returns: a list containing the Lambda function and execution role resources
@@ -1588,12 +1601,12 @@ def _construct_lambda_layer(self, intrinsics_resolver): # type: ignore[no-untyp
1588
1601
old_logical_id = self .logical_id
1589
1602
1590
1603
# This is to prevent the passthrough resource attributes to be included for hashing
1591
- hash_dict = copy .deepcopy (self .to_dict ()) # type: ignore[no-untyped-call]
1592
- if "DeletionPolicy" in hash_dict .get (old_logical_id ):
1604
+ hash_dict = copy .deepcopy (self .to_dict ())
1605
+ if "DeletionPolicy" in hash_dict .get (old_logical_id , {} ):
1593
1606
del hash_dict [old_logical_id ]["DeletionPolicy" ]
1594
- if "UpdateReplacePolicy" in hash_dict .get (old_logical_id ):
1607
+ if "UpdateReplacePolicy" in hash_dict .get (old_logical_id , {} ):
1595
1608
del hash_dict [old_logical_id ]["UpdateReplacePolicy" ]
1596
- if "Metadata" in hash_dict .get (old_logical_id ):
1609
+ if "Metadata" in hash_dict .get (old_logical_id , {} ):
1597
1610
del hash_dict [old_logical_id ]["Metadata" ]
1598
1611
1599
1612
new_logical_id = logical_id_generator .LogicalIdGenerator (old_logical_id , hash_dict ).gen ()
@@ -1615,10 +1628,10 @@ def _construct_lambda_layer(self, intrinsics_resolver): # type: ignore[no-untyp
1615
1628
1616
1629
lambda_layer .LayerName = self .LayerName
1617
1630
lambda_layer .Description = self .Description
1618
- lambda_layer .Content = construct_s3_location_object (self .ContentUri , self .logical_id , "ContentUri" ) # type: ignore[no-untyped-call]
1631
+ lambda_layer .Content = construct_s3_location_object (self .ContentUri , self .logical_id , "ContentUri" )
1619
1632
1620
1633
lambda_layer .CompatibleArchitectures = self .CompatibleArchitectures
1621
- self ._validate_architectures (lambda_layer ) # type: ignore[no-untyped-call]
1634
+ self ._validate_architectures (lambda_layer )
1622
1635
lambda_layer .CompatibleRuntimes = self .CompatibleRuntimes
1623
1636
lambda_layer .LicenseInfo = self .LicenseInfo
1624
1637
@@ -1658,7 +1671,7 @@ def _get_retention_policy_value(self) -> Optional[str]:
1658
1671
"'RetentionPolicy' must be one of the following options: {}." .format ([self .RETAIN , self .DELETE ]),
1659
1672
)
1660
1673
1661
- def _validate_architectures (self , lambda_layer ): # type: ignore[no-untyped-def]
1674
+ def _validate_architectures (self , lambda_layer : LambdaLayerVersion ) -> None :
1662
1675
"""Validate the values inside the CompatibleArchitectures field of a layer
1663
1676
1664
1677
Parameters
@@ -1718,7 +1731,7 @@ class SamStateMachine(SamResourceMacro):
1718
1731
Tracing : Optional [Dict [str , Any ]]
1719
1732
PermissionsBoundary : Optional [Intrinsicable [str ]]
1720
1733
1721
- event_resolver = ResourceTypeResolver ( # type: ignore[no-untyped-call]
1734
+ event_resolver = ResourceTypeResolver (
1722
1735
samtranslator .model .stepfunctions .events ,
1723
1736
samtranslator .model .eventsources .scheduler ,
1724
1737
)
@@ -1756,13 +1769,13 @@ def to_cloudformation(self, **kwargs): # type: ignore[no-untyped-def]
1756
1769
resources = state_machine_generator .to_cloudformation ()
1757
1770
return resources
1758
1771
1759
- def resources_to_link (self , resources ): # type: ignore[no-untyped-def]
1772
+ def resources_to_link (self , resources : Dict [ str , Any ]) -> Dict [ str , Any ]:
1760
1773
try :
1761
- return {"event_resources" : self ._event_resources_to_link (resources )} # type: ignore[no-untyped-call]
1774
+ return {"event_resources" : self ._event_resources_to_link (resources )}
1762
1775
except InvalidEventException as e :
1763
1776
raise InvalidResourceException (self .logical_id , e .message )
1764
1777
1765
- def _event_resources_to_link (self , resources ): # type: ignore[no-untyped-def]
1778
+ def _event_resources_to_link (self , resources : Dict [ str , Any ]) -> Dict [ str , Any ]:
1766
1779
event_resources = {}
1767
1780
if self .Events :
1768
1781
for logical_id , event_dict in self .Events .items ():
0 commit comments