[issue23061] Update pep8 to specify explicitly 'module level' imports at top of file
New submission from Ian Lee: Minor update pep8 to specify explicitly that "Imports at top of file" refers specifically to module level imports, per input from Nick Coghlan @ https://github.com/jcrocholl/pep8/pull/304#issuecomment-66939162 ---------- assignee: docs@python components: Documentation files: pep-0008.patch keywords: patch messages: 232708 nosy: IanLee1521, docs@python, ncoghlan priority: normal severity: normal status: open title: Update pep8 to specify explicitly 'module level' imports at top of file type: enhancement Added file: http://bugs.python.org/file37459/pep-0008.patch _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue23061> _______________________________________
Terry J. Reedy added the comment: I read Nick's post and I would like PEP8 to continue to discourage imports at class scope unless there is actually a use case. I discovered yesterday that idlelib.ColorDelegator has several imports at the top of the module and more as the top of the class (of the same name). I cannot think of any good reason for the split. ---------- nosy: +terry.reedy _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue23061> _______________________________________
Ian Lee added the comment: So one concern that was brought up on GitHub was the fact that currently this is not actually followed universally in the Python standard library. In particular there are 636 errors in the standard library ``python pep8.py --select E402 --statistics /usr/lib/python3.4``. The vast majority are due to issues with dunder definitions ``__{all,author,copyright,credits,version,etc...}__`` before the imports. A lesser cause is imports in the middle of files. ``Lib/tokenize.py`` has pretty much all of these issues. In particular ``__all__`` is specifically mentioned that it should be declared AFTER the imports by PEP-8. That said, I would argue this is a good time to clean up that code in the standard library. Additionally, its possible that there might need to be some wording in the PEP about intermixing "try,except,else,finally" and possibly "if,elif,else" into the imports. E.g. ``` try: import unittest2 except: import unittest ``` ``` if sys.platform == 'win32': import foo ``` ---------- _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue23061> _______________________________________
Changes by Ezio Melotti <ezio.melotti@gmail.com>: ---------- nosy: +ezio.melotti _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue23061> _______________________________________
Ian Lee added the comment: I should add that I would be happy to patch the standard libraries to be compliant w.r.t. the imports at top of the files. ---------- _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue23061> _______________________________________
Raymond Hettinger added the comment:
I would argue this is a good time to clean up that code in the standard library.
Please leave any clean-ups to the module maintainers. PEP 8 is something you should do to your own code, rather than something you inflict on other people's code. We generally leave "clean-ups" to the module maintainers rather than creating code churn which can make it more difficult to apply actual bug fixes across versions. Also, GvR has long expressed a preference for "holistic refactoring" where we avoid making superficial changes across the library and reserve those changes for occasions where we're thinking more deeply about a specific piece of code. The basis for this viewpoint has been confirmed a number of times when we've received Pep 8 patches that introduced bugs into code that was previously correct. ---------- nosy: +rhettinger _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue23061> _______________________________________
Changes by Pas <pasthelod@gmail.com>: ---------- nosy: +pas _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue23061> _______________________________________
Ian Lee added the comment: Are there any other concerns with the patch that I would be able to clean up? ---------- nosy: +barry _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue23061> _______________________________________
Joannah Nanjekye <nanjekyejoannah@gmail.com> added the comment: Since this is a PEP related issue, it should be moved to the PEP Github repository here : https://github.com/python/peps . If it still exists. ---------- nosy: +nanjekyejoannah _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue23061> _______________________________________
Terry J. Reedy <tjreedy@udel.edu> added the comment: At least some idlelib imports are delayed to avoid circular import errors, as described by Nick. The patch adds 'Module level' before 'imports'. Pro: techinically correct as only module level imports can be put at the top. Com: might be seen as giving too much license to putting imports elsewhere. Raymond, what do you think? If you are not 'pro' enough to make the change, I think we should close. ---------- _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue23061> _______________________________________
Raymond Hettinger <raymond.hettinger@gmail.com> added the comment: I concur that this should be closed. ---------- resolution: -> rejected stage: -> resolved status: open -> closed _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue23061> _______________________________________
participants (6)
-
Ezio Melotti
-
Ian Lee
-
Joannah Nanjekye
-
Pas
-
Raymond Hettinger
-
Terry J. Reedy