Skip to content

Support for variadiac arguments for inherited classes? #7424

@adewes

Description

@adewes

I'm unsure if this might be a bug, a feature request or if I just failed to find the right spot in the documentation.

What I want to do: I have an abstract base class that defines an interface which derived classes need to implement. There's an abstract method run that requires several mandatory arguments, and may accept additional optional arguments (to override defaults). Currently I'm not sure how I can model this using mypy. Here's some example code that reproduces the issue (ran with Python 3.7 and mypy 0.720:

import abc
  
from typing import Any

class Base(abc.ABC):

    @abc.abstractmethod
    def run(self, model: Any, **kwargs: int) -> None:
        pass

class Child(Base):
    def run(self, model: Any, foo : int) -> None:
        pass

This produces the following error:

mypy_test.py:12: error: Signature of "run" incompatible with supertype "Base"

I sued the **kwargs annotation in reference to this PEP but I realize the use case is different. I couldn't find anything in the mypy documentation about annotating this correctly.

I could of course use **kwargs in the Child class and parse optional parameters from there, but I don't think that's a good solution. Any help to get this working would be highly appreciated!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions