summaryrefslogtreecommitdiffstats
path: root/Lib/dataclasses.py
diff options
context:
space:
mode:
authorEric V. Smith <ericvsmith@users.noreply.github.com>2018-03-26 17:29:16 (GMT)
committerGitHub <noreply@github.com>2018-03-26 17:29:16 (GMT)
commitde7a2f04d6b9427d568fcb43b6f512f9b4c4bd84 (patch)
tree522d8689db0b51a68dc8a976acae8b3d77d2aaba /Lib/dataclasses.py
parent030345c0bfc2f76684666fe5c61e766ba5debfe6 (diff)
downloadcpython-de7a2f04d6b9427d568fcb43b6f512f9b4c4bd84.zip
cpython-de7a2f04d6b9427d568fcb43b6f512f9b4c4bd84.tar.gz
cpython-de7a2f04d6b9427d568fcb43b6f512f9b4c4bd84.tar.bz2
bpo-33141: Have dataclasses.Field pass through __set_name__ to any default argument. (GH-6260)
This is part of PEP 487 and the descriptor protocol.
Diffstat (limited to 'Lib/dataclasses.py')
-rw-r--r--Lib/dataclasses.py14
1 files changed, 14 insertions, 0 deletions
diff --git a/Lib/dataclasses.py b/Lib/dataclasses.py
index 8ccc4c8..8c197fe 100644
--- a/Lib/dataclasses.py
+++ b/Lib/dataclasses.py
@@ -240,6 +240,20 @@ class Field:
f'metadata={self.metadata}'
')')
+ # This is used to support the PEP 487 __set_name__ protocol in the
+ # case where we're using a field that contains a descriptor as a
+ # defaul value. For details on __set_name__, see
+ # https://www.python.org/dev/peps/pep-0487/#implementation-details.
+ # Note that in _process_class, this Field object is overwritten with
+ # the default value, so the end result is a descriptor that had
+ # __set_name__ called on it at the right time.
+ def __set_name__(self, owner, name):
+ func = getattr(self.default, '__set_name__', None)
+ if func:
+ # There is a __set_name__ method on the descriptor,
+ # call it.
+ func(owner, name)
+
class _DataclassParams:
__slots__ = ('init',