On Wed, Jun 14, 2017 at 8:10 AM, Mahmoud Hashemi <mahm...@hatnote.com> wrote:
> I didn't interpret the initial email as wanting an error on *all* circular
> imports. Merely those which are unresolvable. I've definitely helped people
> diagnose circular imports and wished there was an error that called that out
> programmatically, even if it's just a string admonition to check for
> circular imports, appended to the ImportError message.

Oh! That could be interesting. How about a traceback in the import chain?

# a.py
import b
q = 1

# b.py
import c

# c.py
from a import q

c.py will trigger an ImportError, but that could say something like... oh look:

$ python3 a.py
Traceback (most recent call last):
  File "a.py", line 1, in <module>
    import b
  File "/home/rosuav/tmp/asdf/b.py", line 1, in <module>
    import c
  File "/home/rosuav/tmp/asdf/c.py", line 1, in <module>
    from a import q
ImportError: cannot import name 'q' from 'a' (/home/rosuav/tmp/asdf/a.py)

Already happens :)

A bit harder, but also possible, would be to have an AttributeError on
a module recognize that an import is happening, and report a possible
circular import. That'd take some engineering, but it would be
helpful. That'd catch cases like:

# c.py
import a
print(a.q)

Is that what you're looking for?

ChrisA
_______________________________________________
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to