Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
52199520
Commit
52199520
authored
Mar 29, 2018
by
Eric V. Smith
Committed by
GitHub
Mar 29, 2018
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
bpo-33175: dataclasses should look up __set_name__ on class, not instance (GH-6305)
parent
b9e7fe38
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
38 additions
and
7 deletions
+38
-7
Lib/dataclasses.py
Lib/dataclasses.py
+2
-2
Lib/test/test_dataclasses.py
Lib/test/test_dataclasses.py
+34
-5
Misc/NEWS.d/next/Library/2018-03-29-04-32-25.bpo-33175._zs1yM.rst
...S.d/next/Library/2018-03-29-04-32-25.bpo-33175._zs1yM.rst
+2
-0
No files found.
Lib/dataclasses.py
View file @
52199520
...
...
@@ -248,11 +248,11 @@ class Field:
# 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
)
func
=
getattr
(
type
(
self
.
default
)
,
'__set_name__'
,
None
)
if
func
:
# There is a __set_name__ method on the descriptor,
# call it.
func
(
owner
,
name
)
func
(
self
.
default
,
owner
,
name
)
class
_DataclassParams
:
...
...
Lib/test/test_dataclasses.py
View file @
52199520
...
...
@@ -2705,7 +2705,7 @@ class TestDescriptors(unittest.TestCase):
# Create a descriptor.
class
D
:
def
__set_name__
(
self
,
owner
,
name
):
self
.
name
=
name
self
.
name
=
name
+
'x'
def
__get__
(
self
,
instance
,
owner
):
if
instance
is
not
None
:
return
1
...
...
@@ -2716,7 +2716,7 @@ class TestDescriptors(unittest.TestCase):
@
dataclass
class
C
:
c
:
int
=
D
()
self
.
assertEqual
(
C
.
c
.
name
,
'c'
)
self
.
assertEqual
(
C
.
c
.
name
,
'c
x
'
)
# Now test with a default value and init=False, which is the
# only time this is really meaningful. If not using
...
...
@@ -2724,7 +2724,7 @@ class TestDescriptors(unittest.TestCase):
@
dataclass
class
C
:
c
:
int
=
field
(
default
=
D
(),
init
=
False
)
self
.
assertEqual
(
C
.
c
.
name
,
'c'
)
self
.
assertEqual
(
C
.
c
.
name
,
'c
x
'
)
self
.
assertEqual
(
C
().
c
,
1
)
def
test_non_descriptor
(
self
):
...
...
@@ -2733,12 +2733,41 @@ class TestDescriptors(unittest.TestCase):
class
D
:
def
__set_name__
(
self
,
owner
,
name
):
self
.
name
=
name
self
.
name
=
name
+
'x'
@
dataclass
class
C
:
c
:
int
=
field
(
default
=
D
(),
init
=
False
)
self
.
assertEqual
(
C
.
c
.
name
,
'c'
)
self
.
assertEqual
(
C
.
c
.
name
,
'cx'
)
def
test_lookup_on_instance
(
self
):
# See bpo-33175.
class
D
:
pass
d
=
D
()
# Create an attribute on the instance, not type.
d
.
__set_name__
=
Mock
()
# Make sure d.__set_name__ is not called.
@
dataclass
class
C
:
i
:
int
=
field
(
default
=
d
,
init
=
False
)
self
.
assertEqual
(
d
.
__set_name__
.
call_count
,
0
)
def
test_lookup_on_class
(
self
):
# See bpo-33175.
class
D
:
pass
D
.
__set_name__
=
Mock
()
# Make sure D.__set_name__ is called.
@
dataclass
class
C
:
i
:
int
=
field
(
default
=
D
(),
init
=
False
)
self
.
assertEqual
(
D
.
__set_name__
.
call_count
,
1
)
if
__name__
==
'__main__'
:
...
...
Misc/NEWS.d/next/Library/2018-03-29-04-32-25.bpo-33175._zs1yM.rst
0 → 100644
View file @
52199520
In dataclasses, Field.__set_name__ now looks up the __set_name__ special
method on the class, not the instance, of the default value.
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment