Skip to content

Commit 58d23e6

Browse files
bpo-29695: Deprecated using bad named keyword arguments in builtings: (python#486)
int(), bool(), float(), list() and tuple(). Specify the value as a positional argument instead.
1 parent d31b28e commit 58d23e6

12 files changed

+65
-2
lines changed

Doc/whatsnew/3.7.rst

+5
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,11 @@ Deprecated
169169
both deprecated in Python 3.4 now emit :exc:`DeprecationWarning`. (Contributed
170170
by Matthias Bussonnier in :issue:`29576`)
171171

172+
- Using *x* as a keyword argument in :func:`int`, :func:`bool` and
173+
:func:`float` and using *sequence* as a keyword argument in :func:`list`
174+
and :func:`tuple` are deprecated. Specify the value as a positional argument
175+
instead. (Contributed by Serhiy Storchaka in :issue:`29695`.)
176+
172177

173178
Removed
174179
=======

Lib/test/test_bool.py

+4
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,10 @@ def test_convert(self):
170170
self.assertIs(bool(""), False)
171171
self.assertIs(bool(), False)
172172

173+
def test_keyword_args(self):
174+
with self.assertWarns(DeprecationWarning):
175+
self.assertIs(bool(x=10), True)
176+
173177
def test_format(self):
174178
self.assertEqual("%d" % False, "0")
175179
self.assertEqual("%d" % True, "1")

Lib/test/test_float.py

+4
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,10 @@ def __float__(self):
208208
with self.assertWarns(DeprecationWarning):
209209
self.assertIs(type(FloatSubclass(F())), FloatSubclass)
210210

211+
def test_keyword_args(self):
212+
with self.assertWarns(DeprecationWarning):
213+
self.assertEqual(float(x='3.14'), 3.14)
214+
211215
def test_is_integer(self):
212216
self.assertFalse((1.1).is_integer())
213217
self.assertTrue((1.).is_integer())

Lib/test/test_int.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -246,9 +246,11 @@ def test_no_args(self):
246246

247247
def test_keyword_args(self):
248248
# Test invoking int() using keyword arguments.
249-
self.assertEqual(int(x=1.2), 1)
249+
with self.assertWarns(DeprecationWarning):
250+
self.assertEqual(int(x=1.2), 1)
250251
self.assertEqual(int('100', base=2), 4)
251-
self.assertEqual(int(x='100', base=2), 4)
252+
with self.assertWarns(DeprecationWarning):
253+
self.assertEqual(int(x='100', base=2), 4)
252254
self.assertRaises(TypeError, int, base=10)
253255
self.assertRaises(TypeError, int, base=0)
254256

Lib/test/test_list.py

+7
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ def test_basic(self):
1616
self.assertEqual(list((0, 1, 2, 3)), [0, 1, 2, 3])
1717
self.assertEqual(list(''), [])
1818
self.assertEqual(list('spam'), ['s', 'p', 'a', 'm'])
19+
self.assertEqual(list(x for x in range(10) if x % 2),
20+
[1, 3, 5, 7, 9])
1921

2022
if sys.maxsize == 0x7fffffff:
2123
# This test can currently only work on 32-bit machines.
@@ -39,6 +41,11 @@ def test_basic(self):
3941
x.extend(-y for y in x)
4042
self.assertEqual(x, [])
4143

44+
def test_keyword_args(self):
45+
with self.assertWarns(DeprecationWarning):
46+
self.assertEqual(list(sequence=(x for x in range(10) if x % 2)),
47+
[1, 3, 5, 7, 9])
48+
4249
def test_truth(self):
4350
super().test_truth()
4451
self.assertTrue(not [])

Lib/test/test_tuple.py

+7
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,13 @@ def test_constructors(self):
2323
self.assertEqual(tuple([0, 1, 2, 3]), (0, 1, 2, 3))
2424
self.assertEqual(tuple(''), ())
2525
self.assertEqual(tuple('spam'), ('s', 'p', 'a', 'm'))
26+
self.assertEqual(tuple(x for x in range(10) if x % 2),
27+
(1, 3, 5, 7, 9))
28+
29+
def test_keyword_args(self):
30+
with self.assertWarns(DeprecationWarning):
31+
self.assertEqual(tuple(sequence=(x for x in range(10) if x % 2)),
32+
(1, 3, 5, 7, 9))
2633

2734
def test_truth(self):
2835
super().test_truth()

Misc/NEWS

+4
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ What's New in Python 3.7.0 alpha 1?
1010
Core and Builtins
1111
-----------------
1212

13+
- bpo-29695: Using "x" as a keyword argument in int(), bool() and float() and
14+
using "sequence" as a keyword argument in list() and tuple() are deprecated.
15+
Specify the value as a positional argument instead.
16+
1317
- bpo-28893: Set correct __cause__ for errors about invalid awaitables
1418
returned from __aiter__ and __anext__.
1519

Objects/boolobject.c

+6
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,12 @@ bool_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
4848

4949
if (!PyArg_ParseTupleAndKeywords(args, kwds, "|O:bool", kwlist, &x))
5050
return NULL;
51+
if (kwds != NULL && PyDict_GET_SIZE(kwds) != 0) {
52+
if (PyErr_Warn(PyExc_DeprecationWarning,
53+
"Using 'x' as a keyword argument is deprecated; "
54+
"specify the value as a positional argument instead") < 0)
55+
return NULL;
56+
}
5157
ok = PyObject_IsTrue(x);
5258
if (ok < 0)
5359
return NULL;

Objects/floatobject.c

+6
Original file line numberDiff line numberDiff line change
@@ -1569,6 +1569,12 @@ float_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
15691569
return float_subtype_new(type, args, kwds); /* Wimp out */
15701570
if (!PyArg_ParseTupleAndKeywords(args, kwds, "|O:float", kwlist, &x))
15711571
return NULL;
1572+
if (kwds != NULL && PyDict_GET_SIZE(kwds) != 0) {
1573+
if (PyErr_Warn(PyExc_DeprecationWarning,
1574+
"Using 'x' as a keyword argument is deprecated; "
1575+
"specify the value as a positional argument instead") < 0)
1576+
return NULL;
1577+
}
15721578
/* If it's a string, but not a string subclass, use
15731579
PyFloat_FromString. */
15741580
if (PyUnicode_CheckExact(x))

Objects/listobject.c

+6
Original file line numberDiff line numberDiff line change
@@ -2297,6 +2297,12 @@ list_init(PyListObject *self, PyObject *args, PyObject *kw)
22972297

22982298
if (!PyArg_ParseTupleAndKeywords(args, kw, "|O:list", kwlist, &arg))
22992299
return -1;
2300+
if (arg != NULL && PyTuple_GET_SIZE(args) == 0) {
2301+
if (PyErr_Warn(PyExc_DeprecationWarning,
2302+
"Using 'sequence' as a keyword argument is deprecated; "
2303+
"specify the value as a positional argument instead") < 0)
2304+
return -1;
2305+
}
23002306

23012307
/* Verify list invariants established by PyType_GenericAlloc() */
23022308
assert(0 <= Py_SIZE(self));

Objects/longobject.c

+6
Original file line numberDiff line numberDiff line change
@@ -4811,6 +4811,12 @@ long_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
48114811
}
48124812
return PyLong_FromLong(0L);
48134813
}
4814+
if (PyTuple_GET_SIZE(args) == 0) {
4815+
if (PyErr_Warn(PyExc_DeprecationWarning,
4816+
"Using 'x' as a keyword argument is deprecated; "
4817+
"specify the value as a positional argument instead") < 0)
4818+
return NULL;
4819+
}
48144820
if (obase == NULL)
48154821
return PyNumber_Long(x);
48164822

Objects/tupleobject.c

+6
Original file line numberDiff line numberDiff line change
@@ -654,6 +654,12 @@ tuple_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
654654
return tuple_subtype_new(type, args, kwds);
655655
if (!PyArg_ParseTupleAndKeywords(args, kwds, "|O:tuple", kwlist, &arg))
656656
return NULL;
657+
if (arg != NULL && PyTuple_GET_SIZE(args) == 0) {
658+
if (PyErr_Warn(PyExc_DeprecationWarning,
659+
"Using 'sequence' as a keyword argument is deprecated; "
660+
"specify the value as a positional argument instead") < 0)
661+
return NULL;
662+
}
657663

658664
if (arg == NULL)
659665
return PyTuple_New(0);

0 commit comments

Comments
 (0)