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
13abda41
Commit
13abda41
authored
Oct 08, 2019
by
Serhiy Storchaka
Committed by
GitHub
Oct 08, 2019
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
bpo-38405: Make nested subclasses of typing.NamedTuple pickleable. (GH-16641)
parent
b690a275
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
23 additions
and
8 deletions
+23
-8
Lib/test/test_typing.py
Lib/test/test_typing.py
+21
-7
Lib/typing.py
Lib/typing.py
+1
-1
Misc/NEWS.d/next/Library/2019-10-08-11-18-40.bpo-38405.0-7e7s.rst
...S.d/next/Library/2019-10-08-11-18-40.bpo-38405.0-7e7s.rst
+1
-0
No files found.
Lib/test/test_typing.py
View file @
13abda41
...
...
@@ -3468,6 +3468,9 @@ class NewTypeTests(BaseTestCase):
class
NamedTupleTests
(
BaseTestCase
):
class
NestedEmployee
(
NamedTuple
):
name
:
str
cool
:
int
def
test_basics
(
self
):
Emp
=
NamedTuple
(
'Emp'
,
[(
'name'
,
str
),
(
'id'
,
int
)])
...
...
@@ -3587,14 +3590,25 @@ class XMethBad2(NamedTuple):
with
self
.
assertRaises
(
TypeError
):
NamedTuple
(
'Emp'
,
fields
=
[(
'name'
,
str
),
(
'id'
,
int
)])
def
test_pickle
(
self
):
def
test_
copy_and_
pickle
(
self
):
global
Emp
# pickle wants to reference the class by name
Emp
=
NamedTuple
(
'Emp'
,
[(
'name'
,
str
),
(
'id'
,
int
)])
jane
=
Emp
(
'jane'
,
37
)
for
proto
in
range
(
pickle
.
HIGHEST_PROTOCOL
+
1
):
z
=
pickle
.
dumps
(
jane
,
proto
)
jane2
=
pickle
.
loads
(
z
)
self
.
assertEqual
(
jane2
,
jane
)
Emp
=
NamedTuple
(
'Emp'
,
[(
'name'
,
str
),
(
'cool'
,
int
)])
for
cls
in
Emp
,
CoolEmployee
,
self
.
NestedEmployee
:
with
self
.
subTest
(
cls
=
cls
):
jane
=
cls
(
'jane'
,
37
)
for
proto
in
range
(
pickle
.
HIGHEST_PROTOCOL
+
1
):
z
=
pickle
.
dumps
(
jane
,
proto
)
jane2
=
pickle
.
loads
(
z
)
self
.
assertEqual
(
jane2
,
jane
)
self
.
assertIsInstance
(
jane2
,
cls
)
jane2
=
copy
(
jane
)
self
.
assertEqual
(
jane2
,
jane
)
self
.
assertIsInstance
(
jane2
,
cls
)
jane2
=
deepcopy
(
jane
)
self
.
assertEqual
(
jane2
,
jane
)
self
.
assertIsInstance
(
jane2
,
cls
)
class
TypedDictTests
(
BaseTestCase
):
...
...
Lib/typing.py
View file @
13abda41
...
...
@@ -1593,7 +1593,7 @@ _prohibited = ('__new__', '__init__', '__slots__', '__getnewargs__',
'_fields'
,
'_field_defaults'
,
'_field_types'
,
'_make'
,
'_replace'
,
'_asdict'
,
'_source'
)
_special
=
(
'__module__'
,
'__name__'
,
'__
qualname__'
,
'__
annotations__'
)
_special
=
(
'__module__'
,
'__name__'
,
'__annotations__'
)
class
NamedTupleMeta
(
type
):
...
...
Misc/NEWS.d/next/Library/2019-10-08-11-18-40.bpo-38405.0-7e7s.rst
0 → 100644
View file @
13abda41
Nested subclasses of :class:`typing.NamedTuple` are now pickleable.
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