Skip to content

Commit

Permalink
deny _ method names on modul level
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 15bcfcf commit 43b3628
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 15 deletions.
3 changes: 2 additions & 1 deletion src/RestrictedPython/transformer.py
Expand Up @@ -396,7 +396,8 @@ def check_name(self, node, name, allow_magic_methods=False):
if (name.startswith('_')
and name != '_'
and not (allow_magic_methods
and name in ALLOWED_FUNC_NAMES)):
and name in ALLOWED_FUNC_NAMES
and node.col_offset != 0)):
self.error(
node,
'"{name}" is an invalid variable name because it '
Expand Down
19 changes: 5 additions & 14 deletions tests/transformer/test_functiondef.py
Expand Up @@ -138,19 +138,6 @@ def test_RestrictingNodeTransformer__visit_FunctionDef__8(
_getiter_.reset_mock()


FUNC_NAMES_TEST = """
def __init__(test):
test
"""


@pytest.mark.parametrize(*c_exec)
def test_RestrictingNodeTransformer__module_func_def_name(c_exec):
"""It allows to define functions with the name of allowed magic methods."""
result = c_exec(FUNC_NAMES_TEST)
assert result.errors == ()


BLACKLISTED_FUNC_NAMES_CALL_TEST = """
def __init__(test):
test
Expand All @@ -161,9 +148,13 @@ def __init__(test):

@pytest.mark.parametrize(*c_exec)
def test_RestrictingNodeTransformer__module_func_def_name_call(c_exec):
"""It forbids to use names of allowed magic methods."""
"""It forbids definition and usage of magic methods as functions ...
... at module level.
"""
result = c_exec(BLACKLISTED_FUNC_NAMES_CALL_TEST)
# assert result.errors == ('Line 1: ')
assert result.errors == (
'Line 2: "__init__" is an invalid variable name because it starts with "_"', # NOQA: E501
'Line 5: "__init__" is an invalid variable name because it starts with "_"', # NOQA: E501
)

0 comments on commit 43b3628

Please sign in to comment.