Skip to content

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

Closed
wants to merge 1 commit into from

Conversation

gladhorn
Copy link

Exceptions take keyword args, let mypy accept those.

@gladhorn
Copy link
Author

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

@srittau
Copy link
Collaborator

srittau commented Jan 23, 2019

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

@gladhorn
Copy link
Author

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

@@ -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: ...
Copy link
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
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
Copy link

donvargax commented Feb 9, 2019

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
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
Copy link
Collaborator

srittau commented Feb 9, 2019

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

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