Skip to content

Commit cc6f807

Browse files
gh-116171: Argument Clinic: disallow overriding return converter for __init__ methods (#116172)
1 parent 41baa03 commit cc6f807

File tree

3 files changed

+10
-41
lines changed

3 files changed

+10
-41
lines changed

Lib/test/clinic.test.c

Lines changed: 0 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -5327,47 +5327,6 @@ Test__pyarg_parsestackandkeywords_impl(TestObj *self, PyTypeObject *cls,
53275327
/*[clinic end generated code: output=4fda8a7f2547137c input=fc72ef4b4cfafabc]*/
53285328

53295329

5330-
/*[clinic input]
5331-
Test.__init__ -> long
5332-
Test overriding the __init__ return converter
5333-
[clinic start generated code]*/
5334-
5335-
PyDoc_STRVAR(Test___init____doc__,
5336-
"Test()\n"
5337-
"--\n"
5338-
"\n"
5339-
"Test overriding the __init__ return converter");
5340-
5341-
static long
5342-
Test___init___impl(TestObj *self);
5343-
5344-
static int
5345-
Test___init__(PyObject *self, PyObject *args, PyObject *kwargs)
5346-
{
5347-
int return_value = -1;
5348-
PyTypeObject *base_tp = TestType;
5349-
long _return_value;
5350-
5351-
if ((Py_IS_TYPE(self, base_tp) ||
5352-
Py_TYPE(self)->tp_new == base_tp->tp_new) &&
5353-
!_PyArg_NoPositional("Test", args)) {
5354-
goto exit;
5355-
}
5356-
if ((Py_IS_TYPE(self, base_tp) ||
5357-
Py_TYPE(self)->tp_new == base_tp->tp_new) &&
5358-
!_PyArg_NoKeywords("Test", kwargs)) {
5359-
goto exit;
5360-
}
5361-
_return_value = Test___init___impl((TestObj *)self);
5362-
if ((_return_value == -1) && PyErr_Occurred()) {
5363-
goto exit;
5364-
}
5365-
return_value = PyLong_FromLong(_return_value);
5366-
5367-
exit:
5368-
return return_value;
5369-
}
5370-
53715330
static long
53725331
Test___init___impl(TestObj *self)
53735332
/*[clinic end generated code: output=daf6ee12c4e443fb input=311af0dc7f17e8e9]*/

Lib/test/test_clinic.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2146,6 +2146,14 @@ class Foo "" ""
21462146
expected_error = err_template.format(invalid_kind)
21472147
self.expect_failure(block, expected_error, lineno=3)
21482148

2149+
def test_init_cannot_define_a_return_type(self):
2150+
block = """
2151+
class Foo "" ""
2152+
Foo.__init__ -> long
2153+
"""
2154+
expected_error = "__init__ methods cannot define a return type"
2155+
self.expect_failure(block, expected_error, lineno=1)
2156+
21492157
def test_invalid_getset(self):
21502158
annotations = ["@getter", "@setter"]
21512159
for annotation in annotations:

Tools/clinic/clinic.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5092,6 +5092,8 @@ def resolve_return_converter(
50925092
if forced_converter:
50935093
if self.kind in {GETTER, SETTER}:
50945094
fail(f"@{self.kind.name.lower()} method cannot define a return type")
5095+
if self.kind is METHOD_INIT:
5096+
fail("__init__ methods cannot define a return type")
50955097
ast_input = f"def x() -> {forced_converter}: pass"
50965098
try:
50975099
module_node = ast.parse(ast_input)

0 commit comments

Comments
 (0)