[Python-ideas] ImportError raised for a circular import

Mahmoud Hashemi mahmoud at hatnote.com
Tue Jun 13 18:10:01 EDT 2017


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.

On Tue, Jun 13, 2017 at 1:43 PM, Chris Angelico <rosuav at gmail.com> wrote:

> On Wed, Jun 14, 2017 at 6:35 AM, Barry <barry at barrys-emacs.org> wrote:
> > On 13 Jun 2017, at 20:13, Antoine Rozo <antoine.rozo at gmail.com> wrote:
> >
> > But circular imports are sometimes needed in modules.
> > For example when you have two classes in two different modules that
> > reference each other in their methods (and because you can't pre-declare
> > classes like in C++).
> >
> >
> > Really? It has always been a strong sign of a design bug in all the
> cases I
> > have ever seen.
> > The example you suggest always fails when I accidentally write it.
> >
> > Pylint will certainly shout loud that this case is an error.
> >
>
> Depends on your definition of "circular". Consider this:
>
> # __init__.py
> from flask import Flask
> app = Flask(__name__)
> from . import views
>
> # views.py
> from . import app
> @app.route("/")
> def home():
>     ...
>
>
> Technically this is circular. During the loading of __init__, views
> will be imported, which then imports something from __init__. But it's
> perfectly well-defined (there's no way that views will ever be the
> first one imported, per the rules of packages) and it makes good
> sense. An error on circular imports, or even a warning, would be very
> annoying here.
>
> ChrisA
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20170613/2f00036a/attachment.html>


More information about the Python-ideas mailing list