]> granicus.if.org Git - python/commitdiff
bpo-29695: Deprecated using bad named keyword arguments in builtings: (#486)
authorSerhiy Storchaka <storchaka@gmail.com>
Sun, 5 Mar 2017 22:53:39 +0000 (00:53 +0200)
committerGitHub <noreply@github.com>
Sun, 5 Mar 2017 22:53:39 +0000 (00:53 +0200)
int(), bool(), float(), list() and tuple().  Specify the value as a
positional argument instead.

12 files changed:
Doc/whatsnew/3.7.rst
Lib/test/test_bool.py
Lib/test/test_float.py
Lib/test/test_int.py
Lib/test/test_list.py
Lib/test/test_tuple.py
Misc/NEWS
Objects/boolobject.c
Objects/floatobject.c
Objects/listobject.c
Objects/longobject.c
Objects/tupleobject.c

index 1f5aa79c87a1e580c27acae16859ad8440268445..5c5ca147e3adf5147cdd524e66e897e3d8aca743 100644 (file)
@@ -169,6 +169,11 @@ Deprecated
   both deprecated in Python 3.4 now emit :exc:`DeprecationWarning`. (Contributed
   by Matthias Bussonnier in :issue:`29576`)
 
+- Using *x* as a keyword argument in :func:`int`, :func:`bool` and
+  :func:`float` and using *sequence* as a keyword argument in :func:`list`
+  and :func:`tuple` are deprecated.  Specify the value as a positional argument
+  instead.  (Contributed by Serhiy Storchaka in :issue:`29695`.)
+
 
 Removed
 =======
index 9f8f0e122c8da44eeefdf6038786bd66e835ab0b..1e6ec1983758b638c5a45235a4e62d7ffd8d7012 100644 (file)
@@ -170,6 +170,10 @@ class BoolTest(unittest.TestCase):
         self.assertIs(bool(""), False)
         self.assertIs(bool(), False)
 
+    def test_keyword_args(self):
+        with self.assertWarns(DeprecationWarning):
+            self.assertIs(bool(x=10), True)
+
     def test_format(self):
         self.assertEqual("%d" % False, "0")
         self.assertEqual("%d" % True, "1")
index ac8473db503b76b32b3e2906ab8959fcb2dc1e64..91e3b7ab49d4d27d0fec421021417ea09ef9edd5 100644 (file)
@@ -208,6 +208,10 @@ class GeneralFloatCases(unittest.TestCase):
         with self.assertWarns(DeprecationWarning):
             self.assertIs(type(FloatSubclass(F())), FloatSubclass)
 
+    def test_keyword_args(self):
+        with self.assertWarns(DeprecationWarning):
+            self.assertEqual(float(x='3.14'), 3.14)
+
     def test_is_integer(self):
         self.assertFalse((1.1).is_integer())
         self.assertTrue((1.).is_integer())
index 14bbd6192a0572df887fe90dcfa4e2775a4120c2..db969672e49257abd56b5302aa35823751c8962c 100644 (file)
@@ -246,9 +246,11 @@ class IntTestCases(unittest.TestCase):
 
     def test_keyword_args(self):
         # Test invoking int() using keyword arguments.
-        self.assertEqual(int(x=1.2), 1)
+        with self.assertWarns(DeprecationWarning):
+            self.assertEqual(int(x=1.2), 1)
         self.assertEqual(int('100', base=2), 4)
-        self.assertEqual(int(x='100', base=2), 4)
+        with self.assertWarns(DeprecationWarning):
+            self.assertEqual(int(x='100', base=2), 4)
         self.assertRaises(TypeError, int, base=10)
         self.assertRaises(TypeError, int, base=0)
 
index aee62dca1c4116865093a74ea908ab1fca87dbf5..48c36913c0c0825ebe2ebbbfe7a6109e348db610 100644 (file)
@@ -16,6 +16,8 @@ class ListTest(list_tests.CommonTest):
         self.assertEqual(list((0, 1, 2, 3)), [0, 1, 2, 3])
         self.assertEqual(list(''), [])
         self.assertEqual(list('spam'), ['s', 'p', 'a', 'm'])
+        self.assertEqual(list(x for x in range(10) if x % 2),
+                         [1, 3, 5, 7, 9])
 
         if sys.maxsize == 0x7fffffff:
             # This test can currently only work on 32-bit machines.
@@ -39,6 +41,11 @@ class ListTest(list_tests.CommonTest):
         x.extend(-y for y in x)
         self.assertEqual(x, [])
 
+    def test_keyword_args(self):
+        with self.assertWarns(DeprecationWarning):
+            self.assertEqual(list(sequence=(x for x in range(10) if x % 2)),
+                             [1, 3, 5, 7, 9])
+
     def test_truth(self):
         super().test_truth()
         self.assertTrue(not [])
index 5d1fcf68a8800d4fe6ffe36a2aaf8441bcece67d..d3663acfd49cf443a0dc41252e2218c4d533950f 100644 (file)
@@ -23,6 +23,13 @@ class TupleTest(seq_tests.CommonTest):
         self.assertEqual(tuple([0, 1, 2, 3]), (0, 1, 2, 3))
         self.assertEqual(tuple(''), ())
         self.assertEqual(tuple('spam'), ('s', 'p', 'a', 'm'))
+        self.assertEqual(tuple(x for x in range(10) if x % 2),
+                         (1, 3, 5, 7, 9))
+
+    def test_keyword_args(self):
+        with self.assertWarns(DeprecationWarning):
+            self.assertEqual(tuple(sequence=(x for x in range(10) if x % 2)),
+                             (1, 3, 5, 7, 9))
 
     def test_truth(self):
         super().test_truth()
index 3ff3cb5b8b65e6db1b23664ffa20ebcb5a7e17e7..d542cf1032e5c1bb1775abe6ebf51acf8f8effa6 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -10,6 +10,10 @@ What's New in Python 3.7.0 alpha 1?
 Core and Builtins
 -----------------
 
+- bpo-29695: Using "x" as a keyword argument in int(), bool() and float() and
+  using "sequence" as a keyword argument in list() and tuple() are deprecated.
+  Specify the value as a positional argument instead.
+
 - bpo-28893: Set correct __cause__ for errors about invalid awaitables
   returned from __aiter__ and __anext__.
 
index 31510bb07d56f70299627ceb2999c27b9339b217..b54b0525dfc8e7dfad53b52376dfe70204d3f8f4 100644 (file)
@@ -48,6 +48,12 @@ bool_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
 
     if (!PyArg_ParseTupleAndKeywords(args, kwds, "|O:bool", kwlist, &x))
         return NULL;
+    if (kwds != NULL && PyDict_GET_SIZE(kwds) != 0) {
+        if (PyErr_Warn(PyExc_DeprecationWarning,
+                "Using 'x' as a keyword argument is deprecated; "
+                "specify the value as a positional argument instead") < 0)
+            return NULL;
+    }
     ok = PyObject_IsTrue(x);
     if (ok < 0)
         return NULL;
index 17a55dd114d42e27a1215b84f4463083fb271e07..3a881c3358b47feabd222a5f55d95bce44f9bbd4 100644 (file)
@@ -1569,6 +1569,12 @@ float_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
         return float_subtype_new(type, args, kwds); /* Wimp out */
     if (!PyArg_ParseTupleAndKeywords(args, kwds, "|O:float", kwlist, &x))
         return NULL;
+    if (kwds != NULL && PyDict_GET_SIZE(kwds) != 0) {
+        if (PyErr_Warn(PyExc_DeprecationWarning,
+                "Using 'x' as a keyword argument is deprecated; "
+                "specify the value as a positional argument instead") < 0)
+            return NULL;
+    }
     /* If it's a string, but not a string subclass, use
        PyFloat_FromString. */
     if (PyUnicode_CheckExact(x))
index edd4a15068adca0fc8477fab0eb3cd96686ce3ce..59f68819d4905316c6a58ce9bc0b0e0141ed5614 100644 (file)
@@ -2297,6 +2297,12 @@ list_init(PyListObject *self, PyObject *args, PyObject *kw)
 
     if (!PyArg_ParseTupleAndKeywords(args, kw, "|O:list", kwlist, &arg))
         return -1;
+    if (arg != NULL && PyTuple_GET_SIZE(args) == 0) {
+        if (PyErr_Warn(PyExc_DeprecationWarning,
+                "Using 'sequence' as a keyword argument is deprecated; "
+                "specify the value as a positional argument instead") < 0)
+            return -1;
+    }
 
     /* Verify list invariants established by PyType_GenericAlloc() */
     assert(0 <= Py_SIZE(self));
index 0bf6cee6c17291735553ab598eba16835281cefe..6c476020775214f47effcd73bd717710b6150131 100644 (file)
@@ -4811,6 +4811,12 @@ long_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
         }
         return PyLong_FromLong(0L);
     }
+    if (PyTuple_GET_SIZE(args) == 0) {
+        if (PyErr_Warn(PyExc_DeprecationWarning,
+                "Using 'x' as a keyword argument is deprecated; "
+                "specify the value as a positional argument instead") < 0)
+            return NULL;
+    }
     if (obase == NULL)
         return PyNumber_Long(x);
 
index 5bcadeb9f645986f628991284e30324d5a4cf0e5..c2696c7dfb799aae71ad2e75e97e81dfeb1c85bd 100644 (file)
@@ -654,6 +654,12 @@ tuple_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
         return tuple_subtype_new(type, args, kwds);
     if (!PyArg_ParseTupleAndKeywords(args, kwds, "|O:tuple", kwlist, &arg))
         return NULL;
+    if (arg != NULL && PyTuple_GET_SIZE(args) == 0) {
+        if (PyErr_Warn(PyExc_DeprecationWarning,
+                "Using 'sequence' as a keyword argument is deprecated; "
+                "specify the value as a positional argument instead") < 0)
+            return NULL;
+    }
 
     if (arg == NULL)
         return PyTuple_New(0);