From 83216480ef1f6ef14d277870a2e660a2d779f256 Mon Sep 17 00:00:00 2001 From: Mark Peek Date: Sat, 28 Sep 2024 10:59:48 -0700 Subject: [PATCH] Validate the type for Tags Prior to this change Tags (which is a subclass of AWSHelperFn) would not do validation of the expected type and it prevented validation of Json style Tags needing a dict. --- tests/test_serverless.py | 4 ++-- tests/test_tags.py | 41 ++++++++++++++++++++++++++++++++++++++++ troposphere/__init__.py | 6 ++++-- 3 files changed, 47 insertions(+), 4 deletions(-) diff --git a/tests/test_serverless.py b/tests/test_serverless.py index 2db1ab1c9..fe7e976ae 100644 --- a/tests/test_serverless.py +++ b/tests/test_serverless.py @@ -1,6 +1,6 @@ import unittest -from troposphere import ImportValue, Parameter, Ref, Sub, Tags, Template +from troposphere import ImportValue, Parameter, Ref, Sub, Template from troposphere.s3 import Filter, Rules, S3Key from troposphere.serverless import ( SERVERLESS_TRANSFORM, @@ -70,7 +70,7 @@ def test_tags(self): Handler="index.handler", Runtime="nodejs", CodeUri="s3://bucket/handler.zip", - Tags=Tags({"Tag1": "TagValue1", "Tag2": "TagValue2"}), + Tags={"Tag1": "TagValue1", "Tag2": "TagValue2"}, ) t = Template() t.add_resource(serverless_func) diff --git a/tests/test_tags.py b/tests/test_tags.py index be5886b65..b3cb4ebfc 100644 --- a/tests/test_tags.py +++ b/tests/test_tags.py @@ -73,6 +73,47 @@ def test_Formats(self): with self.assertRaises(TypeError): Tags({}, "key", "value") + def test_json_tags(self): + from troposphere.batch import ContainerProperties, JobDefinition + + JobDefinition( + "myjobdef", + Type="container", + ContainerProperties=ContainerProperties( + Image="image", + Vcpus=2, + Memory=2000, + Command=["echo", "foobar"], + ), + Tags={"foo": "bar"}, + ) + + with self.assertRaises(TypeError): + JobDefinition( + "myjobdef", + Type="container", + ContainerProperties=ContainerProperties( + Image="image", + Vcpus=2, + Memory=2000, + Command=["echo", "foobar"], + ), + Tags=Tags({"foo": "bar"}), + ) + + with self.assertRaises(TypeError): + JobDefinition( + "myjobdef", + Type="container", + ContainerProperties=ContainerProperties( + Image="image", + Vcpus=2, + Memory=2000, + Command=["echo", "foobar"], + ), + Tags="string", + ) + if __name__ == "__main__": unittest.main() diff --git a/troposphere/__init__.py b/troposphere/__init__.py index e618ae705..d4e78ed94 100644 --- a/troposphere/__init__.py +++ b/troposphere/__init__.py @@ -252,9 +252,11 @@ def __setattr__(self, name: str, value: Any) -> None: expected_type = self.props[name][0] # If the value is a AWSHelperFn we can't do much validation - # we'll have to leave that to Amazon. Maybe there's another way + # we'll have to leave that to Amazon. Maybe there's another way # to deal with this that we'll come up with eventually - if isinstance(value, AWSHelperFn): + # + # Don't do this for Tags since we can validate the assigned type below + if isinstance(value, AWSHelperFn) and name != "Tags": return self.properties.__setitem__(name, value) # If it's a function, call it...