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:
Matt Martz 2018-10-12 10:43:09 -05:00 committed by GitHub
parent 0dd17b521f
commit a06a5ded61
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
14 changed files with 57 additions and 37 deletions

View file

@ -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