Skip to content

[red-knot] Add tests for classes that have incompatible __new__ and __init__ methods #17737

@AlexWaygood

Description

@AlexWaygood

It doesn't look like we yet have any test cases for classes where __new__ and __init__ are incompatible with each other. Construction of these classes always fails at runtime, but we should make sure we handle them correctly anyway. (It looks like we do handle them correctly, but that doesn't mean we shouldn't add tests for it.)

import abc

class Foo:
    def __new__(cls) -> "Foo":
        return object.__new__(cls)
    def __init__(self, x):
        self.x = 42

Foo()  # fails due to incorrect arguments to `__init__`
Foo(42)  # fails due to incorrect arguments to `__new__`

class Foo2:
    def __new__(cls, x) -> "Foo2":
        return object.__new__(cls)
    def __init__(self):
        pass

Foo2()  # fails
Foo2(42)  # also fails

class Foo3(metaclass=abc.ABCMeta):
    def __new__(cls) -> "Foo3":
        return object.__new__(cls)
    def __init__(self, x):
        self.x = 42

Foo3()  # fails
Foo3(42)  # also fails

class Foo4(metaclass=abc.ABCMeta):
    def __new__(cls, x) -> "Foo4":
        return object.__new__(cls)
    def __init__(self):
        pass

Foo4()  # fails
Foo4(42)  # also fails

These tests should be added to crates/red_knot_python_semantic/resources/mdtest/call/constructor.md.

Metadata

Metadata

Assignees

Labels

help wantedContributions especially welcometestingRelated to testing Ruff itselftyMulti-file analysis & type inference

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions