[issue42812] @overload-ing method of parent class without actual implementation

Yurii Karabas report at bugs.python.org
Sun Jan 3 12:18:04 EST 2021


Yurii Karabas <1998uriyyo at gmail.com> added the comment:

`mypy` will produce an error on such code:

```
class Parent:
    def foo(self, **kwargs):
        """Argument names of foo vary depending on the child class."""


class Child(Parent):
    @overload
    def foo(self, a, b):
        pass

    def foo(self, **kwargs):
        return super().foo(**kwargs)
```

Result
```
temp.py:10: error: Single overload definition, multiple required
temp.py:10: error: Signature of "foo" incompatible with supertype "Parent"
Found 2 errors in 1 file (checked 1 source file)
```

In case if you want to add an overload for method I would recommend to use such pattern:
```
class Parent:
    def foo(self, **kwargs):
        """Argument names of foo vary depending on the child class."""


class Child(Parent):
    @overload
    def foo(self, a, b):
        pass

    @overload
    def foo(self, **kwargs):
        pass

    def foo(self, **kwargs):
        return super().foo(**kwargs)
```

So signature of `foo` will still match to parent class signature, but it will also have an overloaded variant.

----------

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue42812>
_______________________________________


More information about the Python-bugs-list mailing list