Skip to content

Reporting "too many arguments" to __init__ for class which takes *args #4183

@anentropic

Description

@anentropic

Trying to add hints to this:

class ValidationError(Exception):

    def __init__(self, message: str, log_level: int=logging.ERROR, *args, **kwargs) -> None:
        self.message = message
        self.log_level = log_level
        super().__init__(message, log_level, *args, **kwargs)

gives:

error: Too many arguments for "__init__" of "BaseException"

Well, Exception.__init__ supposedly takes *args, **kwargs so how can this ever have "too many arguments"?

If I change my code to:

class ValidationError(Exception):

    def __init__(self, message: str, log_level: int=logging.ERROR) -> None:
        self.message = message
        self.log_level = log_level
        super().__init__(message, log_level)

then no error reported.

I'm actually happy with this change to my code. But does it suggest an issue with mypy?

This also works:

class ValidationError(Exception):

    def __init__(self, message: str, log_level: int=logging.ERROR, whatever: str='WTF') -> None:
        self.message = message
        self.log_level = log_level
        super().__init__(message, log_level, whatever)

So it seems the issue is not with any specific number of arguments, but of mypy not understanding that *args means 'any number'?

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