mirror of
https://github.com/python/cpython.git
synced 2025-11-25 21:11:09 +00:00
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:
parent
a23cb72ac8
commit
3e46f9fe05
5 changed files with 45 additions and 1 deletions
20
Objects/clinic/longobject.c.h
generated
20
Objects/clinic/longobject.c.h
generated
|
|
@ -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]*/
|
||||
|
|
|
|||
|
|
@ -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 */
|
||||
};
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue