Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
@set_config(required_base_class=["unittest.case.TestCase:MyTestMixin"])
def test_old_style_classes(self):
# We don't support base class checking on old-style classes, but we
# have to be sure not to fall over at least.
node = astroid.parse('''
class MyClass:
pass
''')
with self.assertNoMessages():
self.walk(node)
@set_config(attr_rgx=re.compile('[A-Z]+'))
def test_property_setters(self):
method = test_utils.extract_node("""
class FooClass(object):
@property
def foo(self): pass
@foo.setter
def FOOSETTER(self): #@
pass
""")
with self.assertNoMessages():
self.checker.visit_function(method)
@set_config(docstring_min_length=2)
def test_long_function_nested_statements_no_docstring(self):
func = astroid.extract_node(
"""
def func(tion):
try:
pass
except:
pass
"""
)
message = Message("missing-function-docstring", node=func)
with self.assertAddsMessages(message):
self.checker.visit_functiondef(func)
@set_config(function_rgx=re.compile('(?:(?PFOO)|(?P[A-Z]+)|(?P[a-z]+))$'))
def test_multi_name_detection_exempt(self):
function_defs = test_utils.extract_node("""
def FOO(): #@
pass
def lower(): #@
pass
def FOO(): #@
pass
def UPPER(): #@
pass
""")
with self.assertAddsMessages(Message('invalid-name', node=function_defs[3], args=('function', 'UPPER', ''))):
for func in function_defs:
self.checker.visit_function(func)
self.checker.leave_module(func.root)
@set_config(docstring_min_length=2)
def test_short_function_no_docstring(self):
func = astroid.extract_node(
"""
def func(tion):
pass"""
)
with self.assertNoMessages():
self.checker.visit_functiondef(func)
@set_config(required_base_class=["unittest.case.TestCase:MyTestMixin"])
def test_error_if_class_is_not_used(self):
node = astroid.parse('''
from unittest import TestCase
class MyClass(TestCase):
pass
''')
expected_msg = Message(
'missing-required-base-class',
node=node.body[-1],
args=('MyClass', 'MyTestMixin'),
)
with self.assertAddsMessages(expected_msg):
self.walk(node)
@set_config(class_rgx=MULTI_STYLE_RE)
def test_multi_name_detection_majority(self):
classes = astroid.extract_node(
"""
class classb(object): #@
pass
class CLASSA(object): #@
pass
class CLASSC(object): #@
pass
"""
)
message = Message(
"invalid-name",
node=classes[0],
args=("Class", "classb", "'(?:(?P[A-Z]+)|(?P[a-z]+))$' pattern"),
)
@set_config(logging_format_style="new")
def test_fstr_not_new_format_style_matching_arguments(self):
msg = "logging-format-interpolation"
args = ("{", "")
self._assert_logging_format_message(msg, "(f'{named}')", args)
@set_config(accept_no_raise_doc=False)
def test_numpy_raises_with_prefix(self):
code_snippet = '''
def my_func(self):
"""This is a numpy docstring.
Raises
------
{prefix}re.error
Sometimes
"""
import re
raise re.error('hi') #@
'''
for prefix in ["~", "!"]:
raise_node = astroid.extract_node(code_snippet.format(prefix=prefix))
with self.assertNoMessages():
@set_config(
method_rgx=MULTI_STYLE_RE,
function_rgx=MULTI_STYLE_RE,
name_group=("function:method",),
)
def test_multi_name_detection_group(self):
function_defs = astroid.extract_node(
"""
class First(object):
def func(self): #@
pass
def FUNC(): #@
pass
""",
module_name="test",
)