Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
anentropic opened this issue Nov 1, 2017 · 3 comments · Fixed by python/typeshed#1704
Closed

Comments

@anentropic
Copy link

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'?

@JelleZijlstra
Copy link
Member

This is a typeshed issue. The problem is that BaseException.__init__ in stdlib/3/builtins.pyi is defined as def __init__(self, *args: object) -> None: .... This means it takes arbitrary args, but not arbitrary kwargs, and your code passes **kwargs. The fix is obvious; could you submit a PR or issue to https://github.com/python/typeshed to fix it?

@anentropic
Copy link
Author

@JelleZijlstra thanks, have opened a PR against typeshed

@Callek
Copy link

Callek commented Jan 28, 2020

For anyone who find this in a search - BaseException.init doesn't take keyword args and this was reverted in python/typeshed#2348

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants