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
8fc5839a
Commit
8fc5839a
authored
Sep 17, 2019
by
Serhiy Storchaka
Committed by
GitHub
Sep 17, 2019
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
bpo-38191: Turn warnings into errors in NamedTuple() and TypedDict(). (GH-16238)
parent
2bf31cca
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
13 additions
and
87 deletions
+13
-87
Lib/test/test_typing.py
Lib/test/test_typing.py
+8
-19
Lib/typing.py
Lib/typing.py
+4
-66
Misc/NEWS.d/next/Library/2019-09-17-12-28-27.bpo-38191.1TU0HV.rst
...S.d/next/Library/2019-09-17-12-28-27.bpo-38191.1TU0HV.rst
+1
-2
No files found.
Lib/test/test_typing.py
View file @
8fc5839a
...
...
@@ -3582,16 +3582,10 @@ class XMethBad2(NamedTuple):
NamedTuple
(
'Emp'
,
[(
'name'
,
str
)],
None
)
with
self
.
assertRaises
(
ValueError
):
NamedTuple
(
'Emp'
,
[(
'_name'
,
str
)])
with
self
.
assertWarns
(
DeprecationWarning
):
Emp
=
NamedTuple
(
typename
=
'Emp'
,
name
=
str
,
id
=
int
)
self
.
assertEqual
(
Emp
.
__name__
,
'Emp'
)
self
.
assertEqual
(
Emp
.
_fields
,
(
'name'
,
'id'
))
with
self
.
assertWarns
(
DeprecationWarning
):
Emp
=
NamedTuple
(
'Emp'
,
fields
=
[(
'name'
,
str
),
(
'id'
,
int
)])
self
.
assertEqual
(
Emp
.
__name__
,
'Emp'
)
self
.
assertEqual
(
Emp
.
_fields
,
(
'name'
,
'id'
))
with
self
.
assertRaises
(
TypeError
):
NamedTuple
(
typename
=
'Emp'
,
name
=
str
,
id
=
int
)
with
self
.
assertRaises
(
TypeError
):
NamedTuple
(
'Emp'
,
fields
=
[(
'name'
,
str
),
(
'id'
,
int
)])
def
test_pickle
(
self
):
global
Emp
# pickle wants to reference the class by name
...
...
@@ -3654,15 +3648,10 @@ class TypedDictTests(BaseTestCase):
with
self
.
assertRaises
(
TypeError
):
TypedDict
(
'Emp'
,
[(
'name'
,
str
)],
None
)
with
self
.
assertWarns
(
DeprecationWarning
):
Emp
=
TypedDict
(
_typename
=
'Emp'
,
name
=
str
,
id
=
int
)
self
.
assertEqual
(
Emp
.
__name__
,
'Emp'
)
self
.
assertEqual
(
Emp
.
__annotations__
,
{
'name'
:
str
,
'id'
:
int
})
with
self
.
assertWarns
(
DeprecationWarning
):
Emp
=
TypedDict
(
'Emp'
,
_fields
=
{
'name'
:
str
,
'id'
:
int
})
self
.
assertEqual
(
Emp
.
__name__
,
'Emp'
)
self
.
assertEqual
(
Emp
.
__annotations__
,
{
'name'
:
str
,
'id'
:
int
})
with
self
.
assertRaises
(
TypeError
):
TypedDict
(
_typename
=
'Emp'
,
name
=
str
,
id
=
int
)
with
self
.
assertRaises
(
TypeError
):
TypedDict
(
'Emp'
,
_fields
=
{
'name'
:
str
,
'id'
:
int
})
def
test_typeddict_errors
(
self
):
Emp
=
TypedDict
(
'Emp'
,
{
'name'
:
str
,
'id'
:
int
})
...
...
Lib/typing.py
View file @
8fc5839a
...
...
@@ -1653,81 +1653,20 @@ class NamedTuple(metaclass=NamedTupleMeta):
"""
_root
=
True
def
__new__
(
*
args
,
**
kwargs
):
if
not
args
:
raise
TypeError
(
'NamedTuple.__new__(): not enough arguments'
)
cls
,
*
args
=
args
# allow the "cls" keyword be passed
if
args
:
typename
,
*
args
=
args
# allow the "typename" keyword be passed
elif
'typename'
in
kwargs
:
typename
=
kwargs
.
pop
(
'typename'
)
import
warnings
warnings
.
warn
(
"Passing 'typename' as keyword argument is deprecated"
,
DeprecationWarning
,
stacklevel
=
2
)
else
:
raise
TypeError
(
"NamedTuple.__new__() missing 1 required positional "
"argument: 'typename'"
)
if
args
:
try
:
fields
,
=
args
# allow the "fields" keyword be passed
except
ValueError
:
raise
TypeError
(
f'NamedTuple.__new__() takes from 2 to 3 '
f'positional arguments but
{
len
(
args
)
+
2
}
'
f'were given'
)
from
None
elif
'fields'
in
kwargs
and
len
(
kwargs
)
==
1
:
fields
=
kwargs
.
pop
(
'fields'
)
import
warnings
warnings
.
warn
(
"Passing 'fields' as keyword argument is deprecated"
,
DeprecationWarning
,
stacklevel
=
2
)
else
:
fields
=
None
def
__new__
(
cls
,
typename
,
fields
=
None
,
/
,
**
kwargs
):
if
fields
is
None
:
fields
=
kwargs
.
items
()
elif
kwargs
:
raise
TypeError
(
"Either list of fields or keywords"
" can be provided to NamedTuple, not both"
)
return
_make_nmtuple
(
typename
,
fields
)
__new__
.
__text_signature__
=
'($cls, typename, fields=None, /, **kwargs)'
def
_dict_new
(
*
args
,
**
kwargs
):
if
not
args
:
raise
TypeError
(
'TypedDict.__new__(): not enough arguments'
)
cls
,
*
args
=
args
# allow the "cls" keyword be passed
def
_dict_new
(
cls
,
/
,
*
args
,
**
kwargs
):
return
dict
(
*
args
,
**
kwargs
)
_dict_new
.
__text_signature__
=
'($cls, _typename, _fields=None, /, **kwargs)'
def
_typeddict_new
(
*
args
,
total
=
True
,
**
kwargs
):
if
not
args
:
raise
TypeError
(
'TypedDict.__new__(): not enough arguments'
)
cls
,
*
args
=
args
# allow the "cls" keyword be passed
if
args
:
typename
,
*
args
=
args
# allow the "_typename" keyword be passed
elif
'_typename'
in
kwargs
:
typename
=
kwargs
.
pop
(
'_typename'
)
import
warnings
warnings
.
warn
(
"Passing '_typename' as keyword argument is deprecated"
,
DeprecationWarning
,
stacklevel
=
2
)
else
:
raise
TypeError
(
"TypedDict.__new__() missing 1 required positional "
"argument: '_typename'"
)
if
args
:
try
:
fields
,
=
args
# allow the "_fields" keyword be passed
except
ValueError
:
raise
TypeError
(
f'TypedDict.__new__() takes from 2 to 3 '
f'positional arguments but
{
len
(
args
)
+
2
}
'
f'were given'
)
from
None
elif
'_fields'
in
kwargs
and
len
(
kwargs
)
==
1
:
fields
=
kwargs
.
pop
(
'_fields'
)
import
warnings
warnings
.
warn
(
"Passing '_fields' as keyword argument is deprecated"
,
DeprecationWarning
,
stacklevel
=
2
)
else
:
fields
=
None
def
_typeddict_new
(
cls
,
typename
,
fields
=
None
,
/
,
*
,
total
=
True
,
**
kwargs
):
if
fields
is
None
:
fields
=
kwargs
elif
kwargs
:
...
...
@@ -1742,7 +1681,6 @@ def _typeddict_new(*args, total=True, **kwargs):
pass
return
_TypedDictMeta
(
typename
,
(),
ns
)
_typeddict_new
.
__text_signature__
=
'($cls, _typename, _fields=None, /, *, total=True, **kwargs)'
def
_check_fails
(
cls
,
other
):
...
...
Misc/NEWS.d/next/Library/2019-09-17-12-28-27.bpo-38191.1TU0HV.rst
View file @
8fc5839a
Constructors of :class:`~typing.NamedTuple` and :class:`~typing.TypedDict`
types now accept arbitrary keyword argument names, including "cls", "self",
"typename", "_typename", "fields" and "_fields". Passing positional
arguments by keyword is deprecated.
"typename", "_typename", "fields" and "_fields".
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