Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Merge branch 'main' into float-from-number
  • Loading branch information
serhiy-storchaka committed Sep 4, 2022
commit b9e1473d500cf807625eae45a0aed6793dfa8900
2 changes: 1 addition & 1 deletion Lib/test/test_doctest.py
Original file line number Diff line number Diff line change
Expand Up @@ -707,7 +707,7 @@ def non_Python_modules(): r"""

>>> import builtins
>>> tests = doctest.DocTestFinder().find(builtins)
>>> 820 < len(tests) < 850 # approximate number of objects with docstrings
>>> 825 < len(tests) < 845 # approximate number of objects with docstrings
True
>>> real_tests = [t for t in tests if len(t.examples) > 0]
>>> len(real_tests) # objects that actually have doctests
Expand Down
27 changes: 27 additions & 0 deletions Lib/test/test_float.py
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,33 @@ def test_keyword_args(self):
with self.assertRaisesRegex(TypeError, 'keyword argument'):
float(x='3.14')

def test_keywords_in_subclass(self):
class subclass(float):
pass
u = subclass(2.5)
self.assertIs(type(u), subclass)
self.assertEqual(float(u), 2.5)
with self.assertRaises(TypeError):
subclass(x=0)

class subclass_with_init(float):
def __init__(self, arg, newarg=None):
self.newarg = newarg
u = subclass_with_init(2.5, newarg=3)
self.assertIs(type(u), subclass_with_init)
self.assertEqual(float(u), 2.5)
self.assertEqual(u.newarg, 3)

class subclass_with_new(float):
def __new__(cls, arg, newarg=None):
self = super().__new__(cls, arg)
self.newarg = newarg
return self
u = subclass_with_new(2.5, newarg=3)
self.assertIs(type(u), subclass_with_new)
self.assertEqual(float(u), 2.5)
self.assertEqual(u.newarg, 3)

def assertEqualAndType(self, actual, expected_value, expected_type):
self.assertEqual(actual, expected_value)
self.assertIs(type(actual), expected_type)
Expand Down
2 changes: 1 addition & 1 deletion Objects/clinic/complexobject.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Objects/clinic/floatobject.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions Objects/complexobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -694,6 +694,25 @@ complex___format___impl(PyComplexObject *self, PyObject *format_spec)
return _PyUnicodeWriter_Finish(&writer);
}

/*[clinic input]
complex.__complex__

Convert this value to exact type complex.
[clinic start generated code]*/

static PyObject *
complex___complex___impl(PyComplexObject *self)
/*[clinic end generated code: output=e6b35ba3d275dc9c input=3589ada9d27db854]*/
{
if (PyComplex_CheckExact(self)) {
Py_INCREF(self);
return (PyObject *)self;
}
else {
return PyComplex_FromCComplex(self->cval);
}
}


static PyObject *
complex_from_string_inner(const char *s, Py_ssize_t len, void *type)
Expand Down Expand Up @@ -1033,6 +1052,7 @@ complex_from_number(PyTypeObject *type, PyObject *number)
static PyMethodDef complex_methods[] = {
COMPLEX_FROM_NUMBER_METHODDEF
COMPLEX_CONJUGATE_METHODDEF
COMPLEX___COMPLEX___METHODDEF
COMPLEX___GETNEWARGS___METHODDEF
COMPLEX___FORMAT___METHODDEF
{NULL, NULL} /* sentinel */
Expand Down
You are viewing a condensed version of this merge commit. You can view the full changes here.