mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-04-28 21:31:26 -07:00
Do not use mutable defaults in FieldAttribute, instead allow supplying a callable for defaults of mutable types. Fixes #46824 (#46833)
This commit is contained in:
parent
0dd17b521f
commit
a06a5ded61
14 changed files with 57 additions and 37 deletions
|
@ -87,19 +87,8 @@ class Attribute:
|
|||
self.extend = extend
|
||||
self.prepend = prepend
|
||||
|
||||
if default is not None and self.isa in _CONTAINERS:
|
||||
if default:
|
||||
self.default = deepcopy(default)
|
||||
else:
|
||||
# Don't need to deepcopy default if the container is empty
|
||||
# Note: switch to try: except once Python3 is more widespread
|
||||
if hasattr(default, 'copy'):
|
||||
self.default = default.copy()
|
||||
else:
|
||||
# list on python2 does not have .copy()
|
||||
self.default = copy(default)
|
||||
else:
|
||||
self.default = default
|
||||
if default is not None and self.isa in _CONTAINERS and not callable(default):
|
||||
raise TypeError('defaults for FieldAttribute may not be mutable, please provide a callable instead')
|
||||
|
||||
def __eq__(self, other):
|
||||
return other.priority == self.priority
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue