Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def get_contracts(decorators: List) -> Iterator[Tuple[str, list]]:
for contract in decorators:
if isinstance(contract, TOKENS.ATTR):
name = get_name(contract)
if name not in SUPPORTED_MARKERS:
continue
yield name.split('.')[-1], []
if isinstance(contract, TOKENS.CALL):
if not isinstance(contract.func, TOKENS.ATTR):
continue
name = get_name(contract.func)
if name == 'deal.chain':
yield from get_contracts(contract.args)
if name not in SUPPORTED_CONTRACTS:
continue
yield name.split('.')[-1], contract.args
# infer assigned value
def get_contracts(decorators: List) -> Iterator[Tuple[str, list]]:
for contract in decorators:
if isinstance(contract, TOKENS.ATTR):
name = get_name(contract)
if name not in SUPPORTED_MARKERS:
continue
yield name.split('.')[-1], []
if isinstance(contract, TOKENS.CALL):
if not isinstance(contract.func, TOKENS.ATTR):
continue
name = get_name(contract.func)
if name == 'deal.chain':
yield from get_contracts(contract.args)
if name not in SUPPORTED_CONTRACTS:
continue
yield name.split('.')[-1], contract.args
# infer assigned value
if isinstance(contract, astroid.Name):
assigments = contract.lookup(contract.name)[1]
if not assigments:
continue
# use only the closest assignment
expr = assigments[0]
# can it be not an assignment? IDK
@get_exceptions.register(*TOKENS.CALL)
def handle_call(expr, dive: bool = True) -> Optional[Union[Token, Iterator[Token]]]:
token_info = dict(line=expr.lineno, col=expr.col_offset)
name = get_name(expr.func)
if name and name == 'exit':
return Token(value=SystemExit, **token_info)
# sys.exit()
if isinstance(expr.func, TOKENS.ATTR):
name = get_name(expr.func)
if name and name == 'sys.exit':
return Token(value=SystemExit, **token_info)
# infer function call and check the function body for raises
if dive:
return _exceptions_from_func(expr=expr)
return None
def handle_raise(expr, **kwargs) -> Optional[Token]:
token_info = dict(line=expr.lineno, col=expr.col_offset)
name = get_name(expr.exc)
if not name:
# raised a value, too tricky
if not isinstance(expr.exc, TOKENS.CALL):
return None
# raised an instance of an exception
name = get_name(expr.exc.func)
if not name or name[0].islower():
return None
exc = getattr(builtins, name, name)
token_info['col'] = expr.exc.col_offset
return Token(value=exc, **token_info)
def get_globals(body: list) -> Iterator[Token]:
for expr in traverse(body):
if isinstance(expr, TOKENS.GLOBAL):
yield Token(value='global', line=expr.lineno, col=expr.col_offset)
continue
if isinstance(expr, TOKENS.NONLOCAL):
yield Token(value='nonlocal', line=expr.lineno, col=expr.col_offset)
continue
if type(expr) is ast.Import:
yield Token(value='import', line=expr.lineno, col=expr.col_offset)
continue
if type(expr) is astroid.Import:
yield Token(value='import', line=expr.lineno, col=expr.col_offset)
continue
if type(expr) is ast.ImportFrom:
@get_prints.register(*TOKENS.WITH)
def handle_with(expr) -> Optional[Token]:
token_info = dict(line=expr.lineno, col=expr.col_offset)
for item in expr.items:
if isinstance(item, ast.withitem):
item = item.context_expr
else:
item = item[0]
if _is_pathlib_write(item):
return Token(value='Path.open', **token_info)
if not isinstance(item, TOKENS.CALL):
continue
name = get_name(item.func)
if name == 'open':
if _is_open_to_write(item):
return Token(value='open', **token_info)
return None
def has_returns(body: list) -> bool:
for expr in traverse(body=body):
if isinstance(expr, TOKENS.RETURN + TOKENS.YIELD):
return True
return False
def handle_with(expr) -> Optional[Token]:
token_info = dict(line=expr.lineno, col=expr.col_offset)
for item in expr.items:
if isinstance(item, ast.withitem):
item = item.context_expr
else:
item = item[0]
if _is_pathlib_write(item):
return Token(value='Path.open', **token_info)
if not isinstance(item, TOKENS.CALL):
continue
name = get_name(item.func)
if name == 'open':
if _is_open_to_write(item):
return Token(value='open', **token_info)
return None
@get_prints.register(*TOKENS.CALL)
def handle_call(expr) -> Optional[Token]:
token_info = dict(line=expr.lineno, col=expr.col_offset)
name = get_name(expr.func)
if name in ('print', 'sys.stdout', 'sys.stderr'):
return Token(value=name, **token_info)
if name in ('sys.stdout.write', 'sys.stderr.write'):
return Token(value=name[:-6], **token_info)
if name == 'open':
if _is_open_to_write(expr):
return Token(value='open', **token_info)
if _is_pathlib_write(expr):
return Token(value='Path.open', **token_info)
return None
@get_exceptions.register(*TOKENS.BIN_OP)
def handle_bin_op(expr, **kwargs) -> Optional[Token]:
token_info = dict(line=expr.lineno, col=expr.col_offset)
if isinstance(expr.op, ast.Div) or expr.op == '/':
if isinstance(expr.right, astroid.node_classes.NodeNG):
guesses = infer(expr=expr.right)
token_info['col'] = expr.right.col_offset
for guess in guesses:
if type(guess) is not astroid.Const:
continue
return Token(value=ZeroDivisionError, **token_info)
if isinstance(expr.right, ast.Num) and expr.right.n == 0:
token_info['col'] = expr.right.col_offset
return Token(value=ZeroDivisionError, **token_info)
return None