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

Validate the type for Tags #2270

Merged
merged 1 commit into from
Sep 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions tests/test_serverless.py
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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)
Expand Down
41 changes: 41 additions & 0 deletions tests/test_tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
6 changes: 4 additions & 2 deletions troposphere/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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...
Expand Down