Mercurial > hg
comparison contrib/import-checker.py @ 20386:a05d31b040d7
import-checker: show stdlib and relative imports separately
Make the output more useful for debugging problems.
author | Mads Kiilerich <madski@unity3d.com> |
---|---|
date | Wed, 05 Feb 2014 01:43:51 +0100 |
parents | 4990abb4729d |
children | 466e4c574db0 |
comparison
equal
deleted
inserted
replaced
20385:54235a6ff98a | 20386:a05d31b040d7 |
---|---|
137 Observing this limitation is important as it works around an | 137 Observing this limitation is important as it works around an |
138 annoying lib2to3 bug in relative import rewrites: | 138 annoying lib2to3 bug in relative import rewrites: |
139 http://bugs.python.org/issue19510. | 139 http://bugs.python.org/issue19510. |
140 | 140 |
141 >>> list(verify_stdlib_on_own_line('import sys, foo')) | 141 >>> list(verify_stdlib_on_own_line('import sys, foo')) |
142 ['mixed stdlib and relative imports:\\n foo, sys'] | 142 ['mixed imports\\n stdlib: sys\\n relative: foo'] |
143 >>> list(verify_stdlib_on_own_line('import sys, os')) | 143 >>> list(verify_stdlib_on_own_line('import sys, os')) |
144 [] | 144 [] |
145 >>> list(verify_stdlib_on_own_line('import foo, bar')) | 145 >>> list(verify_stdlib_on_own_line('import foo, bar')) |
146 [] | 146 [] |
147 """ | 147 """ |
148 for node in ast.walk(ast.parse(source)): | 148 for node in ast.walk(ast.parse(source)): |
149 if isinstance(node, ast.Import): | 149 if isinstance(node, ast.Import): |
150 from_stdlib = {} | 150 from_stdlib = {False: [], True: []} |
151 for n in node.names: | 151 for n in node.names: |
152 from_stdlib[n.name] = n.name in stdlib_modules | 152 from_stdlib[n.name in stdlib_modules].append(n.name) |
153 num_std = len([x for x in from_stdlib.values() if x]) | 153 if from_stdlib[True] and from_stdlib[False]: |
154 if num_std not in (len(from_stdlib.values()), 0): | 154 yield ('mixed imports\n stdlib: %s\n relative: %s' % |
155 yield ('mixed stdlib and relative imports:\n %s' % | 155 (', '.join(sorted(from_stdlib[True])), |
156 ', '.join(sorted(from_stdlib.iterkeys()))) | 156 ', '.join(sorted(from_stdlib[False])))) |
157 | 157 |
158 class CircularImport(Exception): | 158 class CircularImport(Exception): |
159 pass | 159 pass |
160 | 160 |
161 | 161 |