bpo-21720: Improve exception message of __import__()#4113
Conversation
Exception message was unclear when the type of
fromlist parameter is not str:
TypeError: hasattr(): attribute name must be string
|
CI runs under |
|
Thank you for the review, @ncoghlan. I've fixed the |
| self.assertEqual(cm.exception.name, SUBMOD_NAME) | ||
|
|
||
| def test_fromlist_invalid_type(self): | ||
| with self.assertRaises(TypeError) as cm: |
There was a problem hiding this comment.
Add an 'error' filter for BytesWarning.
| "Sequence item 0 in 'from list' must be str, not 'bytes'" | ||
| ) | ||
| with self.assertRaises(TypeError) as cm: | ||
| self.__import__('encodings', fromlist=['aliases', b'codecs']) |
There was a problem hiding this comment.
I think it is better to use 'utf_8' instead of 'codecs'. 'codecs' is just an outer module imported in 'encodings'. It could be imported as '_codecs' for example.
|
@serhiy-storchaka proposed an alternate solution in #4118 (adjusting the importlib behaviour back to more closely matching the behaviour of the old import system), and that seems like a better option to me since it minimises the overhead of the new check, while still avoiding the cryptic bytes warning. |
Exception message was unclear when the type of
fromlist parameter is not str:
https://bugs.python.org/issue21720