Skip to content

Erroneous number of arguments is reported in a constructor call with multiple inheritance  #5299

@gh-andre

Description

@gh-andre

Describe the bug

When multiple inheritance is used, a call of one of the __init__ methods in __mro__ order is reported as if it expects zero arguments, when the referenced constructor expects arguments.

To Reproduce
This is an example with constructors with different arguments.

class XB:
    def __init__(self) -> None:
        # this would call Y.__init__ in Z.__mro__ order, but there's no `name`/`num` to pass in
        #super().__init__()
        pass

class X(XB):
    def __init__(self, name: str) -> None:
        super().__init__()
        print(f"Use {name} for side effects")

class Y:
    def __init__(self, name: str, num: int):
        self.name = name
        self.num = num

# Z.__mro__ = (Z, X, XB, Y, object)
class Z(X, Y):
    def __init__(self, name: str, num: int) -> None:
        super().__init__(name)
        # call Y.__init__
        super(XB, self).__init__(name, num) # <-  error: Expected 0 positional arguments (reportGeneralTypeIssues)
        
z: Z = Z("abc", 123)

Having constructors arranged to have same arguments, which isn't always doable for obvious reasons, reports the same error, just in the different spot.

class XB:
    def __init__(self, name: str, num: int) -> None:
        # calls Y.__init__(name, num)
        super().__init__(name, num) # <- error: Expected 0 positional arguments (reportGeneralTypeIssues)

class X(XB):
    def __init__(self, name: str, num: int) -> None:
        super().__init__(name, num)

class Y:
    def __init__(self, name: str, num: int) -> None:
        super().__init__()
        self.name = name
        self.num = num

# Z.__mro__ = (Z, X, XB, Y, object)
class Z(X, Y):
    def __init__(self, name: str, num: int) -> None:
        super().__init__(name, num)
        
z: Z = Z("abc", 123)

Expected behavior

In both cases the Y.__init__ call requires arguments and should not be reported as an error.

Additional context

Observed in the latest Visual Studio and in pyright v1.1.304 on Windows, with Python 3.10.

Metadata

Metadata

Assignees

No one assigned

    Labels

    addressed in next versionIssue is fixed and will appear in next published versionbugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions