-
-
Notifications
You must be signed in to change notification settings - Fork 31.7k
Misleading error message when ImportError called with invalid keyword args #65777
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
Comments
>>> ImportError(spam='spam')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: ImportError does not take keyword arguments However, it *does* take keyword arguments: >>> ImportError(name='spam', path='spam')
ImportError() |
Here's a patch with a simple test case. |
Thanks for the review, Eric. Here's a new patch. |
Looks good to me. Thanks for doing this. If no one objects in the meantime, I'll commit this in a few days. |
Eric, do you want me to commit the patch? Should this also be committed to the 3.4 branch? |
Here's a new patch which uses assertRaisesRegex instead of assertRaises. |
The standard error message for this case is: xxx() got an unexpected keyword argument 'foo' I have no idea where that gets generated (a grep didn't teach me anything useful), but I think it would make sense to use that form for the message. I think the fix can be applied to 3.4. |
Thanks for the review, David.
I found two similar messages in the codebase:
But, in ImportError case it can take more than one keyword arguments: ImportError(spam="SPAM", eggs=True) What error message should be printed for the above case? |
Just the first unexpected keyword. That's what happens if you pass more than one unexpected keyword to a python function. |
Assigning to Berker to apply his own patch since Eric has not gotten around to this. |
Why not use PyArg_ParseTupleAndKeywords()? |
I am a little uncomfortable with the empty tuple. It's only used as a placeholder for PyArg_ParseTupleAndKeywords. But this way we can achieve consistent error message. Don't know how to choose. |
Ping. |
Serhiy's patch looks good to me. It would be nice to add a test for multiple invalid keyword arguments: with self.assertRaisesRegex(TypeError, msg):
ImportError('test', invalid='keyword', another=True) Using empty_tuple seems reasonable to me. Brett and Eric, what do you think? |
Left a review, but basically LGTM. |
New changeset 9b8f0db1944f by Serhiy Storchaka in branch '3.5': New changeset 95549f4970d0 by Serhiy Storchaka in branch '3.6': New changeset 59268ac85f4e by Serhiy Storchaka in branch 'default': |
Added a test for multiple invalid keyword arguments, added braces, fixed a leak. But there is other oddity in ImportError constructor (bpo-28289). |
Misc/NEWS
so that it is managed by towncrier #552Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: