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
b01786c8
Commit
b01786c8
authored
Feb 12, 2019
by
Christopher Hunt
Committed by
Eric V. Smith
Feb 12, 2019
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
bpo-35960: Fix dataclasses.field throwing away empty metadata. (GH-11815)
parent
a31f4cc8
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
15 additions
and
3 deletions
+15
-3
Lib/dataclasses.py
Lib/dataclasses.py
+1
-1
Lib/test/test_dataclasses.py
Lib/test/test_dataclasses.py
+12
-2
Misc/NEWS.d/next/Library/2019-02-10-20-57-12.bpo-35960.bh-6Ja.rst
...S.d/next/Library/2019-02-10-20-57-12.bpo-35960.bh-6Ja.rst
+2
-0
No files found.
Lib/dataclasses.py
View file @
b01786c8
...
...
@@ -241,7 +241,7 @@ class Field:
self
.
hash
=
hash
self
.
compare
=
compare
self
.
metadata
=
(
_EMPTY_METADATA
if
metadata
is
None
or
len
(
metadata
)
==
0
else
if
metadata
is
None
else
types
.
MappingProxyType
(
metadata
))
self
.
_field_type
=
None
...
...
Lib/test/test_dataclasses.py
View file @
b01786c8
...
...
@@ -1737,23 +1737,33 @@ class TestCase(unittest.TestCase):
i
:
int
=
field
(
metadata
=
0
)
# Make sure an empty dict works.
d
=
{}
@
dataclass
class
C
:
i
:
int
=
field
(
metadata
=
{}
)
i
:
int
=
field
(
metadata
=
d
)
self
.
assertFalse
(
fields
(
C
)[
0
].
metadata
)
self
.
assertEqual
(
len
(
fields
(
C
)[
0
].
metadata
),
0
)
# Update should work (see bpo-35960).
d
[
'foo'
]
=
1
self
.
assertEqual
(
len
(
fields
(
C
)[
0
].
metadata
),
1
)
self
.
assertEqual
(
fields
(
C
)[
0
].
metadata
[
'foo'
],
1
)
with
self
.
assertRaisesRegex
(
TypeError
,
'does not support item assignment'
):
fields
(
C
)[
0
].
metadata
[
'test'
]
=
3
# Make sure a non-empty dict works.
d
=
{
'test'
:
10
,
'bar'
:
'42'
,
3
:
'three'
}
@
dataclass
class
C
:
i
:
int
=
field
(
metadata
=
{
'test'
:
10
,
'bar'
:
'42'
,
3
:
'three'
}
)
i
:
int
=
field
(
metadata
=
d
)
self
.
assertEqual
(
len
(
fields
(
C
)[
0
].
metadata
),
3
)
self
.
assertEqual
(
fields
(
C
)[
0
].
metadata
[
'test'
],
10
)
self
.
assertEqual
(
fields
(
C
)[
0
].
metadata
[
'bar'
],
'42'
)
self
.
assertEqual
(
fields
(
C
)[
0
].
metadata
[
3
],
'three'
)
# Update should work.
d
[
'foo'
]
=
1
self
.
assertEqual
(
len
(
fields
(
C
)[
0
].
metadata
),
4
)
self
.
assertEqual
(
fields
(
C
)[
0
].
metadata
[
'foo'
],
1
)
with
self
.
assertRaises
(
KeyError
):
# Non-existent key.
fields
(
C
)[
0
].
metadata
[
'baz'
]
...
...
Misc/NEWS.d/next/Library/2019-02-10-20-57-12.bpo-35960.bh-6Ja.rst
0 → 100644
View file @
b01786c8
Fix :func:`dataclasses.field` throwing away empty mapping objects passed as
metadata.
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