You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Improve AttributeError message for partially initialized module #77418
Cyclic import usually leads to an AttributeError "module 'spam' has no attribute 'ham'" which usually is confusing because in normal case 'spam.ham' exists, and the user can have no ideas why it is disappeared.
The proposed PR allows to specialize the AttributeError message for partially initialized module. Any suggestions about the error message?
While I like the idea of this change, the "partially initialized" addition is fairly subtle, and relatively easy to miss.
Perhaps append "(most likely due to a circular import)" to the partially initialized case?:
AttributeError: partially initialized "module 'spam' has no attribute 'ham' (most likely due to a circular import).
Crucially, for folks encountering the error for the first time, that also introduces them to the main phrase they may want to search for: "circular import".
The "most likely" weasel wording stems from the fact that the problem won't always be with the circular import - they may have just straight up referenced the wrong module or the wrong attribute name, so the apparently circular import is an error.
I have applied the Nick's suggestion. Needed to find a place for new test.
The code is copied from PyImport_ImportModuleLevelObject(). I'm not happy from how verbose it is. And testing mod.__spec__._initialized adds relatively large overhead for importing already imported module in PyImport_ImportModuleLevelObject(). Is it possible to invent a faster way for checking whether the module is partially imported?
The main idea that comes to mind is to cache a reference to _frozen_importlib._module_locks in the interpreter state, and do a key lookup in there (since any in-progress import should have a lock allocated to it).
That would be a separate performance issue though - for this issue, we're on an error handling path, so the speed with which the error gets reported isn't critical (although it does technically slow down try/except import fallback chains).
$ cat foo.py
import bar
bar.baz
$ cat bar.py
import foo
baz = 2
$ ./python foo.py
Traceback (most recent call last):
File "foo.py", line 1, in <module>import bar
File "/home/serhiy/py/cpython/bar.py", line 1, in <module>import foo
File "/home/serhiy/py/cpython/foo.py", line 2, in <module>
bar.baz
AttributeError: module 'bar' has no attribute 'baz'
Patched:
$ ./python foo.py
Traceback (most recent call last):
File "foo.py", line 1, in <module>import bar
File "/home/serhiy/py/cpython/bar.py", line 1, in <module>import foo
File "/home/serhiy/py/cpython/foo.py", line 2, in <module>
bar.baz
AttributeError: partially initialized module 'bar' has no attribute 'baz' (most likely due to a circular import)
New changeset 3e429dc by Serhiy Storchaka in branch 'master': bpo-33237: Improve AttributeError message for partially initialized module. (GH-6398) 3e429dc
Activity
serhiy-storchaka commentedon Apr 6, 2018
Cyclic import usually leads to an AttributeError "module 'spam' has no attribute 'ham'" which usually is confusing because in normal case 'spam.ham' exists, and the user can have no ideas why it is disappeared.
The proposed PR allows to specialize the AttributeError message for partially initialized module. Any suggestions about the error message?
ncoghlan commentedon Apr 6, 2018
While I like the idea of this change, the "partially initialized" addition is fairly subtle, and relatively easy to miss.
Perhaps append "(most likely due to a circular import)" to the partially initialized case?:
Crucially, for folks encountering the error for the first time, that also introduces them to the main phrase they may want to search for: "circular import".
The "most likely" weasel wording stems from the fact that the problem won't always be with the circular import - they may have just straight up referenced the wrong module or the wrong attribute name, so the apparently circular import is an error.
brettcannon commentedon Apr 7, 2018
+1 from me for Nick's suggestion.
ncoghlan commentedon Apr 8, 2018
Oops, just realised my suggested text had an extraneous double quote in it due to a copy-and-paste error. Fixed version:
serhiy-storchaka commentedon Apr 9, 2018
I have applied the Nick's suggestion. Needed to find a place for new test.
The code is copied from PyImport_ImportModuleLevelObject(). I'm not happy from how verbose it is. And testing mod.__spec__._initialized adds relatively large overhead for importing already imported module in PyImport_ImportModuleLevelObject(). Is it possible to invent a faster way for checking whether the module is partially imported?
ncoghlan commentedon Apr 10, 2018
The main idea that comes to mind is to cache a reference to
_frozen_importlib._module_locksin the interpreter state, and do a key lookup in there (since any in-progress import should have a lock allocated to it).That would be a separate performance issue though - for this issue, we're on an error handling path, so the speed with which the error gets reported isn't critical (although it does technically slow down try/except import fallback chains).
serhiy-storchaka commentedon Oct 20, 2018
Example:
Patched:
serhiy-storchaka commentedon Oct 30, 2018
New changeset 3e429dc by Serhiy Storchaka in branch 'master':
bpo-33237: Improve AttributeError message for partially initialized module. (GH-6398)
3e429dc