Skip to content

Allow kwargs for BaseException.__init__#2760

Closed
gladhorn wants to merge 1 commit into
python:masterfrom
gladhorn:master
Closed

Allow kwargs for BaseException.__init__#2760
gladhorn wants to merge 1 commit into
python:masterfrom
gladhorn:master

Conversation

@gladhorn

Copy link
Copy Markdown

Exceptions take keyword args, let mypy accept those.

@gladhorn

Copy link
Copy Markdown
Author

I'm at a loss for the failed test, can someone explain what it does? Is this something I broke?

@srittau

srittau commented Jan 23, 2019

Copy link
Copy Markdown
Collaborator

You need to copy stdlib/2and3/builtins.pyi to stdlib/2/__builtin__.pyi. These two files need to remain synchronized.

@gladhorn

Copy link
Copy Markdown
Author

Thanks :) I would have expected the build system to do such copying.

Comment thread stdlib/2/__builtin__.pyi Outdated
__context__: Optional[BaseException]
__traceback__: Optional[TracebackType]
def __init__(self, *args: object) -> None: ...
def __init__(self, *args: object, **kwargs) -> None: ...

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add : object as the type? We like to keep all arguments typed.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense, thanks.

@donvargax

donvargax commented Feb 9, 2019

Copy link
Copy Markdown

I recently updated mypy to the latest version and started facing an issue:

Too many arguments for "__init__" of "BaseException".

Since it used to work, I tried to figure out what went wrong and I found these two pull requests:

#1704
#2348

One to add kwargs, the second one to remove them. What's the correct behavior? Thanks!

@JelleZijlstra

Copy link
Copy Markdown
Member

And I merged both of them, that's embarrassing. :)

I think removing them is right: BaseException really does not take kwargs

>>> BaseException(x=3)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: exceptions.BaseException does not take keyword arguments

@srittau

srittau commented Feb 9, 2019

Copy link
Copy Markdown
Collaborator

I agree with @JelleZijlstra. Can you give examples of code that is wrongly flagged by mypy without this patch? The example from python/mypy#4183 (referenced from #1704) is actually an example of why keyword arguments should not be allowed:

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

Calling ValidationError with keyword arguments will fail at runtime:

ValidationError("", foo=123)  # TypeError: ValidationError does not take keyword arguments

@donvargax

Copy link
Copy Markdown

I didn't knew BaseException didn't accept kwargs, mypy wasn't showing any errors until I updated it yesterday. If no kwargs is the correct behavior then I'll update my code, I just wanted to point out the conflict between versions, and if you merge this one then the next version will work differently again.

@JelleZijlstra

Copy link
Copy Markdown
Member

Well, if your code is showing errors for code that's working correctly, there might still be a problem somewhere. Can you share the problematic code, and did you confirm that it does work correctly at runtime?

@gladhorn

Copy link
Copy Markdown
Author

At least in my case, I mis-interpreted the class and added the kwargs, which was clearly wrong, it hid a mistake in one exception definition we had, so removing them again is certainly correct.

@gladhorn gladhorn closed this Feb 12, 2019
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 this pull request may close these issues.

4 participants