Skip to content

Multiple inheritance triggers issues that cannot be resolved #4335

@jmh045000

Description

@jmh045000

When using multiple inheritance, the mixing of arguments to different base classes triggers warnings that cannot be resolved.

from typing import Any, Dict, Optional, Sequence, Tuple

class BaseClass(object):
    def __init__(self, foo: str, *args: Sequence[Any], **kwargs: Dict[str, Any]) -> None:
        self.foo = foo
        super().__init__(*args, **kwargs)

class Mixin(object):
    def __init__(self, bar: str, baz: Optional[str]=None, *args: Sequence[Any], **kwargs: Dict[str, Any]) -> None:
        self.bar = bar
        self.baz = baz or 'baz2'
        super().__init__(*args, **kwargs)

class Derived(BaseClass, Mixin):
    def __init__(self, foo: str, bar: str, other: str, *args: Sequence[Any], **kwargs: Dict[str, Any]) -> None:
        self.other = other
        super().__init__(foo=foo, bar=bar, *args, **kwargs)

d = Derived('foo', 'bar', 'other')

print(d.foo)
print(d.bar)
print(d.other)
print(d.baz)
% python3.6 test.py 
foo
bar
other
baz2
% mypy test.py                                                  
test.py:7: error: Too many arguments for "__init__" of "object"
test.py:13: error: Too many arguments for "__init__" of "object"
test.py:18: error: "__init__" of "BaseClass" gets multiple values for keyword argument "foo"
test.py:18: error: Argument 1 to "__init__" of "BaseClass" has incompatible type *Tuple[Sequence[Any], ...]; expected "str"
test.py:18: error: Argument 4 to "__init__" of "BaseClass" has incompatible type **Dict[str, Dict[str, Any]]; expected "str"
test.py:18: error: Argument 4 to "__init__" of "BaseClass" has incompatible type **Dict[str, Dict[str, Any]]; expected Sequence[Any]
test.py:18: error: Argument 3 to "__init__" of "BaseClass" has incompatible type "str"; expected Dict[str, Any]
% mypy --version
mypy 0.521
% python3.6 --version
Python 3.6.3

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions