From 43b30ec86e3c286c948888088a52aa4a8d91ce45 Mon Sep 17 00:00:00 2001 From: Pieter Pas Date: Tue, 29 Aug 2023 02:57:49 +0200 Subject: [PATCH] Apply mypy changes --- src/py_build_cmake/build.py | 4 ++-- src/py_build_cmake/commands/cmd_runner.py | 4 ++-- src/py_build_cmake/commands/try_run.py | 2 +- src/py_build_cmake/common/util.py | 14 +++++++------- src/py_build_cmake/config/config_options.py | 16 +++++++--------- src/py_build_cmake/config/dynamic.py | 5 ++--- src/py_build_cmake/config/load.py | 6 +++--- src/py_build_cmake/config/quirks.py | 3 ++- src/py_build_cmake/export/native_tags.py | 2 +- src/py_build_cmake/help.py | 2 +- 10 files changed, 28 insertions(+), 30 deletions(-) diff --git a/src/py_build_cmake/build.py b/src/py_build_cmake/build.py index f8bc459..1255f73 100644 --- a/src/py_build_cmake/build.py +++ b/src/py_build_cmake/build.py @@ -6,7 +6,7 @@ from pathlib import Path from typing import Any -from distlib.wheel import Wheel # type: ignore +from distlib.wheel import Wheel # type: ignore[import] from .commands.cmake import ( CMakeBuildSettings, @@ -246,7 +246,7 @@ def build_wheel_in_dir( return self.create_wheel(paths, cfg, cmake_cfg, pkg_info) @staticmethod - def get_pkg_info(cfg: Config | ComponentConfig, module: Module): + def get_pkg_info(cfg: Config | ComponentConfig, module: Module | None): return PackageInfo( version=str(cfg.standard_metadata.version), package_name=cfg.package_name, diff --git a/src/py_build_cmake/commands/cmd_runner.py b/src/py_build_cmake/commands/cmd_runner.py index c3da511..d739194 100644 --- a/src/py_build_cmake/commands/cmd_runner.py +++ b/src/py_build_cmake/commands/cmd_runner.py @@ -6,11 +6,11 @@ from subprocess import CalledProcessError from subprocess import run as sp_run -from distlib.version import NormalizedVersion # type: ignore +from distlib.version import NormalizedVersion # type: ignore[import] class CommandRunner: - def __init__(self, verbose=False, dry=False): + def __init__(self, verbose: bool = False, dry: bool = False): self.verbose = verbose self.dry = dry diff --git a/src/py_build_cmake/commands/try_run.py b/src/py_build_cmake/commands/try_run.py index 302ceb2..54a9180 100644 --- a/src/py_build_cmake/commands/try_run.py +++ b/src/py_build_cmake/commands/try_run.py @@ -1,6 +1,6 @@ from __future__ import annotations -from distlib.version import NormalizedVersion +from distlib.version import NormalizedVersion # type: ignore[import] from ..common import Config from ..common.util import get_os_name diff --git a/src/py_build_cmake/common/util.py b/src/py_build_cmake/common/util.py index e8b2939..9bc6562 100644 --- a/src/py_build_cmake/common/util.py +++ b/src/py_build_cmake/common/util.py @@ -2,15 +2,15 @@ import platform import re -from typing import Any, Sequence, cast +import sys +from typing import Sequence, cast -OSIdentifier: Any -try: +if sys.version_info < (3, 8): + OSIdentifier = str +else: from typing import Literal OSIdentifier = Literal["linux", "windows", "mac"] -except ImportError: - OSIdentifier = str def get_os_name() -> OSIdentifier: @@ -26,12 +26,12 @@ def get_os_name() -> OSIdentifier: return cast(OSIdentifier, osname) -def normalize_name_wheel_pep_427(name): +def normalize_name_wheel_pep_427(name: str) -> str: """https://www.python.org/dev/peps/pep-0427/#escaping-and-unicode""" return re.sub(r"[^\w\d.]+", "_", name, flags=re.UNICODE) -def normalize_name_wheel(name): +def normalize_name_wheel(name: str) -> str: """https://packaging.python.org/en/latest/specifications/binary-distribution-format/#escaping-and-unicode""" return re.sub(r"[-_.]+", "_", name).lower() diff --git a/src/py_build_cmake/config/config_options.py b/src/py_build_cmake/config/config_options.py index 78aa3b3..74d614b 100644 --- a/src/py_build_cmake/config/config_options.py +++ b/src/py_build_cmake/config/config_options.py @@ -158,14 +158,14 @@ def get_default( ) -> DefaultValueWrapper | None: return DefaultValueWrapper(self.value) - def get_name(self): + def get_name(self) -> str: if isinstance(self.value, bool): return str(self.value).lower() return repr(self.value) class NoDefaultValue(DefaultValue): - def __init__(self, name="none"): + def __init__(self, name: str = "none"): self.name = name def get_default( @@ -178,7 +178,7 @@ def get_default( ) -> DefaultValueWrapper | None: return None - def get_name(self): + def get_name(self) -> str: return self.name @@ -198,7 +198,7 @@ def get_default( msg = f"{pth2str(cfgpath)} requires a value" raise MissingDefaultError(msg) - def get_name(self): + def get_name(self) -> str: return "required" @@ -220,11 +220,11 @@ def get_default( if self.relative: absoptpath = joinpth(optpath, ("^", *absoptpath)) abscfgpath = joinpth(cfgpath, ("^", *abscfgpath)) - opt = rootopts.get(absoptpath) - if opt is None: + ref_opt = rootopts.get(absoptpath) + if ref_opt is None: msg = f"DefaultValue: reference to nonexisting option {pth2str(absoptpath)}" raise ValueError(msg) - return opt.update_default(rootopts, cfg, abscfgpath, absoptpath) + return ref_opt.update_default(rootopts, cfg, abscfgpath, absoptpath) def get_name(self) -> str: r = pth2str(self.path).replace("^", "..") @@ -307,8 +307,6 @@ def get(self, key: ConfPath, default=None): def setdefault(self, path: ConfPath, default: Any): tgt = self[parent(path)] - if tgt.sub is None: - tgt.sub = {} return tgt.sub.setdefault(basename(path), default) def contains(self, path: ConfPath): diff --git a/src/py_build_cmake/config/dynamic.py b/src/py_build_cmake/config/dynamic.py index e58c40a..2165819 100644 --- a/src/py_build_cmake/config/dynamic.py +++ b/src/py_build_cmake/config/dynamic.py @@ -39,7 +39,7 @@ from contextlib import contextmanager from pathlib import Path -import distlib.version +import distlib.version # type: ignore[import] from ..common import ( ConfigError, @@ -74,6 +74,7 @@ def get_docstring_and_version_via_ast(mod_filename: Path): # read as bytes to enable custom encodings with mod_filename.open("rb") as f: node = ast.parse(f.read()) + version = None for child in node.body: # Only use the version from the given module if it's a simple # string assignment to __version__ @@ -87,8 +88,6 @@ def get_docstring_and_version_via_ast(mod_filename: Path): ): version = child.value.s break - else: - version = None return ast.get_docstring(node), version diff --git a/src/py_build_cmake/config/load.py b/src/py_build_cmake/config/load.py index 92709cb..69363e7 100644 --- a/src/py_build_cmake/config/load.py +++ b/src/py_build_cmake/config/load.py @@ -9,7 +9,7 @@ from typing import Any, cast import pyproject_metadata -from distlib.util import normalize_name # type: ignore +from distlib.util import normalize_name # type: ignore[import] from .. import __version__ from ..common import Config, ConfigError @@ -23,9 +23,9 @@ from .quirks import config_quirks try: - import tomllib as toml_ # type: ignore + import tomllib as toml_ # type: ignore[import,unused-ignore] except ImportError: - import tomli as toml_ # type: ignore + import tomli as toml_ # type: ignore[import,no-redef,unused-ignore] logger = logging.getLogger(__name__) diff --git a/src/py_build_cmake/config/quirks.py b/src/py_build_cmake/config/quirks.py index e48361d..7c103ea 100644 --- a/src/py_build_cmake/config/quirks.py +++ b/src/py_build_cmake/config/quirks.py @@ -11,7 +11,7 @@ from pathlib import Path from typing import Any -from distlib.util import get_platform as get_platform_dashes # type: ignore +from distlib.util import get_platform as get_platform_dashes # type: ignore[import] from ..common.util import ( archflags_to_platform_tag, @@ -206,6 +206,7 @@ def config_quirks_mac(config: ConfigNode): def config_quirks_pypy(config: ConfigNode): + assert config.sub is not None if sys.version_info < (3, 8): with contextlib.suppress(KeyError): del config.sub["stubgen"] diff --git a/src/py_build_cmake/export/native_tags.py b/src/py_build_cmake/export/native_tags.py index 49c31ca..a186345 100644 --- a/src/py_build_cmake/export/native_tags.py +++ b/src/py_build_cmake/export/native_tags.py @@ -11,7 +11,7 @@ from importlib.machinery import EXTENSION_SUFFIXES from typing import Dict, List -from distlib.util import get_platform as get_platform_dashes # type: ignore +from distlib.util import get_platform as get_platform_dashes # type: ignore[import] from ..common.util import platform_to_platform_tag diff --git a/src/py_build_cmake/help.py b/src/py_build_cmake/help.py index ef3e058..5944442 100644 --- a/src/py_build_cmake/help.py +++ b/src/py_build_cmake/help.py @@ -114,7 +114,7 @@ def recursive_help_print(opt: ConfigOption, level=0): if v.example: _print_wrapped("For example: " + v.example, indent + " ") default = v.default.get_name() - if default is not None and not is_required: + if not is_required: print(textwrap.indent("Default: " + default, indent + " "))