How to use the deal.linter._extractors.common.Extractor function in deal

To help you get started, we’ve selected a few deal examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github life4 / deal / deal / linter / _extractors / prints.py View on Github external
# built-in
import ast
from typing import Optional

# external
import astroid

# app
from .common import TOKENS, Extractor, Token, get_name, infer


get_prints = Extractor()


@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)
github life4 / deal / deal / linter / _extractors / asserts.py View on Github external
# built-in
import ast
from typing import Optional

# external
import astroid

# app
from .common import TOKENS, Extractor, Token, infer


get_asserts = Extractor()
inner_extractor = Extractor()


@get_asserts.register(*TOKENS.ASSERT)
def handle_assert(expr) -> Optional[Token]:
    # inner_extractor
    for token in inner_extractor.handle(expr=expr.test):
        return token

    # astroid inference
    if hasattr(expr.test, 'infer'):
        for value in infer(expr.test):
            if not isinstance(value, astroid.Const):
                continue
            if value.value:
                continue
github life4 / deal / deal / linter / _extractors / exceptions_stubs.py View on Github external
# built-in
import builtins
from pathlib import Path
from typing import Iterator, Optional, Tuple

# external
import astroid

# app
from .._contract import Category
from .._stub import EXTENSION, StubFile, StubsManager
from .common import Extractor, Token, infer


get_exceptions_stubs = Extractor()


@get_exceptions_stubs.register(astroid.Call)
def handle_astroid_call(expr: astroid.Call, *, dive: bool = True, stubs: StubsManager) -> Iterator[Token]:
    extra = dict(
        line=expr.lineno,
        col=expr.col_offset,
    )
    for value in infer(expr=expr.func):
        if type(value) is not astroid.FunctionDef:
            continue
        module_name, func_name = _get_full_name(expr=value)
        stub = _get_stub(module_name=module_name, expr=value, stubs=stubs)
        if stub is None:
            continue
        names = stub.get(func=func_name, contract=Category.RAISES)
github life4 / deal / deal / linter / _extractors / exceptions.py View on Github external
# built-in
import ast
import builtins
from typing import Iterator, Optional, Union

# external
import astroid

# app
from .common import TOKENS, Extractor, Token, get_name, infer
from .contracts import get_contracts


get_exceptions = Extractor()


@get_exceptions.register(*TOKENS.ASSERT)
def handle_assert(expr, **kwargs) -> Optional[Token]:
    token_info = dict(line=expr.lineno, col=expr.col_offset)
    return Token(value=AssertionError, **token_info)


# explicit raise
@get_exceptions.register(*TOKENS.RAISE)
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):
github life4 / deal / deal / linter / _extractors / returns.py View on Github external
# built-in
import ast
from typing import Optional

# external
import astroid

# app
from .common import TOKENS, Extractor, Token, infer, traverse


get_returns = Extractor()
inner_extractor = Extractor()


def has_returns(body: list) -> bool:
    for expr in traverse(body=body):
        if isinstance(expr, TOKENS.RETURN + TOKENS.YIELD):
            return True
    return False


@get_returns.register(*TOKENS.RETURN)
def handle_returns(expr) -> Optional[Token]:
    # inner_extractor
    for token in inner_extractor.handle(expr=expr.value):
        return token
github life4 / deal / deal / linter / _extractors / imports.py View on Github external
# built-in
import ast

# external
import astroid

# app
from .common import Extractor, Token


get_imports = Extractor()


@get_imports.register(astroid.ImportFrom)
def handle_astroid(expr: astroid.ImportFrom) -> Token:
    token_info = dict(line=expr.lineno, col=expr.col_offset)
    dots = '.' * (expr.level or 0)
    name = expr.modname or ''
    return Token(value=dots + name, **token_info)


@get_imports.register(ast.ImportFrom)
def handle_ast(expr: ast.ImportFrom) -> Token:
    token_info = dict(line=expr.lineno, col=expr.col_offset)
    dots = '.' * expr.level
    name = expr.module or ''
    return Token(value=dots + name, **token_info)
github life4 / deal / deal / linter / _extractors / returns.py View on Github external
# built-in
import ast
from typing import Optional

# external
import astroid

# app
from .common import TOKENS, Extractor, Token, infer, traverse


get_returns = Extractor()
inner_extractor = Extractor()


def has_returns(body: list) -> bool:
    for expr in traverse(body=body):
        if isinstance(expr, TOKENS.RETURN + TOKENS.YIELD):
            return True
    return False


@get_returns.register(*TOKENS.RETURN)
def handle_returns(expr) -> Optional[Token]:
    # inner_extractor
    for token in inner_extractor.handle(expr=expr.value):
        return token

    # astroid inference
github life4 / deal / deal / linter / _extractors / asserts.py View on Github external
# built-in
import ast
from typing import Optional

# external
import astroid

# app
from .common import TOKENS, Extractor, Token, infer


get_asserts = Extractor()
inner_extractor = Extractor()


@get_asserts.register(*TOKENS.ASSERT)
def handle_assert(expr) -> Optional[Token]:
    # inner_extractor
    for token in inner_extractor.handle(expr=expr.test):
        return token

    # astroid inference
    if hasattr(expr.test, 'infer'):
        for value in infer(expr.test):
            if not isinstance(value, astroid.Const):
                continue
            if value.value:
                continue
            return Token(value=value.value, line=expr.lineno, col=expr.col_offset)
github life4 / deal / deal / linter / _extractors / value.py View on Github external
# built-in
import ast
from contextlib import suppress

# external
import astroid

# app
from .common import Extractor, infer


inner_extractor = Extractor()
UNKNOWN = object()


def get_value(expr):
    if isinstance(expr, ast.AST):
        with suppress(ValueError, SyntaxError):
            return ast.literal_eval(expr)

    if isinstance(expr, astroid.node_classes.NodeNG):
        # AttributeError: 'AsStringVisitor3' object has no attribute 'visit_unknown'
        with suppress(AttributeError):  # pragma: no cover
            renderred = expr.as_string()
            with suppress(ValueError, SyntaxError):
                return ast.literal_eval(renderred)

    handler = inner_extractor.handlers.get(type(expr))
github life4 / deal / deal / linter / _extractors / pre.py View on Github external
# built-in
import ast
from typing import Iterator

# external
import astroid

# app
from .common import Extractor, Token, infer
from .contracts import get_contracts
from .value import UNKNOWN, get_value


get_pre = Extractor()


@get_pre.register(astroid.Call)
def handle_call(expr: astroid.Call) -> Iterator[Token]:
    from .._contract import Contract, Category

    args = []
    for subnode in expr.args:
        value = get_value(expr=subnode)
        if value is UNKNOWN:
            return
        args.append(value)

    kwargs = {}
    for subnode in (expr.keywords or ()):
        value = get_value(expr=subnode.value)