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_relative_from_import(self):
node = test_utils.extract_node('from os import path #@')
message = testutils.Message('no-absolute-import', node=node)
with self.assertAddsMessages(message):
self.checker.visit_import(node)
'''function foo ...
Args:
xarg1 (int): bla xarg
yarg (float): bla yarg
zarg1 (str): bla zarg
'''
return xarg + yarg
"""
)
with self.assertAddsMessages(
Message(msg_id="missing-param-doc", node=node, args=("xarg, zarg",)),
Message(msg_id="missing-type-doc", node=node, args=("xarg, zarg",)),
Message(msg_id="differing-param-doc", node=node, args=("xarg1, zarg1",)),
Message(msg_id="differing-type-doc", node=node, args=("xarg1, zarg1",)),
):
self.checker.visit_functiondef(node)
node = astroid.extract_node(
"""
def function_foo(xarg, yarg):
'''function foo ...
Args:
yarg1 (float): bla yarg
For the other parameters, see bla.
'''
return xarg + yarg
"""
)
def my_func(self):
"""This is a docstring.
:raises NameError: Never
"""
def ex_func(val):
def inner_func(value):
return OSError(value)
return RuntimeError(val)
raise ex_func('hi') #@
raise NameError('hi')
'''
)
node = raise_node.frame()
with self.assertAddsMessages(
Message(msg_id="missing-raises-doc", node=node, args=("RuntimeError",))
):
# we do NOT expect a warning about the OSError in inner_func!
self.checker.visit_raise(raise_node)
"""
func_node, node = astroid.extract_node(
"""
class Foo(object):
def foo(self): #@
'''int: docstring ...
Raises:
RuntimeError: Always
'''
raise RuntimeError()
return 10 #@
"""
)
with self.assertAddsMessages(
Message(msg_id="missing-return-doc", node=func_node),
Message(msg_id="missing-return-type-doc", node=func_node),
):
self.checker.visit_return(node)
def test_missing_field_on_repeated_warns(self):
node = astroid.extract_node("""
import repeated_pb2
outer = repeated_pb2.Outer()
inner = outer.items.add()
inner.invalid_field = 123 #@
""")
message = pylint.testutils.Message(
'protobuf-undefined-attribute',
node=node.targets[0], args=('invalid_field', 'Inner')
)
with self.assertAddsMessages(message):
self.walk(node.root())
def test_nomember_on_c_extension_error_msg(self):
node = astroid.extract_node(
"""
from coverage import tracer
tracer.CTracer #@
"""
)
message = Message(
"no-member", node=node, args=("Module", "coverage.tracer", "CTracer", "")
)
with self.assertAddsMessages(message):
self.checker.visit_attribute(node)
documentation in a Sphinx style docstring
"""
func_node, node = astroid.extract_node(
"""
class Foo(object):
def foo(self): #@
'''docstring ...
:type: int
'''
return 10 #@
"""
)
with self.assertAddsMessages(
Message(msg_id="missing-return-doc", node=func_node),
Message(msg_id="missing-return-type-doc", node=func_node),
):
self.checker.visit_return(node)
class ClassFoo(object):
def __init__(self, x, y):
'''docstring foo constructor
:param y: bla
missing constructor parameter documentation
'''
pass
"""
)
constructor_node = node.body[0]
with self.assertAddsMessages(
Message(msg_id="missing-param-doc", node=constructor_node, args=("x",)),
Message(msg_id="missing-type-doc", node=constructor_node, args=("x, y",)),
):
self._visit_methods_of_class(node)
def test_nonstandard_logging_module(self):
stmts = test_utils.extract_node("""
from my import logging as blogging #@
blogging.warn('%s' % '%s') #@
""")
self.checker.visit_module(None)
self.checker.visit_import(stmts[0])
with self.assertAddsMessages(Message('logging-not-lazy', node=stmts[1])):
self.checker.visit_callfunc(stmts[1])
def test_dict_iter_method(self):
for meth in ('keys', 'values', 'items'):
node = test_utils.extract_node('x.iter%s() #@' % meth)
message = testutils.Message('dict-iter-method', node=node)
with self.assertAddsMessages(message):
self.checker.visit_callfunc(node)