From ab87cb7e5989ba4c5bd772805071966a0c6e28df Mon Sep 17 00:00:00 2001 From: Benedikt Reinartz Date: Mon, 20 Oct 2025 22:22:38 +0200 Subject: [PATCH] Add a few more type hints --- clr_loader/mono.py | 12 +++++++----- clr_loader/netfx.py | 6 +++--- clr_loader/types.py | 17 ++++++++++------- 3 files changed, 20 insertions(+), 15 deletions(-) diff --git a/clr_loader/mono.py b/clr_loader/mono.py index f7f90e5..638c601 100644 --- a/clr_loader/mono.py +++ b/clr_loader/mono.py @@ -4,7 +4,7 @@ from typing import Any, Dict, Optional, Sequence from .ffi import ffi, load_mono -from .types import Runtime, RuntimeInfo +from .types import Runtime, RuntimeInfo, StrOrPath from .util import optional_path_as_string, path_as_string __all__ = ["Mono"] @@ -32,7 +32,7 @@ def __init__( ): self._assemblies: Dict[Path, Any] = {} - self._version = initialize( + self._version: str = initialize( config_file=optional_path_as_string(config_file), debug=debug, jit_options=jit_options, @@ -50,7 +50,9 @@ def __init__( else: raise NotImplementedError - def _get_callable(self, assembly_path, typename, function): + def _get_callable( + self, assembly_path: StrOrPath, typename: str, function: str + ) -> "MonoMethod": assembly_path = Path(assembly_path) assembly = self._assemblies.get(assembly_path) if not assembly: @@ -87,14 +89,14 @@ def shutdown(self) -> None: class MethodDesc: - def __init__(self, typename, function): + def __init__(self, typename: str, function: str): self._desc = f"{typename}:{function}" self._ptr = _MONO.mono_method_desc_new( self._desc.encode("utf8"), 1, # include_namespace ) - def search(self, image): + def search(self, image: str): return _MONO.mono_method_desc_search_in_image(self._ptr, image) def __del__(self): diff --git a/clr_loader/netfx.py b/clr_loader/netfx.py index 4d46b37..ac3fdec 100644 --- a/clr_loader/netfx.py +++ b/clr_loader/netfx.py @@ -12,7 +12,7 @@ class NetFx(Runtime): def __init__( self, domain: Optional[str] = None, config_file: Optional[Path] = None ): - self._domain = None + self._domain: Optional[str] = None initialize() if config_file is not None: @@ -22,8 +22,8 @@ def __init__( domain_s = domain.encode("utf8") if domain else ffi.NULL - self._domain_name = domain - self._config_file = config_file + self._domain_name: Optional[str] = domain + self._config_file: Optional[Path] = config_file self._domain = _FW.pyclr_create_appdomain(domain_s, config_file_s) def info(self) -> RuntimeInfo: diff --git a/clr_loader/types.py b/clr_loader/types.py index 6b54030..f7b47c1 100644 --- a/clr_loader/types.py +++ b/clr_loader/types.py @@ -5,7 +5,10 @@ __all__ = ["StrOrPath"] -StrOrPath = Union[str, PathLike] +try: + StrOrPath = Union[str, PathLike[str]] +except TypeError: + StrOrPath = Union[str, PathLike] @dataclass @@ -34,7 +37,7 @@ class RuntimeInfo: def __str__(self) -> str: return ( - f"Runtime: {self.kind}\n" + f"Runtime: {self.kind}\n" # pyright: ignore[reportImplicitStringConcatenation] "=============\n" f" Version: {self.version}\n" f" Initialized: {self.initialized}\n" @@ -51,9 +54,9 @@ class ClrFunction: def __init__( self, runtime: "Runtime", assembly: StrOrPath, typename: str, func_name: str ): - self._assembly = assembly - self._class = typename - self._name = func_name + self._assembly: StrOrPath = assembly + self._class: str = typename + self._name: str = func_name self._callable = runtime._get_callable(assembly, typename, func_name) @@ -69,8 +72,8 @@ def __repr__(self) -> str: class Assembly: def __init__(self, runtime: "Runtime", path: StrOrPath): - self._runtime = runtime - self._path = path + self._runtime: "Runtime" = runtime + self._path: StrOrPath = path def get_function(self, name: str, func: Optional[str] = None) -> ClrFunction: """Get a wrapped .NET function instance