Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
d = [1, 2, 3]
1 / d # resolved into not a constant
a = b
@deal.raises(KeyError)
def f():
a = 1
a() # resolved not in a function
unknown() # cannot resolve
subf() # resolve
subf2()
"""
tree = astroid.parse(dedent(text))
print(tree.repr_tree())
func_tree = tree.body[-1].body
returns = tuple(r.value for r in get_exceptions(body=func_tree))
assert returns == (ValueError, ZeroDivisionError)
def test_inference_assign():
text = """
def subf():
raise Unknown
@deal.raises(KeyError)
def f():
b = subf()
"""
tree = astroid.parse(dedent(text))
print(tree.repr_tree())
func_tree = tree.body[-1].body
returns = tuple(r.value for r in get_exceptions(body=func_tree))
assert returns == ('Unknown', )
def test_get_exceptions_simple(text, expected):
tree = astroid.parse(text)
print(tree.repr_tree())
returns = tuple(r.value for r in get_exceptions(body=tree.body))
assert returns == expected
tree = ast.parse(text)
print(ast.dump(tree))
returns = tuple(r.value for r in get_exceptions(body=tree.body))
assert returns == expected
def test_get_exceptions_simple(text, expected):
tree = astroid.parse(text)
print(tree.repr_tree())
returns = tuple(r.value for r in get_exceptions(body=tree.body))
assert returns == expected
tree = ast.parse(text)
print(ast.dump(tree))
returns = tuple(r.value for r in get_exceptions(body=tree.body))
assert returns == expected
def test_resolve_doesnt_fail_for_simple_ast():
text = """
def subf():
raise ValueError # explicit raise
@deal.raises(KeyError)
def f():
subf()
"""
tree = ast.parse(dedent(text))
print(ast.dump(tree))
func_tree = tree.body[-1].body
tuple(get_exceptions(body=func_tree))
def test_inference_ok_uncalled():
text = """
def subf():
raise ValueError
@deal.raises(KeyError)
def f():
subf
"""
tree = astroid.parse(dedent(text))
print(tree.repr_tree())
func_tree = tree.body[-1].body
returns = tuple(r.value for r in get_exceptions(body=func_tree))
assert returns == ()
def test_inference_subcontracts():
text = """
@deal.raises(SomeError) # actual contract
@deal.raises(1) # ignore junk
@deal.post(lambda _: 1) # ignore other contracts
def subf():
return 1
@deal.raises(KeyError)
def f():
b = subf()
"""
tree = astroid.parse(dedent(text))
print(tree.repr_tree())
func_tree = tree.body[-1].body
returns = tuple(r.value for r in get_exceptions(body=func_tree))
assert returns == ('SomeError', )
def test_inference_subcalls():
text = """
def subf():
raise ValueError
def subf2():
raise IndexError
@deal.raises(KeyError)
def f():
other(subf(), b=subf2())
"""
tree = astroid.parse(dedent(text))
print(tree.repr_tree())
func_tree = tree.body[-1].body
returns = tuple(r.value for r in get_exceptions(body=func_tree))
assert returns == (ValueError, IndexError)
def _check(self, func: Func, contract: Contract, stubs: StubsManager = None) -> Iterator[Error]:
allowed = contract.exceptions
allowed_types = tuple(exc for exc in allowed if type(exc) is not str)
tokens = [get_exceptions(body=func.body)]
if stubs is not None:
tokens.append(get_exceptions_stubs(body=func.body, stubs=stubs))
for token in chain(*tokens):
if token.value in allowed:
continue
exc = token.value
if isinstance(exc, type):
if issubclass(exc, allowed_types):
continue
exc = exc.__name__
yield Error(
code=self.code,
text=self.message,
value=str(exc),
row=token.line,
col=token.col,