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
23581c01
Commit
23581c01
authored
Mar 18, 2019
by
Raymond Hettinger
Committed by
GitHub
Mar 18, 2019
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
bpo-36321: Fix misspelled attribute in namedtuple() (GH-12375)
parent
6a16b182
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
15 additions
and
8 deletions
+15
-8
Doc/library/collections.rst
Doc/library/collections.rst
+2
-2
Lib/collections/__init__.py
Lib/collections/__init__.py
+2
-0
Lib/test/test_collections.py
Lib/test/test_collections.py
+6
-6
Misc/NEWS.d/next/Library/2019-03-16-13-40-59.bpo-36321.s6crQx.rst
...S.d/next/Library/2019-03-16-13-40-59.bpo-36321.s6crQx.rst
+5
-0
No files found.
Doc/library/collections.rst
View file @
23581c01
...
...
@@ -953,14 +953,14 @@ field names, the method and attribute names start with an underscore.
>>> Pixel(11, 22, 128, 255, 0)
Pixel(x=11, y=22, red=128, green=255, blue=0)
.. attribute:: somenamedtuple._field
s
_defaults
.. attribute:: somenamedtuple._field_defaults
Dictionary mapping field names to default values.
.. doctest::
>>> Account = namedtuple('Account', ['type', 'balance'], defaults=[0])
>>> Account._field
s
_defaults
>>> Account._field_defaults
{'balance': 0}
>>> Account('premium')
Account(type='premium', balance=0)
...
...
Lib/collections/__init__.py
View file @
23581c01
...
...
@@ -445,6 +445,8 @@ def namedtuple(typename, field_names, *, rename=False, defaults=None, module=Non
'__doc__'
:
f'
{
typename
}
(
{
arg_list
}
)'
,
'__slots__'
:
(),
'_fields'
:
field_names
,
'_field_defaults'
:
field_defaults
,
# alternate spelling for backward compatiblity
'_fields_defaults'
:
field_defaults
,
'__new__'
:
__new__
,
'_make'
:
_make
,
...
...
Lib/test/test_collections.py
View file @
23581c01
...
...
@@ -248,18 +248,18 @@ class TestNamedTuple(unittest.TestCase):
def
test_defaults
(
self
):
Point
=
namedtuple
(
'Point'
,
'x y'
,
defaults
=
(
10
,
20
))
# 2 defaults
self
.
assertEqual
(
Point
.
_field
s
_defaults
,
{
'x'
:
10
,
'y'
:
20
})
self
.
assertEqual
(
Point
.
_field_defaults
,
{
'x'
:
10
,
'y'
:
20
})
self
.
assertEqual
(
Point
(
1
,
2
),
(
1
,
2
))
self
.
assertEqual
(
Point
(
1
),
(
1
,
20
))
self
.
assertEqual
(
Point
(),
(
10
,
20
))
Point
=
namedtuple
(
'Point'
,
'x y'
,
defaults
=
(
20
,))
# 1 default
self
.
assertEqual
(
Point
.
_field
s
_defaults
,
{
'y'
:
20
})
self
.
assertEqual
(
Point
.
_field_defaults
,
{
'y'
:
20
})
self
.
assertEqual
(
Point
(
1
,
2
),
(
1
,
2
))
self
.
assertEqual
(
Point
(
1
),
(
1
,
20
))
Point
=
namedtuple
(
'Point'
,
'x y'
,
defaults
=
())
# 0 defaults
self
.
assertEqual
(
Point
.
_field
s
_defaults
,
{})
self
.
assertEqual
(
Point
.
_field_defaults
,
{})
self
.
assertEqual
(
Point
(
1
,
2
),
(
1
,
2
))
with
self
.
assertRaises
(
TypeError
):
Point
(
1
)
...
...
@@ -276,21 +276,21 @@ class TestNamedTuple(unittest.TestCase):
Point
=
namedtuple
(
'Point'
,
'x y'
,
defaults
=
False
)
Point
=
namedtuple
(
'Point'
,
'x y'
,
defaults
=
None
)
# default is None
self
.
assertEqual
(
Point
.
_field
s
_defaults
,
{})
self
.
assertEqual
(
Point
.
_field_defaults
,
{})
self
.
assertIsNone
(
Point
.
__new__
.
__defaults__
,
None
)
self
.
assertEqual
(
Point
(
10
,
20
),
(
10
,
20
))
with
self
.
assertRaises
(
TypeError
):
# catch too few args
Point
(
10
)
Point
=
namedtuple
(
'Point'
,
'x y'
,
defaults
=
[
10
,
20
])
# allow non-tuple iterable
self
.
assertEqual
(
Point
.
_field
s
_defaults
,
{
'x'
:
10
,
'y'
:
20
})
self
.
assertEqual
(
Point
.
_field_defaults
,
{
'x'
:
10
,
'y'
:
20
})
self
.
assertEqual
(
Point
.
__new__
.
__defaults__
,
(
10
,
20
))
self
.
assertEqual
(
Point
(
1
,
2
),
(
1
,
2
))
self
.
assertEqual
(
Point
(
1
),
(
1
,
20
))
self
.
assertEqual
(
Point
(),
(
10
,
20
))
Point
=
namedtuple
(
'Point'
,
'x y'
,
defaults
=
iter
([
10
,
20
]))
# allow plain iterator
self
.
assertEqual
(
Point
.
_field
s
_defaults
,
{
'x'
:
10
,
'y'
:
20
})
self
.
assertEqual
(
Point
.
_field_defaults
,
{
'x'
:
10
,
'y'
:
20
})
self
.
assertEqual
(
Point
.
__new__
.
__defaults__
,
(
10
,
20
))
self
.
assertEqual
(
Point
(
1
,
2
),
(
1
,
2
))
self
.
assertEqual
(
Point
(
1
),
(
1
,
20
))
...
...
Misc/NEWS.d/next/Library/2019-03-16-13-40-59.bpo-36321.s6crQx.rst
0 → 100644
View file @
23581c01
collections.namedtuple() misspelled the name of an attribute. To be
consistent with typing.NamedTuple, the attribute name should have been
"_field_defaults" instead of "_fields_defaults". For backwards
compatibility, both spellings are now created. The misspelled version may
be removed in the future.
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