avoid persistent containers in attribute defaults

moved from the field attribute declaration and created a placeholder
which then is resolved in the field attribute class.

this is to avoid unwanted persistent of the defaults across objects which introduces
stealth bugs when multiple objects of the same kind are used in succession while
not overriding the default values.
This commit is contained in:
Brian Coca 2015-12-09 07:21:00 -08:00
commit 87969868d4
9 changed files with 29 additions and 18 deletions

View file

@ -32,6 +32,17 @@ class Attribute:
self.priority = priority
self.always_post_validate = always_post_validate
# This is here to avoid `default=<container>` unwanted persistence across object instances
# We cannot rely on None as some fields use it to skip the code
# that would detect an empty container as a user error
if self.default == '_ansible_container':
if self.isa == 'list':
self.default = []
elif self.isa == 'dict':
self.default = {}
elif self.isa == 'set':
self.default = set()
def __eq__(self, other):
return other.priority == self.priority