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
7a3602e7
Commit
7a3602e7
authored
Aug 30, 2015
by
Raymond Hettinger
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Issue #24931: Resolve __dict__ conflict in namedtuple subclasses.
parent
1a837464
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
19 additions
and
14 deletions
+19
-14
Doc/library/collections.rst
Doc/library/collections.rst
+3
-3
Lib/collections/__init__.py
Lib/collections/__init__.py
+1
-10
Lib/test/test_collections.py
Lib/test/test_collections.py
+11
-1
Misc/NEWS
Misc/NEWS
+4
-0
No files found.
Doc/library/collections.rst
View file @
7a3602e7
...
...
@@ -816,10 +816,10 @@ field names, the method and attribute names start with an underscore.
.. method:: somenamedtuple._asdict()
Return a new :class:`OrderedDict` which maps field names to their corresponding
values. Note, this method is no longer needed now that the same effect can
be achieved by using the built-in :func:`vars` function::
values::
>>> vars(p)
>>> p = Point(x=11, y=22)
>>> p._asdict()
OrderedDict([('x', 11), ('y', 22)])
.. versionchanged:: 3.1
...
...
Lib/collections/__init__.py
View file @
7a3602e7
...
...
@@ -272,23 +272,14 @@ class {typename}(tuple):
'Return a nicely formatted representation string'
return self.__class__.__name__ + '({repr_fmt})' % self
@property
def __dict__(self):
'A new OrderedDict mapping field names to their values'
return OrderedDict(zip(self._fields, self))
def _asdict(self):
'Return a new OrderedDict which maps field names to their values.'
return
self.__dict__
return
OrderedDict(zip(self._fields, self))
def __getnewargs__(self):
'Return self as a plain tuple. Used by copy and pickle.'
return tuple(self)
def __getstate__(self):
'Exclude the OrderedDict from pickling'
return None
{field_defs}
"""
...
...
Lib/test/test_collections.py
View file @
7a3602e7
...
...
@@ -225,7 +225,6 @@ class TestNamedTuple(unittest.TestCase):
self
.
assertEqual
(
p
.
_fields
,
(
'x'
,
'y'
))
# test _fields attribute
self
.
assertEqual
(
p
.
_replace
(
x
=
1
),
(
1
,
22
))
# test _replace method
self
.
assertEqual
(
p
.
_asdict
(),
dict
(
x
=
11
,
y
=
22
))
# test _asdict method
self
.
assertEqual
(
vars
(
p
),
p
.
_asdict
())
# verify that vars() works
try
:
p
.
_replace
(
x
=
1
,
error
=
2
)
...
...
@@ -380,6 +379,17 @@ class TestNamedTuple(unittest.TestCase):
globals
().
pop
(
'NTColor'
,
None
)
# clean-up after this test
def
test_namedtuple_subclass_issue_24931
(
self
):
class
Point
(
namedtuple
(
'_Point'
,
[
'x'
,
'y'
])):
pass
a
=
Point
(
3
,
4
)
self
.
assertEqual
(
a
.
_asdict
(),
OrderedDict
([(
'x'
,
3
),
(
'y'
,
4
)]))
a
.
w
=
5
self
.
assertEqual
(
a
.
__dict__
,
{
'w'
:
5
})
################################################################################
### Abstract Base Classes
################################################################################
...
...
Misc/NEWS
View file @
7a3602e7
...
...
@@ -78,6 +78,10 @@ Library
-
Issue
#
21112
:
Fix
regression
in
unittest
.
expectedFailure
on
subclasses
.
Patch
from
Berker
Peksag
.
-
Issue
#
24931
:
Instances
of
subclasses
of
namedtuples
have
their
own
__dict__
which
breaks
the
inherited
__dict__
property
and
breaks
the
_asdict
()
method
.
Removed
the
__dict__
property
to
prevent
the
conflict
and
fixed
_asdict
().
-
Issue
#
24764
:
cgi
.
FieldStorage
.
read_multi
()
now
ignores
the
Content
-
Length
header
in
part
headers
.
Patch
written
by
Peter
Landry
and
reviewed
by
Pierre
Quentel
.
...
...
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