[Python-Dev] from __future__ syntax changed

Tim Peters tim.peters at gmail.com
Tue Jan 31 06:53:26 CET 2006

[Guido van Rossum]
> It looks like the syntax for "from __future__ import ..." changes in
> Python 2.5.  I was playing around with Cheetah, and it stumbled over a
> file with the following structure:
>   # comments
>   """ docstring """
>   __author__ = "..."
>   __version__ = "..."
>   from __future__ import generators
> Python 2.2 throug 2.4 accept this fine.
> Perhaps the AST tree enforces stricter syntax for what can precede a
> future statement?  I think this should be fixed.

Peculiar.  It wasn't intended that be allowed; PEP 236 spelled this out:

In addition, all future_statments must appear near the top of the
module.  The only lines that can appear before a future_statement are:

    + The module docstring (if any).
    + Comments.
    + Blank lines.
    + Other future_statements.

That isn't frivolous, since a __future__ gimmick may affect the
legality of binding statements (as in your example) or other import
statements, etc -- it was deliberate that only blank lines, comments
and (maybe) a docstring could appear before the first future statment.
 I'd call this a bug in 2.2-2.4.

Note too that Tools/scripts/cleanfuture.py in 2.4 doesn't change that
example, because it doesn't believe it contains any __future__
statements (cleanfuture.py follows the PEP's syntax).

More information about the Python-Dev mailing list