Skip to content

Commit

Permalink
raised minimum python version to 3.9
Browse files Browse the repository at this point in the history
  • Loading branch information
mbway committed Mar 24, 2024
1 parent 002ac50 commit b37223e
Show file tree
Hide file tree
Showing 14 changed files with 86 additions and 145 deletions.
4 changes: 1 addition & 3 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,9 @@ on:
python_version:
type: choice
description: Python version to test with
default: '3.11'
default: '3.12'
options:
- 'all'
- '3.8'
- '3.9'
- '3.10'
- '3.11'
Expand Down Expand Up @@ -95,7 +94,6 @@ jobs:
- macos-13
- windows-latest
PYTHON_VERSION: |
- '3.8'
- '3.9'
- '3.10'
- '3.11'
Expand Down
6 changes: 3 additions & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ authors = [
]
readme = "README.md"
version = "0.1.0"
requires-python = ">=3.8"
requires-python = ">=3.9"
dependencies = [
"filelock",
"tomli>=1.1.0 ; python_version<'3.11'"
Expand All @@ -33,7 +33,7 @@ where = ["src"]

[tool.ruff]
line-length = 120
target-version = "py38"
target-version = "py39"

[tool.ruff.format]
preview = true
Expand Down Expand Up @@ -69,7 +69,7 @@ ignore = [
]

[tool.mypy]
python_version = "3.8"
python_version = "3.9"
strict = true
allow_redefinition = true
exclude = [
Expand Down
3 changes: 1 addition & 2 deletions src/maturin_import_hook/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import shutil
import subprocess
from pathlib import Path
from typing import Dict

from maturin_import_hook._building import get_default_build_dir
from maturin_import_hook._site import (
Expand Down Expand Up @@ -119,7 +118,7 @@ def _dir_size_mib(dir_path: Path) -> str:
return f"{cache_size / (1024 * 1024):.2f} MiB"


def _print_info(info: Dict[str, object], format_name: str) -> None:
def _print_info(info: dict[str, object], format_name: str) -> None:
if format_name == "text":
for k, v in info.items():
print(f"{k}: {v}")
Expand Down
15 changes: 8 additions & 7 deletions src/maturin_import_hook/_building.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@
import subprocess
import sys
import zipfile
from collections.abc import Generator, Iterable
from contextlib import contextmanager
from dataclasses import dataclass
from operator import itemgetter
from pathlib import Path
from typing import Any, Dict, Generator, Iterable, List, Optional, Tuple
from typing import Any, Optional

import filelock

Expand All @@ -30,10 +31,10 @@ class BuildStatus:

build_mtime: float
source_path: Path
maturin_args: List[str]
maturin_args: list[str]
maturin_output: str

def to_json(self) -> Dict[str, Any]:
def to_json(self) -> dict[str, Any]:
return {
"build_mtime": self.build_mtime,
"source_path": str(self.source_path),
Expand All @@ -42,7 +43,7 @@ def to_json(self) -> Dict[str, Any]:
}

@staticmethod
def from_json(json_data: Dict[Any, Any]) -> Optional["BuildStatus"]:
def from_json(json_data: dict[Any, Any]) -> Optional["BuildStatus"]:
try:
return BuildStatus(
build_mtime=json_data["build_mtime"],
Expand Down Expand Up @@ -185,7 +186,7 @@ def develop_build_project(
return output


def find_maturin(lower_version: Tuple[int, int, int], upper_version: Tuple[int, int, int]) -> Path:
def find_maturin(lower_version: tuple[int, int, int], upper_version: tuple[int, int, int]) -> Path:
logger.debug("searching for maturin")
maturin_path_str = shutil.which("maturin")
if maturin_path_str is None:
Expand All @@ -202,7 +203,7 @@ def find_maturin(lower_version: Tuple[int, int, int], upper_version: Tuple[int,
raise MaturinError(msg)


def get_maturin_version(maturin_path: Path) -> Tuple[int, int, int]:
def get_maturin_version(maturin_path: Path) -> tuple[int, int, int]:
success, output = run_maturin(maturin_path, ["--version"])
if not success:
msg = f'running "{maturin_path} --version" failed'
Expand All @@ -214,7 +215,7 @@ def get_maturin_version(maturin_path: Path) -> Tuple[int, int, int]:
return int(match.group(1)), int(match.group(2)), int(match.group(3))


def run_maturin(maturin_path: Path, args: List[str]) -> Tuple[bool, str]:
def run_maturin(maturin_path: Path, args: list[str]) -> tuple[bool, str]:
command = [str(maturin_path), *args]
if logger.isEnabledFor(logging.DEBUG):
logger.debug("running command: %s", subprocess.list2cmdline(command))
Expand Down
22 changes: 11 additions & 11 deletions src/maturin_import_hook/_resolve_project.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import itertools
from dataclasses import dataclass
from pathlib import Path
from typing import Any, Dict, List, Optional, Tuple, Type, TypeVar
from typing import Any, Optional, TypeVar

from maturin_import_hook._logging import logger

Expand All @@ -15,7 +15,7 @@


class _TomlFile:
def __init__(self, path: Path, data: Dict[Any, Any]) -> None:
def __init__(self, path: Path, data: dict[Any, Any]) -> None:
self.path = path
self.data = data

Expand All @@ -25,11 +25,11 @@ def load(path: Path) -> "_TomlFile":
data = tomllib.load(f)
return _TomlFile(path, data)

def get_value_or_default(self, keys: List[str], required_type: Type[_T], default: _T) -> _T:
def get_value_or_default(self, keys: list[str], required_type: type[_T], default: _T) -> _T:
value = self.get_value(keys, required_type)
return default if value is None else value

def get_value(self, keys: List[str], required_type: Type[_T]) -> Optional[_T]:
def get_value(self, keys: list[str], required_type: type[_T]) -> Optional[_T]:
assert keys
current_data: Any = self.data
num_keys = len(keys)
Expand Down Expand Up @@ -78,7 +78,7 @@ def is_maybe_maturin_project(project_dir: Path) -> bool:

class ProjectResolver:
def __init__(self) -> None:
self._resolved_project_cache: Dict[Path, Optional[MaturinProject]] = {}
self._resolved_project_cache: dict[Path, Optional[MaturinProject]] = {}

def resolve(self, project_dir: Path) -> Optional["MaturinProject"]:
if project_dir not in self._resolved_project_cache:
Expand Down Expand Up @@ -106,9 +106,9 @@ class MaturinProject:
# the location that the compiled extension module is written to when installed in editable/unpacked mode
extension_module_dir: Optional[Path]
# path dependencies listed in the Cargo.toml of the main project
immediate_path_dependencies: List[Path]
immediate_path_dependencies: list[Path]
# all path dependencies including transitive dependencies
_all_path_dependencies: Optional[List[Path]] = None
_all_path_dependencies: Optional[list[Path]] = None

@property
def package_name(self) -> str:
Expand All @@ -124,13 +124,13 @@ def is_mixed(self) -> bool:
return self.extension_module_dir is not None

@property
def all_path_dependencies(self) -> List[Path]:
def all_path_dependencies(self) -> list[Path]:
if self._all_path_dependencies is None:
self._all_path_dependencies = _find_all_path_dependencies(self.immediate_path_dependencies)
return self._all_path_dependencies


def _find_all_path_dependencies(immediate_path_dependencies: List[Path]) -> List[Path]:
def _find_all_path_dependencies(immediate_path_dependencies: list[Path]) -> list[Path]:
if not immediate_path_dependencies:
return []
all_path_dependencies = set()
Expand Down Expand Up @@ -194,7 +194,7 @@ def _resolve_project(project_dir: Path) -> MaturinProject:
)


def _resolve_rust_module(python_dir: Path, module_name: str) -> Tuple[Path, Path, str]:
def _resolve_rust_module(python_dir: Path, module_name: str) -> tuple[Path, Path, str]:
"""This follows the same logic as project_layout.rs (ProjectLayout::determine).
rust_module is the directory that the extension library gets written to when the package is
Expand Down Expand Up @@ -234,7 +234,7 @@ def _resolve_module_name(pyproject: _TomlFile, cargo: _TomlFile) -> Optional[str
return cargo.get_value(["package", "name"], str)


def _get_immediate_path_dependencies(manifest_dir_path: Path, cargo: _TomlFile) -> List[Path]:
def _get_immediate_path_dependencies(manifest_dir_path: Path, cargo: _TomlFile) -> list[Path]:
path_dependencies = []
for dependency in cargo.get_value_or_default(["dependencies"], dict, {}).values():
if isinstance(dependency, dict):
Expand Down
33 changes: 17 additions & 16 deletions src/maturin_import_hook/project_importer.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@
import urllib.parse
import urllib.request
from abc import ABC, abstractmethod
from collections.abc import Iterator, Sequence
from importlib.machinery import ExtensionFileLoader, ModuleSpec, PathFinder
from pathlib import Path
from types import ModuleType
from typing import ClassVar, Iterator, List, Optional, Sequence, Set, Tuple, Union
from typing import ClassVar, Optional, Union

from maturin_import_hook._building import (
BuildCache,
Expand Down Expand Up @@ -53,7 +54,7 @@ class ProjectFileSearcher(ABC):
def get_source_paths(
self,
project_dir: Path,
all_path_dependencies: List[Path],
all_path_dependencies: list[Path],
installed_package_root: Path,
) -> Iterator[Path]:
"""find the files corresponding to the source code of the given project"""
Expand Down Expand Up @@ -211,7 +212,7 @@ def _rebuild_project(
self,
package_name: str,
project_dir: Path,
) -> Tuple[Optional[ModuleSpec], bool]:
) -> tuple[Optional[ModuleSpec], bool]:
resolved = self._resolver.resolve(project_dir)
if resolved is None:
return None, False
Expand Down Expand Up @@ -283,7 +284,7 @@ def _get_spec_for_up_to_date_package(
resolved: MaturinProject,
settings: MaturinSettings,
build_cache: LockedBuildCache,
) -> Tuple[Optional[ModuleSpec], Optional[str]]:
) -> tuple[Optional[ModuleSpec], Optional[str]]:
"""Return a spec for the package if it exists and is newer than the source
code that it is derived from.
"""
Expand Down Expand Up @@ -381,7 +382,7 @@ def _find_maturin_project_above(path: Path) -> Optional[Path]:

def _load_dist_info(
path: Path, package_name: str, *, require_project_target: bool = True
) -> Tuple[Optional[Path], bool]:
) -> tuple[Optional[Path], bool]:
dist_info_path = next(path.glob(f"{package_name}-*.dist-info"), None)
if dist_info_path is None:
return None, False
Expand Down Expand Up @@ -457,7 +458,7 @@ class DefaultProjectFileSearcher(ProjectFileSearcher):
# - https://github.com/github/gitignore/blob/main/Rust.gitignore
# - https://github.com/github/gitignore/blob/main/Python.gitignore
# - https://github.com/jupyter/notebook/blob/main/.gitignore
DEFAULT_SOURCE_EXCLUDED_DIR_NAMES: ClassVar[Set[str]] = {
DEFAULT_SOURCE_EXCLUDED_DIR_NAMES: ClassVar[set[str]] = {
".cache",
".env",
".git",
Expand All @@ -481,20 +482,20 @@ class DefaultProjectFileSearcher(ProjectFileSearcher):
"target",
"venv",
}
DEFAULT_SOURCE_EXCLUDED_DIR_MARKERS: ClassVar[Set[str]] = {
DEFAULT_SOURCE_EXCLUDED_DIR_MARKERS: ClassVar[set[str]] = {
"CACHEDIR.TAG", # https://bford.info/cachedir/
}
DEFAULT_SOURCE_EXCLUDED_FILE_EXTENSIONS: ClassVar[Set[str]] = {
DEFAULT_SOURCE_EXCLUDED_FILE_EXTENSIONS: ClassVar[set[str]] = {
".so",
".pyc",
}

def __init__(
self,
*,
source_excluded_dir_names: Optional[Set[str]] = None,
source_excluded_dir_markers: Optional[Set[str]] = None,
source_excluded_file_extensions: Optional[Set[str]] = None,
source_excluded_dir_names: Optional[set[str]] = None,
source_excluded_dir_markers: Optional[set[str]] = None,
source_excluded_file_extensions: Optional[set[str]] = None,
) -> None:
"""
Args:
Expand Down Expand Up @@ -525,7 +526,7 @@ def __init__(
def get_source_paths(
self,
project_dir: Path,
all_path_dependencies: List[Path],
all_path_dependencies: list[Path],
installed_package_root: Path,
) -> Iterator[Path]:
excluded_dirs = set()
Expand Down Expand Up @@ -557,10 +558,10 @@ def get_installation_paths(self, installed_package_root: Path) -> Iterator[Path]
def get_files_in_dir(
self,
root_path: Path,
ignore_dirs: Set[Path],
excluded_dir_names: Set[str],
excluded_dir_markers: Set[str],
excluded_file_extensions: Set[str],
ignore_dirs: set[Path],
excluded_dir_names: set[str],
excluded_dir_markers: set[str],
excluded_file_extensions: set[str],
) -> Iterator[Path]:
if root_path.name in excluded_dir_names:
return
Expand Down
7 changes: 4 additions & 3 deletions src/maturin_import_hook/rust_file_importer.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@
import sys
import tempfile
import time
from collections.abc import Iterator, Sequence
from importlib.machinery import ExtensionFileLoader, ModuleSpec
from pathlib import Path
from types import ModuleType
from typing import TYPE_CHECKING, Iterator, Optional, Sequence, Tuple, Union
from typing import TYPE_CHECKING, Optional, Union

from maturin_import_hook._building import (
BuildCache,
Expand Down Expand Up @@ -189,7 +190,7 @@ def _handle_reload(self, module_path: str, spec: ModuleSpec) -> ModuleSpec:

def _import_rust_file(
self, module_path: str, module_name: str, file_path: Path
) -> Tuple[Optional[ModuleSpec], bool]:
) -> tuple[Optional[ModuleSpec], bool]:
logger.debug('importing rust file "%s" as "%s"', file_path, module_path)

with self._build_cache.lock() as build_cache:
Expand Down Expand Up @@ -250,7 +251,7 @@ def _get_spec_for_up_to_date_extension_module(
source_path: Path,
settings: MaturinSettings,
build_cache: LockedBuildCache,
) -> Tuple[Optional[ModuleSpec], Optional[str]]:
) -> tuple[Optional[ModuleSpec], Optional[str]]:
"""Return a spec for the given module at the given search_dir if it exists and is newer than the source
code that it is derived from.
"""
Expand Down
Loading

0 comments on commit b37223e

Please sign in to comment.