Implement __new__ signatures, fixes #2073

This commit is contained in:
Dave Halter
2026-04-28 02:25:18 +02:00
parent ff581e8403
commit 3365d0763b
2 changed files with 23 additions and 0 deletions
+12
View File
@@ -378,6 +378,18 @@ class ClassMixin:
return sigs
args = ValuesArguments([])
init_funcs = self.py__call__(args).py__getattribute__('__init__')
if len(init_funcs) == 1:
init = next(iter(init_funcs))
try:
class_context = init.class_context
except AttributeError:
pass
else:
# In the case where we are on object.__init__, we try to use
# __new__.
if class_context.get_root_context().is_builtins_module() \
and init.class_context.name.string_name == "object":
init_funcs = self.py__call__(args).py__getattribute__('__new__')
dataclass_sigs = self._get_dataclass_transform_signatures()
if dataclass_sigs:
+11
View File
@@ -27,6 +27,17 @@ def test_valid_call(Script):
assert_signature(Script, 'bool()', 'bool', column=5)
def test_dunder_new(Script):
# From #2073
s = dedent("""\
from typing import Self
class C:
def __new__(cls, b) -> Self:
pass
C( )""")
assert_signature(Script, s, 'C', 0, line=5, column=2)
class TestSignatures(TestCase):
@pytest.fixture(autouse=True)
def init(self, Script):