diff --git a/tests/test_basic.py b/tests/test_basic.py index f5714b16e..7a0384227 100644 --- a/tests/test_basic.py +++ b/tests/test_basic.py @@ -122,6 +122,17 @@ def test___ne__(self): assert Parameter("param1") != Parameter("param2") assert Stack("stack1") != Stack("stack2") + def test___hash__(self): + """Test __hash__.""" + from troposphere.ec2 import SecurityGroup + + test_sg = SecurityGroup( + "TestSg", + VpcId="myVPC", + GroupDescription="Test Security Group", + ) + _ = hash(test_sg) + def test_badproperty(self): with self.assertRaises(AttributeError): Instance( diff --git a/troposphere/__init__.py b/troposphere/__init__.py index 1f214e258..da86b4907 100644 --- a/troposphere/__init__.py +++ b/troposphere/__init__.py @@ -431,6 +431,9 @@ def __eq__(self, other: object) -> bool: def __ne__(self, other: object) -> bool: return not self == other + def __hash__(self) -> int: + return hash(json.dumps({"title": self.title, **self.to_dict()}, indent=0)) + class AWSObject(BaseAWSObject): dictname = "Properties" @@ -1115,6 +1118,3 @@ def check_type(t: type, v: Any) -> bool: "%s can only be used with parameters of " "the CommaDelimitedList type." % p ) - - def __hash__(self) -> int: - return hash(json.dumps({"title": self.title, **self.to_dict()}, indent=0))