Skip to content

Commit 2e56424

Browse files
bpo-29695: Remove bad keyword parameters in int(), bool(), float(), list() and tuple(). (#518)
1 parent b76ad51 commit 2e56424

12 files changed

+31
-61
lines changed

Doc/whatsnew/3.7.rst

+4-5
Original file line numberDiff line numberDiff line change
@@ -169,11 +169,6 @@ 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-
177172

178173
Removed
179174
=======
@@ -192,6 +187,10 @@ API and Feature Removals
192187
Python 3.1, and has now been removed. Use the :func:`~os.path.splitdrive`
193188
function instead.
194189

190+
* Functions :func:`bool`, :func:`float`, :func:`list` and :func:`tuple` no
191+
longer take keyword arguments. The first argument of :func:`int` can now
192+
be passes only as positional argument.
193+
195194

196195
Porting to Python 3.7
197196
=====================

Lib/test/test_bool.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -171,8 +171,8 @@ def test_convert(self):
171171
self.assertIs(bool(), False)
172172

173173
def test_keyword_args(self):
174-
with self.assertWarns(DeprecationWarning):
175-
self.assertIs(bool(x=10), True)
174+
with self.assertRaisesRegex(TypeError, 'keyword argument'):
175+
bool(x=10)
176176

177177
def test_format(self):
178178
self.assertEqual("%d" % False, "0")

Lib/test/test_float.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -209,8 +209,8 @@ def __float__(self):
209209
self.assertIs(type(FloatSubclass(F())), FloatSubclass)
210210

211211
def test_keyword_args(self):
212-
with self.assertWarns(DeprecationWarning):
213-
self.assertEqual(float(x='3.14'), 3.14)
212+
with self.assertRaisesRegex(TypeError, 'keyword argument'):
213+
float(x='3.14')
214214

215215
def test_is_integer(self):
216216
self.assertFalse((1.1).is_integer())

Lib/test/test_int.py

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

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

Lib/test/test_list.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,8 @@ def test_basic(self):
4242
self.assertEqual(x, [])
4343

4444
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])
45+
with self.assertRaisesRegex(TypeError, 'keyword argument'):
46+
list(sequence=[])
4847

4948
def test_truth(self):
5049
super().test_truth()

Lib/test/test_tuple.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,8 @@ def test_constructors(self):
2727
(1, 3, 5, 7, 9))
2828

2929
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))
30+
with self.assertRaisesRegex(TypeError, 'keyword argument'):
31+
tuple(sequence=())
3332

3433
def test_truth(self):
3534
super().test_truth()

Misc/NEWS

+2-3
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,8 @@ Core and Builtins
1313
- bpo-29714: Fix a regression that bytes format may fail when containing zero
1414
bytes inside.
1515

16-
- bpo-29695: Using "x" as a keyword argument in int(), bool() and float() and
17-
using "sequence" as a keyword argument in list() and tuple() are deprecated.
18-
Specify the value as a positional argument instead.
16+
- bpo-29695: bool(), float(), list() and tuple() no longer take keyword arguments.
17+
The first argument of int() can now be passes only as positional argument.
1918

2019
- bpo-28893: Set correct __cause__ for errors about invalid awaitables
2120
returned from __aiter__ and __anext__.

Objects/boolobject.c

+3-8
Original file line numberDiff line numberDiff line change
@@ -42,18 +42,13 @@ PyObject *PyBool_FromLong(long ok)
4242
static PyObject *
4343
bool_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
4444
{
45-
static char *kwlist[] = {"x", 0};
4645
PyObject *x = Py_False;
4746
long ok;
4847

49-
if (!PyArg_ParseTupleAndKeywords(args, kwds, "|O:bool", kwlist, &x))
48+
if (!_PyArg_NoKeywords("bool()", kwds))
49+
return NULL;
50+
if (!PyArg_UnpackTuple(args, "bool", 0, 1, &x))
5051
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-
}
5752
ok = PyObject_IsTrue(x);
5853
if (ok < 0)
5954
return NULL;

Objects/floatobject.c

+3-8
Original file line numberDiff line numberDiff line change
@@ -1563,18 +1563,13 @@ static PyObject *
15631563
float_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
15641564
{
15651565
PyObject *x = Py_False; /* Integer zero */
1566-
static char *kwlist[] = {"x", 0};
15671566

15681567
if (type != &PyFloat_Type)
15691568
return float_subtype_new(type, args, kwds); /* Wimp out */
1570-
if (!PyArg_ParseTupleAndKeywords(args, kwds, "|O:float", kwlist, &x))
1569+
if (!_PyArg_NoKeywords("float()", kwds))
1570+
return NULL;
1571+
if (!PyArg_UnpackTuple(args, "float", 0, 1, &x))
15711572
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-
}
15781573
/* If it's a string, but not a string subclass, use
15791574
PyFloat_FromString. */
15801575
if (PyUnicode_CheckExact(x))

Objects/listobject.c

+3-8
Original file line numberDiff line numberDiff line change
@@ -2293,16 +2293,11 @@ static int
22932293
list_init(PyListObject *self, PyObject *args, PyObject *kw)
22942294
{
22952295
PyObject *arg = NULL;
2296-
static char *kwlist[] = {"sequence", 0};
22972296

2298-
if (!PyArg_ParseTupleAndKeywords(args, kw, "|O:list", kwlist, &arg))
2297+
if (!_PyArg_NoKeywords("list()", kw))
2298+
return -1;
2299+
if (!PyArg_UnpackTuple(args, "list", 0, 1, &arg))
22992300
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-
}
23062301

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

Objects/longobject.c

+1-7
Original file line numberDiff line numberDiff line change
@@ -4796,7 +4796,7 @@ long_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
47964796
{
47974797
PyObject *obase = NULL, *x = NULL;
47984798
Py_ssize_t base;
4799-
static char *kwlist[] = {"x", "base", 0};
4799+
static char *kwlist[] = {"", "base", 0};
48004800

48014801
if (type != &PyLong_Type)
48024802
return long_subtype_new(type, args, kwds); /* Wimp out */
@@ -4811,12 +4811,6 @@ 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-
}
48204814
if (obase == NULL)
48214815
return PyNumber_Long(x);
48224816

Objects/tupleobject.c

+3-8
Original file line numberDiff line numberDiff line change
@@ -648,18 +648,13 @@ static PyObject *
648648
tuple_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
649649
{
650650
PyObject *arg = NULL;
651-
static char *kwlist[] = {"sequence", 0};
652651

653652
if (type != &PyTuple_Type)
654653
return tuple_subtype_new(type, args, kwds);
655-
if (!PyArg_ParseTupleAndKeywords(args, kwds, "|O:tuple", kwlist, &arg))
654+
if (!_PyArg_NoKeywords("tuple()", kwds))
655+
return NULL;
656+
if (!PyArg_UnpackTuple(args, "tuple", 0, 1, &arg))
656657
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-
}
663658

664659
if (arg == NULL)
665660
return PyTuple_New(0);

0 commit comments

Comments
 (0)