Skip to content

Commit

Permalink
Fix multi-part imports in the type binder
Browse files Browse the repository at this point in the history
Summary: The root part of the import name ends up visible in python, so track it as such, since readonly doesn't care about anything beyond that.

Differential Revision: D35090635

fbshipit-source-id: 1e93f79
  • Loading branch information
Orvid authored and facebook-github-bot committed Mar 24, 2022
1 parent 83f65b5 commit 82eea4c
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
11 changes: 8 additions & 3 deletions Lib/compiler/readonly/type_binder.py
Original file line number Diff line number Diff line change
Expand Up @@ -1056,10 +1056,15 @@ def visitarg(self, node: ast.arg) -> None:
self.declare(node.arg, node, readonly_value)

def visitalias(self, node: ast.alias) -> None:
root_visible_name = node.name
if node.asname:
self.declare(node.asname, node, MUTABLE)
else:
self.declare(node.name, node, MUTABLE)
root_visible_name = node.asname

# If this is a multi-part import, make the root name
# visible in the current scope.
if (loc := root_visible_name.find(".")) != -1:
root_visible_name = root_visible_name[:loc]
self.declare(root_visible_name, node, MUTABLE)

def visitModule(self, node: ast.Module) -> None:
self.child_scope(ReadonlyModuleBindingScope(node, self))
Expand Down
13 changes: 13 additions & 0 deletions Lib/test/test_compiler/test_readonly/test_type_binder.py
Original file line number Diff line number Diff line change
Expand Up @@ -742,6 +742,19 @@ def somefunc():
errors = self.lint(code)
self.assertEqual(errors.errors, [])

def test_import_local_multipart(self) -> None:
code = """
import e
def somefunc():
import d.f
le = e
ld = d
le = d.f
"""
errors = self.lint(code)
self.assertEqual(errors.errors, [])

def test_readonly_walrus(self) -> None:
code = """
def g(arr):
Expand Down

0 comments on commit 82eea4c

Please sign in to comment.