Skip to content

Commit

Permalink
more tests and flake8
Browse files Browse the repository at this point in the history
  • Loading branch information
loechel authored and Michael Howitz committed May 17, 2018
1 parent 4299c95 commit eb9f838
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
6 changes: 2 additions & 4 deletions src/RestrictedPython/transformer.py
Expand Up @@ -452,10 +452,8 @@ def check_import_names(self, node):
=> 'from _a import x' is ok, because '_a' is not added to the scope.
"""
if (isinstance(node, ast.ImportFrom)
and not node.module == '__future__'
and any(
[name.startswith('_') for name in node.module.split('.')]
)):
and not node.module == '__future__'
and any([name.startswith('_') for name in node.module.split('.')])): # NOQA: E501
self.error(node, 'module name starts "_", which is forbidden.')

for name in node.names:
Expand Down
17 changes: 17 additions & 0 deletions tests/transformer/test_import.py
Expand Up @@ -71,6 +71,15 @@ def test_RestrictingNodeTransformer__visit_Import__6_3(c_exec):
)


@pytest.mark.parametrize(*c_exec)
def test_RestrictingNodeTransformer__visit_Import__6_4(c_exec):
"""It allows importing from a module starting with `_`."""
result = c_exec('from _a._b import m as x')
assert result.errors == (
'Line 1: module name starts "_", which is forbidden.',
)


@pytest.mark.parametrize(*c_exec)
def test_RestrictingNodeTransformer__visit_Import__7(c_exec):
"""It denies importing from a module as something starting with `_`."""
Expand Down Expand Up @@ -106,3 +115,11 @@ def test_RestrictingNodeTransformer__visit_Import_star__2(c_exec):
result = c_exec('from a import *')
assert result.errors == ('Line 1: "*" imports are not allowed.',)
assert result.code is None


@pytest.mark.parametrize(*c_exec)
def test_RestrictingNodeTransformer__visit_Import__future__1(c_exec):
"""It allows importing from a module."""
result = c_exec('from __future__ import print_function')
assert result.errors == ()
assert result.code is not None

0 comments on commit eb9f838

Please sign in to comment.