Skip to content

Commit

Permalink
Apply mypy changes
Browse files Browse the repository at this point in the history
  • Loading branch information
tttapa committed Aug 29, 2023
1 parent 7f7536d commit 43b30ec
Show file tree
Hide file tree
Showing 10 changed files with 28 additions and 30 deletions.
4 changes: 2 additions & 2 deletions src/py_build_cmake/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions src/py_build_cmake/commands/cmd_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion src/py_build_cmake/commands/try_run.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down
14 changes: 7 additions & 7 deletions src/py_build_cmake/common/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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()

Expand Down
16 changes: 7 additions & 9 deletions src/py_build_cmake/config/config_options.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -178,7 +178,7 @@ def get_default(
) -> DefaultValueWrapper | None:
return None

def get_name(self):
def get_name(self) -> str:
return self.name


Expand All @@ -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"


Expand All @@ -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("^", "..")
Expand Down Expand Up @@ -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):
Expand Down
5 changes: 2 additions & 3 deletions src/py_build_cmake/config/dynamic.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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__
Expand All @@ -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


Expand Down
6 changes: 3 additions & 3 deletions src/py_build_cmake/config/load.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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__)

Expand Down
3 changes: 2 additions & 1 deletion src/py_build_cmake/config/quirks.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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"]
Expand Down
2 changes: 1 addition & 1 deletion src/py_build_cmake/export/native_tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion src/py_build_cmake/help.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 + " "))


Expand Down

0 comments on commit 43b30ec

Please sign in to comment.