-
-
Notifications
You must be signed in to change notification settings - Fork 30.7k
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
better error message when __all__ contains non-str objects #77113
Comments
I see people wrongly write non-str objects in __all__ and the error message for this case is simply a AttributeError which doesn't reveal the cause directly. >>> from test import *
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: attribute name must be string, not 'C' It would be better to make the cause more obvious, like importlib._bootstrap._handle_fromlist does: Traceback (most recent call last):
File "/root/cpython/Lib/test/test_importlib/import_/test_fromlist.py", line 166, in test_invalid_type_in_all
self.__import__('pkg', fromlist=['*'])
File "/root/cpython/Lib/importlib/_bootstrap.py", line 1094, in __import__
return _handle_fromlist(module, fromlist, _gcd_import)
File "/root/cpython/Lib/importlib/_bootstrap.py", line 1019, in _handle_fromlist
recursive=True)
File "/root/cpython/Lib/importlib/_bootstrap.py", line 1014, in _handle_fromlist
raise TypeError(f"Item in {where} must be str, "
TypeError: Item in pkg.__all__ must be str, not bytes |
s/AttributeError/TypeError |
Agreed. Seems this was an oversight. Do you want to write a patch yourself or left it on to me? |
Added import experts since this issue can be not so trivial as looked at first glance. |
Fixing the message makes sense. I assume this is happening in ceval.c or import.c since cpython/Lib/importlib/_bootstrap.py Line 1021 in 42c35d9
|
I was wondering why the error is not raised by the IMPORT_NAME opcode which predates IMPORT_FROM. It calls _handle_fromlist() from _bootstrap. But in this case the module doesn't have the __path__ attribute and the sanity check was skipped. I'm wondering if it is enough to add the sanity check in _handle_fromlist() for the case when the module doesn't have the __path__ attribute. The Python code could be simpler than the C code. |
I believe the original rationale for the However, giving a better error message for __all__ in ordinary modules also seems like a good reason to follow that branch. |
On other hand, adding checks in Python code will add a slowdown. See bpo-32946 which moves in contrary direction. |
This is only for |
I +1'ed Serhiy's patch for bpo-32946, so we'll have to take that micro-optimisation into account if we decide to rely on the Python level Given that optimisation, it's probably simpler to just enhance the C error path to use the same error message as the Python version, though. |
I was fooled by similarity of Python and C code, but actually Python and C code are not different implementations of the same algorithm, they have different purposes. The purpose of _bootstrap._handle_fromlist() is importing requested submodules first than I'm not very happy that we adds so much C code in ceval.c for handling a subtle error, but seems there is no other way (except keeping all as it is). |
I would like the error message improved. The Python message is obviously much better than the C message. It sends the cause to your eyes. Different messages issued from similar statements are confusing. And due to the non-ideal message is generated by C, the traceback is more limited. tmpdir/
>>> from tmpdir import *
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<frozen importlib._bootstrap>", line 1031, in _handle_fromlist
File "<frozen importlib._bootstrap>", line 1026, in _handle_fromlist
TypeError: Item in tmpdir.__all__ must be str, not int
>>> from tmpdir.a import *
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: attribute name must be string, not 'float' |
Note: 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: