#23860 closed New feature (fixed)
Document the preferred imports order
Reported by: | Wojtek Ruszczewski | Owned by: | Tim Graham |
---|---|---|---|
Component: | Documentation | Version: | dev |
Severity: | Normal | Keywords: | |
Cc: | Triage Stage: | Accepted | |
Has patch: | yes | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
I think it would be nice to have the (soft?) convention on imports order documented. It's said that Django has one, but I've recently spent some time trying to figure out what it is and couldn't find any precise info or get a consistent view based on the source.
PEP 8 only asks to put imports on separate lines at the top of the file, guides to make "standard library", "related" and "local" groups, recommends absolute imports (unless dealing with complex package layouts), and discourages the use of "*" in most cases.
A short example on the coding style page would be enough. Things to mention:
- alphabetical order for modules, classes and objects (within groups);
- modules before classes, other objects after classes?
- "import" before "from ... import"?
- line breaking of lengthy imports.
Some conventions from other projects: Google, reddit, OpenStack, Plone.
Change History (11)
comment:1 by , 10 years ago
Triage Stage: | Unreviewed → Accepted |
---|
follow-up: 3 comment:2 by , 10 years ago
Here's a preliminary docs addition with an example chosen to demonstrate what there is to consider. Some highlights: -- blank line after __future__
? -- not grouping imported objects? -- ignore case within words?
comment:3 by , 10 years ago
Replying to wrwrwr:
Here's a preliminary docs addition with an example chosen to demonstrate what there is to consider. Some highlights: -- blank line after
__future__
? -- not grouping imported objects? -- ignore case within words?
If you create a pull-request, that would allow in-line comments.
comment:5 by , 10 years ago
Triage Stage: | Accepted → Ready for checkin |
---|
comment:6 by , 10 years ago
Has patch: | set |
---|---|
Owner: | changed from | to
Patch needs improvement: | set |
Status: | new → assigned |
Triage Stage: | Ready for checkin → Accepted |
As I mentioned on the mailing list, I'd like to take a look at isort and see if it'll work for us so we can automate this and not have to think about it very much.
comment:7 by , 10 years ago
Good idea. I've been playing with isort
a bit and prepared some flake8
/ isort
pre-commit hooks for those on the other side of the looking-glass. Maybe you'll find the isort config useful. The convention would need some amending to make it work: -- putting "import" before "from" statements, -- choosing a slightly different line breaking style, -- sorting objects into constants, classes, modules & other objects (case based).
comment:9 by , 10 years ago
Patch needs improvement: | unset |
---|
comment:10 by , 10 years ago
Resolution: | → fixed |
---|---|
Status: | assigned → closed |
Yes, whatever convention is chosen, it would be good to have on for new or refactored code.