Exemplo n.º 1
0
def _parse_python_version(s: Any) -> PythonVersion:
    if not isinstance(s, str):
        raise dacite.WrongTypeError(str, s)

    try:
        return PythonVersion.parse_short_representation(s)
    except KeyError as e:
        raise dacite.DaciteError(str(e))
Exemplo n.º 2
0
def _parse_mypy_targets(base_dir: pathlib.Path, config: Any) -> List[MypyTarget]:
    if not isinstance(config, list):
        raise dacite.WrongTypeError(List[MypyTarget], config)

    if not all(isinstance(x, dict) for x in config):
        raise dacite.DaciteError("tool.pysen.lint.mypy_targets must be a list of dicts")

    return [_parse_mypy_target(base_dir, x) for x in config]
Exemplo n.º 3
0
def _parse_isort_section_name(s: Any) -> IsortSectionName:
    if not isinstance(s, str):
        raise dacite.WrongTypeError(MypyPreset, s)

    try:
        return IsortSectionName[s.upper()]
    except KeyError:
        raise dacite.DaciteError(f"invalid default_section value: {s}") from None
Exemplo n.º 4
0
def _parse_mypy_preset(s: Any) -> MypyPreset:
    if not isinstance(s, str):
        raise dacite.WrongTypeError(MypyPreset, s)

    try:
        return MypyPreset[s.upper()]
    except KeyError:
        raise dacite.DaciteError(f"invalid mypy_preset value: {s}") from None
Exemplo n.º 5
0
def _parse_mypy_follow_imports(s: Any) -> MypyFollowImports:
    if not isinstance(s, str):
        raise dacite.WrongTypeError(MypyFollowImports, s)

    try:
        return MypyFollowImports[s.upper()]
    except KeyError:
        raise dacite.DaciteError(f"invalid follow_imports value: {s}") from None
Exemplo n.º 6
0
def _parse_plugin_configs(base_dir: pathlib.Path, data: Any) -> List[PluginConfig]:
    if data is None:
        return []

    if not isinstance(data, dict):
        raise dacite.WrongTypeError(dict, data, PLUGIN_PATH_ROOT)

    result: List[PluginConfig] = []

    for key, value in data.items():
        path = f"{PLUGIN_PATH_ROOT}.{key}"

        if not isinstance(value, dict):
            raise dacite.WrongTypeError(dict, value, path)

        # NOTE(igarashi): ensure that `location` is unspecified in pyproject.toml
        if "location" in value:
            raise dacite.DaciteError(f"unknown filed: {path}.location")

        config = dacite.from_dict(
            PluginConfig,
            value,
            config=dacite.Config(
                type_hooks={pathlib.Path: lambda x: _expand_path(base_dir, x)},
                strict=True,
            ),
        )
        assert isinstance(config, PluginConfig)
        config.location = path

        if config.function is None and config.script is None:
            raise dacite.DaciteError(
                "must specify either function or script for plugin field"
            )

        if config.function is not None and config.script is not None:
            raise dacite.DaciteError("only one of function or script must be specified")

        result.append(config)

    return result
Exemplo n.º 7
0
def _migrate_alias_fields(config: Config) -> None:
    if config.lint is not None:
        lint_config = config.lint

        if lint_config.mypy_ignore_packages is not None:
            mypy_modules: Dict[str, MypyModuleOption] = lint_config.mypy_modules or {}

            for m in lint_config.mypy_ignore_packages:
                if m in mypy_modules:
                    raise dacite.DaciteError(
                        f"{m} is configured in both mypy_ignore_packages and mypy_modules"
                    )

                mypy_modules[m] = MypyModuleOption(
                    ignore_errors=True, follow_imports=MypyFollowImports.SKIP
                )

            lint_config.mypy_ignore_packages = None
            lint_config.mypy_modules = mypy_modules
Exemplo n.º 8
0
def _parse_mypy_target(base_dir: pathlib.Path, d: Any) -> MypyTarget:
    if not isinstance(d, dict):
        raise dacite.WrongTypeError(dict, d, "tool.pysen.lint.mypy_targets")

    target = dacite.from_dict(
        MypyTarget,
        d,
        config=dacite.Config(
            type_hooks={pathlib.Path: lambda x: _expand_path(base_dir, x)},
            strict=True,
        ),
    )
    assert isinstance(target, MypyTarget)
    if len(target.paths) == 0:
        raise dacite.DaciteError(
            "invalid mypy_target: each target must have one or more paths"
        )

    return target
Exemplo n.º 9
0
def _parse_mypy_modules(config: Any) -> Dict[str, MypyModuleOption]:
    if not isinstance(config, dict):
        raise dacite.WrongTypeError(Dict[str, MypyModuleOption], config)

    mypy_modules: Dict[str, MypyModuleOption] = {}

    for target_module, option_dict in config.items():
        if not isinstance(target_module, str):
            raise dacite.WrongTypeError(
                str, target_module, "tool.pysen.lint.mypy_modules"
            )

        if not isinstance(option_dict, dict):
            raise dacite.WrongTypeError(
                MypyModuleOption,
                option_dict,
                f'tool.pysen.lint.mypy_modules."{target_module}"',
            )

        try:
            module_option = dacite.from_dict(
                MypyModuleOption,
                option_dict,
                config=dacite.Config(
                    strict=True,
                    type_hooks={
                        MypyPreset: _parse_mypy_preset,
                        MypyFollowImports: _parse_mypy_follow_imports,
                    },
                ),
            )
            assert isinstance(module_option, MypyModuleOption)
            mypy_modules[target_module] = module_option
        except ValueError as e:
            raise dacite.DaciteError(f"invalid mypy_module: {target_module}, {e}")

    return mypy_modules
Exemplo n.º 10
0
def _parse_source(base_dir: pathlib.Path, d: Any) -> Source:
    if isinstance(d, list):
        return Source(includes=[_expand_path(base_dir, x) for x in d])
    elif isinstance(d, dict):

        @dataclasses.dataclass
        class _SourceConfig:
            includes: Optional[List[pathlib.Path]] = None
            include_globs: Optional[List[str]] = None
            excludes: Optional[List[pathlib.Path]] = None
            exclude_globs: Optional[List[str]] = None

        config = dacite.from_dict(
            _SourceConfig,
            d,
            config=dacite.Config(
                type_hooks={pathlib.Path: lambda x: _expand_path(base_dir, x)},
                strict=True,
            ),
        )
        source = Source(includes=config.includes, excludes=config.excludes)
        if config.include_globs is not None:
            for i in config.include_globs:
                source.add_include(i, glob=True, base_dir=base_dir)

        if config.exclude_globs is not None:
            for e in config.exclude_globs:
                source.add_exclude(e, glob=True, base_dir=base_dir)

        if len(source.includes) == 0:
            source.add_include(".", base_dir=base_dir)

        return source

    else:
        raise dacite.DaciteError(f"invalid source value: {d}") from None