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.
This commit is contained in:
Shantanu 2022-12-23 20:30:27 -06:00 committed by GitHub
parent a23cb72ac8
commit 3e46f9fe05
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 45 additions and 1 deletions

View file

@ -467,4 +467,22 @@ skip_optional_kwonly:
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]*/

View file

@ -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 */
};