Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Import as fix #1

Merged
merged 3 commits into from Feb 24, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 18 additions & 1 deletion supplement/linter.py
Expand Up @@ -290,6 +290,9 @@ def visit_Import(self, node):

def visit_ImportFrom(self, node):
idx = 0
if node.module == '__future__':
return

if node.module:
idx += node.module.count('.') + 1

Expand Down Expand Up @@ -381,7 +384,21 @@ def visit_ListComp(self, node):
def visit_For(self, node):
start = node.body[0]
with self.loop((start.lineno, start.col_offset), self.get_expr_end(node)):
self.generic_visit(node)
self.visit(node.target)
self.visit(node.iter)

oldbranch = self.scope.branch
branch = oldbranch.add_child(Branch(oldbranch))

self.scope.branch = branch
for r in node.body:
self.visit(r)

self.scope.branch = branch.create_orelse()
for r in node.orelse:
self.visit(r)

self.scope.branch = oldbranch

def visit_While(self, node):
with self.loop((node.lineno, node.col_offset), self.get_expr_end(node)):
Expand Down
2 changes: 1 addition & 1 deletion supplement/names.py
Expand Up @@ -354,7 +354,7 @@ def visit_ImportFrom(self, node):
def visit_Import(self, node):
for n in node.names:
if n.asname:
self.names[n.asname] = ModuleName, n.name
self.add_name(n.asname, (ModuleName, n.name, set()), node.lineno)
else:
name, _, tail = n.name.partition('.')
self.add_name(name, (ModuleName, name, set()), node.lineno)
Expand Down
12 changes: 12 additions & 0 deletions tests/test_assist.py
Expand Up @@ -65,6 +65,18 @@ def test_assist_for_relative_star_imported_names(project):

assert result == ['test']

def test_assist_for_import_as(project):
project.create_module('toimport', '''
test = 1
''')

result = do_assist(project, '''
import toimport as foo
foo.te''')

assert result == ['test']


def test_assist_for_function_names(project):
result = do_assist(project, '''
test1 = 1
Expand Down
16 changes: 16 additions & 0 deletions tests/test_linter.py
Expand Up @@ -235,3 +235,19 @@ def foo():
$result$ = lambda boo, bar: boo + bar
''')

def test_lambda_assigned_to_var():
assert_names('''
from __future__ import absolute_import
''')

def test_for_loop_alt_logic_branches():
assert_names('''
def foo():
for _ in []:
bar = 1
break
else:
bar = 2

print bar
''')