classification
Title: Improve AttributeError on circular imports of submodules
Type: enhancement Stage: resolved
Components: Interpreter Core Versions:
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: FFY00, lukasz.langa, pablogsal
Priority: normal Keywords: patch

Created on 2021-07-23 00:51 by FFY00, last changed 2021-07-25 01:27 by FFY00. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 27299 merged FFY00, 2021-07-23 00:52
PR 27331 merged pablogsal, 2021-07-24 13:12
PR 27338 merged FFY00, 2021-07-24 18:22
PR 27344 open FFY00, 2021-07-25 01:27
Messages (6)
msg398021 - (view) Author: Filipe Laíns (FFY00) * (Python triager) Date: 2021-07-23 00:51
Consider the following


$ tree a
a
├── b
│   ├── c.py
│   └── __init__.py
└── __init__.py

1 directory, 3 files
$ cat a/b/__init__.py
import a.b.c
$ cat a/b/c.py
import a.b

a.b
$ python
Python 3.9.6 (default, Jun 30 2021, 10:22:16)
[GCC 11.1.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import a.b
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/anubis/test/a/b/__init__.py", line 1, in <module>
    import a.b.c
  File "/home/anubis/test/a/b/c.py", line 3, in <module>
    a.b
AttributeError: module 'a' has no attribute 'b'


This happens because even though the module `a` is initialized, the `b` attribute is not set due to the circular import. When a module is partially initialized, we get a more helpful error description hinting that we cannot access the attribute because the module is partially initialized, we could have something similar here.
msg398134 - (view) Author: Łukasz Langa (lukasz.langa) * (Python committer) Date: 2021-07-24 09:50
New changeset 8072a1181dd64135f700b44372fbf7bf91e68072 by Filipe Laíns in branch 'main':
bpo-44717: improve AttributeError on circular imports of submodules (GH-27299)
https://github.com/python/cpython/commit/8072a1181dd64135f700b44372fbf7bf91e68072
msg398135 - (view) Author: Łukasz Langa (lukasz.langa) * (Python committer) Date: 2021-07-24 09:51
Thanks a lot for your report and patch, Filipe! ✨ 🍰 ✨
msg398146 - (view) Author: Pablo Galindo Salgado (pablogsal) * (Python committer) Date: 2021-07-24 12:58
There is a buildbot failure after merging this PR:

https://buildbot.python.org/all/#/builders/464/builds/574

If is not fixed in 24 we will need to revert per our buildbot policy
msg398154 - (view) Author: Pablo Galindo Salgado (pablogsal) * (Python committer) Date: 2021-07-24 13:33
New changeset 3eae8f20d7b6f88d3618b0afc94893165a914022 by Pablo Galindo Salgado in branch 'main':
Revert "bpo-44717: improve AttributeError on circular imports of submodules (GH-27299)" (GH-27331)
https://github.com/python/cpython/commit/3eae8f20d7b6f88d3618b0afc94893165a914022
msg398176 - (view) Author: Pablo Galindo Salgado (pablogsal) * (Python committer) Date: 2021-07-24 22:45
New changeset 0a8ae8a50a0fea3d39ec49b220a5c7a5b70e36f8 by Filipe Laíns in branch 'main':
bpo-44717: improve AttributeError on circular imports of submodules (GH-27338)
https://github.com/python/cpython/commit/0a8ae8a50a0fea3d39ec49b220a5c7a5b70e36f8
History
Date User Action Args
2021-07-25 01:27:23FFY00setpull_requests: + pull_request25886
2021-07-24 22:45:30pablogsalsetstatus: open -> closed
resolution: fixed
stage: patch review -> resolved
2021-07-24 22:45:15pablogsalsetmessages: + msg398176
2021-07-24 18:22:52FFY00setpull_requests: + pull_request25881
2021-07-24 13:33:12pablogsalsetmessages: + msg398154
2021-07-24 13:12:43pablogsalsetstage: resolved -> patch review
pull_requests: + pull_request25874
2021-07-24 12:58:48pablogsalsetstatus: closed -> open
resolution: fixed -> (no value)
messages: + msg398146
2021-07-24 09:51:07lukasz.langasetstatus: open -> closed
resolution: fixed
messages: + msg398135

stage: patch review -> resolved
2021-07-24 09:50:22lukasz.langasetnosy: + lukasz.langa
messages: + msg398134
2021-07-23 00:52:52FFY00setkeywords: + patch
stage: patch review
pull_requests: + pull_request25842
2021-07-23 00:51:19FFY00create