Example #1
0
    def extend(
        draw: Map[Strategy[Domain], Domain],
        base: Strategy[Tuple[Parameter,
                             ...]]) -> Strategy[Tuple[Parameter, ...]]:
        precursors = draw(base)
        precursors_names = set(map(attrgetter('name'), precursors))
        precursors_kinds = to_parameters_by_kind(precursors)
        last_precursor = precursors[-1]

        def is_kind_valid(parameter: Parameter) -> bool:
            if parameter.kind not in (Parameter.positionals_kinds
                                      | Parameter.keywords_kinds):
                return not precursors_kinds[parameter.kind]
            return True

        def normalize(parameter: Parameter) -> Parameter:
            if parameter.kind in Parameter.positionals_kinds:
                if last_precursor.has_default and not parameter.has_default:
                    return Parameter(name=parameter.name,
                                     kind=parameter.kind,
                                     has_default=True)
            return parameter

        follower = draw(
            to_parameters(
                names=identifiers.filter(negate(
                    precursors_names.__contains__)),
                kinds=(parameters_kinds.filter(
                    partial(le, max(precursors_kinds)))),
                has_default_flags=parameters_has_default_flags).filter(
                    is_kind_valid).map(normalize))
        return precursors + (follower, )
Example #2
0
def to_unexpected_kwargs(
    signature: Base, *,
    values: Strategy[Domain] = strategies.none()) -> Strategy[Kwargs]:
    keywords = signature_to_keywords_union(signature)
    is_unexpected = negate(keywords.__contains__)
    return (strategies.dictionaries(identifiers.filter(is_unexpected),
                                    values).filter(bool))
Example #3
0
        plain_non_variadic_signatures
        | to_overloaded_signatures(plain_non_variadic_signatures,
                                   max_size=MAX_ARGUMENTS_COUNT))
non_variadic_signatures_with_unexpected_args = non_variadic_signatures.flatmap(
        to_signature_with_unexpected_args)
non_variadic_signatures_with_unexpected_kwargs = (
    non_variadic_signatures.flatmap(to_signature_with_unexpected_kwargs))


@singledispatch
def is_signature_empty(signature: models.Base) -> bool:
    raise TypeError('Unsupported signature type: {type}.'
                    .format(type=type(signature)))


@is_signature_empty.register(models.Plain)
def is_plain_signature_empty(signature: models.Plain) -> bool:
    return False


@is_signature_empty.register(models.Overloaded)
def is_overloaded_signature_empty(signature: models.Overloaded) -> bool:
    return not signature.signatures


non_empty_signatures = signatures.filter(negate(is_signature_empty))
non_empty_signatures_with_expected_args = non_empty_signatures.flatmap(
        to_signature_with_expected_args)
non_empty_signatures_with_expected_kwargs = non_empty_signatures.flatmap(
        to_signature_with_expected_kwargs)
Example #4
0
from pathlib import Path
from tempfile import NamedTemporaryFile
from typing import Any

from tests.strategies import (invalid_sources, modules_paths, paths)
from tests.utils import negate

modules_paths = modules_paths
non_existent_files_paths = paths.filter(negate(Path.exists))


def to_non_python_file(source: str) -> Any:
    result = NamedTemporaryFile(mode='w')
    result.file.write(source)
    result.file.close()
    return result


non_python_files = invalid_sources.map(to_non_python_file)
Example #5
0
from types import ModuleType

from tests.strategies import modules
from tests.utils import negate


def is_python_module(module: ModuleType) -> bool:
    try:
        module.__file__
    except AttributeError:
        return False
    else:
        return True


python_modules = modules.filter(is_python_module)


def is_python_package(module: ModuleType) -> bool:
    try:
        module.__path__
    except AttributeError:
        return False
    else:
        return True


plain_python_modules = python_modules.filter(negate(is_python_package))
python_packages = modules.filter(is_python_package)
Example #6
0
from tests.utils import (Strategy, negate, pack)


def to_homogeneous_tuples(
        elements: Optional[Strategy[Domain]] = None,
        *,
        min_size: int = 0,
        max_size: Optional[int] = None) -> Strategy[Tuple[Domain, ...]]:
    return (strategies.lists(elements, min_size=min_size,
                             max_size=max_size).map(tuple))


identifiers_characters = strategies.sampled_from(ascii_letters + '_')
identifiers = (strategies.text(identifiers_characters,
                               min_size=1).filter(str.isidentifier).filter(
                                   negate(keyword.iskeyword)))


def to_valid_length(parts: List[str], *, limit: int = 255) -> List[str]:
    result = []
    parts = iter(parts)
    while limit > 0:
        try:
            part = next(parts)
        except StopIteration:
            break
        part = part[:limit]
        result.append(part)
        limit -= len(part) + len(os.sep)
    return result