-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Allow kwargs for BaseException.__init__ #2760
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
Conversation
I'm at a loss for the failed test, can someone explain what it does? Is this something I broke? |
You need to copy |
Thanks :) I would have expected the build system to do such copying. |
stdlib/2/__builtin__.pyi
Outdated
@@ -1438,7 +1438,7 @@ class BaseException(object): | |||
__cause__: Optional[BaseException] | |||
__context__: Optional[BaseException] | |||
__traceback__: Optional[TracebackType] | |||
def __init__(self, *args: object) -> None: ... | |||
def __init__(self, *args: object, **kwargs) -> None: ... |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Makes sense, thanks.
I recently updated mypy to the latest version and started facing an issue:
Since it used to work, I tried to figure out what went wrong and I found these two pull requests: One to add kwargs, the second one to remove them. What's the correct behavior? Thanks! |
And I merged both of them, that's embarrassing. :) I think removing them is right: BaseException really does not take kwargs
|
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("", foo=123) # TypeError: ValidationError does not take keyword arguments |
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. |
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? |
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. |
Exceptions take keyword args, let mypy accept those.