Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_basic_import(self):
python_node = self.get_ast_node(
"""
import foo
"""
)
namespace = dlint.namespace.Namespace.from_module_node(python_node)
result = namespace.name_imported("foo")
expected = True
assert result == expected
def test_basic_as_from_import(self):
python_node = self.get_ast_node(
"""
from foo import bar as baz
"""
)
namespace = dlint.namespace.Namespace.from_module_node(python_node)
result = namespace.name_imported("baz")
expected = True
assert result == expected
def test_basic_from_import(self):
python_node = self.get_ast_node(
"""
from foo import bar
"""
)
namespace = dlint.namespace.Namespace.from_module_node(python_node)
result = namespace.name_imported("bar")
expected = True
assert result == expected
def test_from_module_node_unknown_type(self):
unknown_type = None
with pytest.raises(TypeError):
dlint.namespace.Namespace.from_module_node(unknown_type)
def test_as_import_mismatch(self):
python_node = self.get_ast_node(
"""
import foo as bar
"""
)
namespace = dlint.namespace.Namespace.from_module_node(python_node)
result = namespace.name_imported("foo")
expected = False
assert result == expected
def test_as_from_import_mismatch(self):
python_node = self.get_ast_node(
"""
from foo import bar as baz
"""
)
namespace = dlint.namespace.Namespace.from_module_node(python_node)
result = (
namespace.name_imported("foo")
or namespace.name_imported("bar")
)
expected = False
assert result == expected
def test_basic_as_import(self):
python_node = self.get_ast_node(
"""
import foo as bar
"""
)
namespace = dlint.namespace.Namespace.from_module_node(python_node)
result = namespace.name_imported("bar")
expected = True
assert result == expected
def visit(self, node):
if not self.namespace:
# In MultiNodeVisitor runs this will have already been set since
# the namespace remains the same for each linter. However, during
# testing or single-linter runs we still need to initialize the
# namespace for the linter.
self.namespace = namespace.Namespace.from_module_node(node)
super(BaseLinter, self).visit(node)