modernize icon indicating copy to clipboard operation
modernize copied to clipboard

import statements should be after any shebang line

Open daira opened this issue 11 years ago • 8 comments

+from __future__ import print_function
 #!/usr/bin/env python

Imports should only be added after any initial lines that are either blank or start with #. That would account for copyright headers etc., as well as shebang lines.

(moved from https://github.com/mitsuhiko/python-modernize/issues/21 )

daira avatar Aug 13 '14 00:08 daira

Taking into account #19, an import of a module that is not already imported at top-level should go:

  • after existing __future__ imports, if any
  • otherwise, after the module docstring, if any
  • otherwise, after any initial lines that are either blank or start with #.

daira avatar Aug 28 '14 14:08 daira

(A module docstring is defined as a string at top-level that precedes any other code.)

daira avatar Aug 28 '14 14:08 daira

This only affects scripts; it can wait until the next release.

daira avatar Oct 14 '14 01:10 daira

I would say that this can be simplified to "just before any real Python code that is not merely a string".

techtonik avatar Oct 16 '14 08:10 techtonik

No, because of #19; I originally filed #19 because of such a case in Tahoe-LAFS.

daira avatar Oct 16 '14 18:10 daira

I think the ordering should be:

  1. Shebang
  2. Encoding comment
  3. Module docstring
  4. __future__ imports
  5. Regular imports
  6. General code

And any combination of the above can be omitted. I think the tricky thing is that it tries to add new imports at the end of the imports block, and that can go wrong if people mix other bits of code into their imports.

takluyver avatar Oct 16 '14 18:10 takluyver

@daira don't treat unicode strings as "merely a string" and that's it.

techtonik avatar Oct 17 '14 17:10 techtonik

https://github.com/mitsuhiko/python-modernize/issues/30 was a duplicate (see also https://github.com/django-wiki/django-wiki/pull/33 ).

daira avatar Nov 25 '14 20:11 daira