[Python-checkins] gh-100268: Add is_integer method to int (#100439)
gpshead
webhook-mailer at python.org
Fri Dec 23 21:30:33 EST 2022
https://github.com/python/cpython/commit/3e46f9fe05b40ee42009878620f448d3a4b44cb5
commit: 3e46f9fe05b40ee42009878620f448d3a4b44cb5
branch: main
author: Shantanu <12621235+hauntsaninja at users.noreply.github.com>
committer: gpshead <greg at krypto.org>
date: 2022-12-23T18:30:27-08:00
summary:
gh-100268: Add is_integer method to int (#100439)
This improves the lives of type annotation users of `float` - which type checkers implicitly treat as `int|float` because that is what most code actually wants. Before this change a `.is_integer()` method could not be assumed to exist on things annotated as `: float` due to the method not existing on both types.
files:
A Misc/NEWS.d/next/Core and Builtins/2022-12-22-21-56-08.gh-issue-100268.xw_phB.rst
M Doc/library/stdtypes.rst
M Lib/test/test_long.py
M Objects/clinic/longobject.c.h
M Objects/longobject.c
diff --git a/Doc/library/stdtypes.rst b/Doc/library/stdtypes.rst
index 624284f7092f..0ef03035a572 100644
--- a/Doc/library/stdtypes.rst
+++ b/Doc/library/stdtypes.rst
@@ -609,6 +609,12 @@ class`. In addition, it provides a few more methods:
.. versionadded:: 3.8
+.. method:: int.is_integer()
+
+ Returns ``True``. Exists for duck type compatibility with :meth:`float.is_integer`.
+
+ .. versionadded:: 3.12
+
Additional Methods on Float
---------------------------
diff --git a/Lib/test/test_long.py b/Lib/test/test_long.py
index 77b37ca1fa4a..569ab15820e3 100644
--- a/Lib/test/test_long.py
+++ b/Lib/test/test_long.py
@@ -1553,6 +1553,11 @@ def test_from_bytes_small(self):
b = i.to_bytes(2, signed=True)
self.assertIs(int.from_bytes(b, signed=True), i)
+ def test_is_integer(self):
+ self.assertTrue((-1).is_integer())
+ self.assertTrue((0).is_integer())
+ self.assertTrue((1).is_integer())
+
def test_access_to_nonexistent_digit_0(self):
# http://bugs.python.org/issue14630: A bug in _PyLong_Copy meant that
# ob_digit[0] was being incorrectly accessed for instances of a
diff --git a/Misc/NEWS.d/next/Core and Builtins/2022-12-22-21-56-08.gh-issue-100268.xw_phB.rst b/Misc/NEWS.d/next/Core and Builtins/2022-12-22-21-56-08.gh-issue-100268.xw_phB.rst
new file mode 100644
index 000000000000..73d04c19d1cc
--- /dev/null
+++ b/Misc/NEWS.d/next/Core and Builtins/2022-12-22-21-56-08.gh-issue-100268.xw_phB.rst
@@ -0,0 +1 @@
+Add :meth:`int.is_integer` to improve duck type compatibility between :class:`int` and :class:`float`.
diff --git a/Objects/clinic/longobject.c.h b/Objects/clinic/longobject.c.h
index dde49099cf95..206bffdd086a 100644
--- a/Objects/clinic/longobject.c.h
+++ b/Objects/clinic/longobject.c.h
@@ -467,4 +467,22 @@ int_from_bytes(PyTypeObject *type, PyObject *const *args, Py_ssize_t nargs, PyOb
exit:
return return_value;
}
-/*[clinic end generated code: output=bf6074ecf2f32cf4 input=a9049054013a1b77]*/
+
+PyDoc_STRVAR(int_is_integer__doc__,
+"is_integer($self, /)\n"
+"--\n"
+"\n"
+"Returns True. Exists for duck type compatibility with float.is_integer.");
+
+#define INT_IS_INTEGER_METHODDEF \
+ {"is_integer", (PyCFunction)int_is_integer, METH_NOARGS, int_is_integer__doc__},
+
+static PyObject *
+int_is_integer_impl(PyObject *self);
+
+static PyObject *
+int_is_integer(PyObject *self, PyObject *Py_UNUSED(ignored))
+{
+ return int_is_integer_impl(self);
+}
+/*[clinic end generated code: output=e518fe2b5d519322 input=a9049054013a1b77]*/
diff --git a/Objects/longobject.c b/Objects/longobject.c
index 8596ce9797b5..0df3b9a9d564 100644
--- a/Objects/longobject.c
+++ b/Objects/longobject.c
@@ -6168,6 +6168,19 @@ long_long_meth(PyObject *self, PyObject *Py_UNUSED(ignored))
return long_long(self);
}
+/*[clinic input]
+int.is_integer
+
+Returns True. Exists for duck type compatibility with float.is_integer.
+[clinic start generated code]*/
+
+static PyObject *
+int_is_integer_impl(PyObject *self)
+/*[clinic end generated code: output=90f8e794ce5430ef input=7e41c4d4416e05f2]*/
+{
+ Py_RETURN_TRUE;
+}
+
static PyMethodDef long_methods[] = {
{"conjugate", long_long_meth, METH_NOARGS,
"Returns self, the complex conjugate of any int."},
@@ -6186,6 +6199,7 @@ static PyMethodDef long_methods[] = {
INT___GETNEWARGS___METHODDEF
INT___FORMAT___METHODDEF
INT___SIZEOF___METHODDEF
+ INT_IS_INTEGER_METHODDEF
{NULL, NULL} /* sentinel */
};
More information about the Python-checkins
mailing list