Пример #1
0
def _expand_path(base_dir: pathlib.Path, s: Any) -> pathlib.Path:
    if isinstance(s, pathlib.Path):
        return s
    elif isinstance(s, str):
        return base_dir / s
    else:
        raise dacite.WrongTypeError(pathlib.Path, s)
Пример #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]
Пример #3
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))
Пример #4
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
Пример #5
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
Пример #6
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
Пример #7
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
Пример #8
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
Пример #9
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