1+ from __future__ import annotations
2+
13import hashlib
24import json
35import logging
810import subprocess
911import sys
1012import zipfile
11- from collections .abc import Generator , Iterable
1213from contextlib import contextmanager
1314from dataclasses import dataclass
1415from operator import itemgetter
1516from pathlib import Path
16- from typing import Any , Optional
17+ from typing import TYPE_CHECKING , Any
1718
1819import filelock
1920
2021from maturin_import_hook ._logging import logger
2122from maturin_import_hook .error import ImportHookError , MaturinError
22- from maturin_import_hook .settings import MaturinSettings
23+
24+ if TYPE_CHECKING :
25+ from collections .abc import Generator , Iterable
26+
27+ from maturin_import_hook .settings import MaturinSettings
2328
2429
2530@dataclass
@@ -43,7 +48,7 @@ def to_json(self) -> dict[str, Any]:
4348 }
4449
4550 @staticmethod
46- def from_json (json_data : dict [Any , Any ]) -> Optional [ " BuildStatus" ] :
51+ def from_json (json_data : dict [Any , Any ]) -> BuildStatus | None :
4752 try :
4853 return BuildStatus (
4954 build_mtime = json_data ["build_mtime" ],
@@ -70,7 +75,7 @@ def store_build_status(self, build_status: BuildStatus) -> None:
7075 with self ._build_status_path (build_status .source_path ).open ("w" ) as f :
7176 json .dump (build_status .to_json (), f , indent = " " )
7277
73- def get_build_status (self , source_path : Path ) -> Optional [ BuildStatus ] :
78+ def get_build_status (self , source_path : Path ) -> BuildStatus | None :
7479 try :
7580 with self ._build_status_path (source_path ).open ("r" ) as f :
7681 return BuildStatus .from_json (json .load (f ))
@@ -83,7 +88,7 @@ def tmp_project_dir(self, project_path: Path, module_name: str) -> Path:
8388
8489
8590class BuildCache :
86- def __init__ (self , build_dir : Optional [ Path ] , lock_timeout_seconds : Optional [ float ] ) -> None :
91+ def __init__ (self , build_dir : Path | None , lock_timeout_seconds : float | None ) -> None :
8792 self ._build_dir = build_dir if build_dir is not None else get_default_build_dir ()
8893 self ._lock = filelock .FileLock (
8994 self ._build_dir / "lock" , timeout = - 1 if lock_timeout_seconds is None else lock_timeout_seconds
@@ -96,7 +101,7 @@ def lock(self) -> Generator[LockedBuildCache, None, None]:
96101
97102
98103@contextmanager
99- def _acquire_lock (lock : filelock .FileLock ) -> Generator [None , None , None ]:
104+ def _acquire_lock (lock : filelock .BaseFileLock ) -> Generator [None , None , None ]:
100105 try :
101106 try :
102107 with lock .acquire (blocking = False ):
@@ -245,7 +250,7 @@ def build_unpacked_wheel(maturin_path: Path, manifest_path: Path, output_dir: Pa
245250 return output
246251
247252
248- def _find_single_file (dir_path : Path , extension : Optional [ str ] ) -> Optional [ Path ] :
253+ def _find_single_file (dir_path : Path , extension : str | None ) -> Path | None :
249254 if dir_path .exists ():
250255 candidate_files = [p for p in dir_path .iterdir () if extension is None or p .suffix == extension ]
251256 else :
@@ -261,8 +266,8 @@ def maturin_output_has_warnings(output: str) -> bool:
261266class Freshness :
262267 is_fresh : bool
263268 reason : str
264- oldest_installed_path : Optional [ Path ]
265- newest_source_path : Optional [ Path ]
269+ oldest_installed_path : Path | None
270+ newest_source_path : Path | None
266271
267272
268273def get_installation_freshness (
@@ -326,7 +331,7 @@ def get_installation_freshness(
326331 return Freshness (True , "" , oldest_installed_path , newest_source_path )
327332
328333
329- def get_installation_mtime (installed_paths : Iterable [Path ]) -> Optional [ float ] :
334+ def get_installation_mtime (installed_paths : Iterable [Path ]) -> float | None :
330335 try :
331336 installation_mtime = min (path .stat ().st_mtime for path in installed_paths )
332337 except ValueError :
0 commit comments