Merge lp:~jtv/launchpad/format-relative-imports into lp:launchpad

Proposed by Jeroen T. Vermeulen
Status: Merged
Approved by: Curtis Hovey
Approved revision: no longer in the source branch.
Merged at revision: 16046
Proposed branch: lp:~jtv/launchpad/format-relative-imports
Merge into: lp:launchpad
Diff against target: 24 lines (+5/-2)
1 file modified
utilities/format-imports (+5/-2)
To merge this branch: bzr merge lp:~jtv/launchpad/format-relative-imports
Reviewer Review Type Date Requested Status
Curtis Hovey (community) code Approve
Review via email: mp+124878@code.launchpad.net

Commit message

Teach format-imports about relative imports, so it doesn't crash on e.g. “from . import main.”

Description of the change

This is a backport of a change I made in MAAS: an import in python can now start with a module/package name or now, for a relative import, with a series of dots. From the script's perspective, either plays the same role in the import statement.

I discussed my solution with wgrant, SteveK, and lifeless but not very much came out. I had a bug and a fix so I took silence as assent.

It's not a particularly forward-looking solution: “.” is defined as local (duh) but there's no cleverness for “..” or “...” etc. — they're /probably/ local imports but the more dots there are, the less likely that becomes. At least my immediate problem of “.” is a clear-cut case. If we ever see more leading dots, that'd be a good time to figure out what the right thing to do is.

Jeroen

To post a comment you must log in.
Revision history for this message
Robert Collins (lifeless) wrote :

+1

Revision history for this message
Curtis Hovey (sinzui) wrote :

Thank you.

review: Approve (code)

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'utilities/format-imports'
2--- utilities/format-imports 2012-02-21 22:46:28 +0000
3+++ utilities/format-imports 2012-09-18 10:19:18 +0000
4@@ -152,7 +152,10 @@
5 comment_regex = re.compile(
6 "(?P<comment>(^#.+\n)+)(^import|^from) +(?P<module>[a-zA-Z0-9_.]+)", re.M)
7 split_regex = re.compile(",\s*")
8-module_base_regex = re.compile("([^. ]+)")
9+
10+# The base part of an import is its leading part: either a series of
11+# dots, or a leading identifier.
12+module_base_regex = re.compile("([.]+|[^. ]+)")
13
14 # Module docstrings are multiline (""") strings that are not indented and are
15 # followed at some point by an import .
16@@ -287,7 +290,7 @@
17 return imports
18
19 LOCAL_PACKAGES = (
20- 'canonical', 'lp', 'launchpad_loggerhead', 'devscripts',
21+ '.', 'canonical', 'lp', 'launchpad_loggerhead', 'devscripts',
22 # database/* have some implicit relative imports.
23 'fti', 'replication', 'preflight', 'security', 'upgrade',
24 )