Skip to content

Commit 80fe342

Browse files
committed
Add ty type checking and fix typing issues
1 parent fc5c9d2 commit 80fe342

10 files changed

Lines changed: 94 additions & 28 deletions

File tree

‎.github/workflows/ci.yml‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ jobs:
3636
run: ruff format --check
3737
- name: Check lints
3838
run: ruff check
39+
- name: Check types
40+
run: uv run --group dev ty check clr_loader
3941

4042
test:
4143
runs-on: ${{ matrix.os.instance }}

‎.gitignore‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,8 @@ target/
6464
# pyenv
6565
.python-version
6666

67-
# mypy
68-
.mypy_cache/
69-
.dmypy.json
70-
dmypy.json
67+
# ty
68+
.ty_cache/
7169

7270
# Pyre type checker
7371
.pyre/

‎Justfile‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,15 @@ test:
1313
lint:
1414
uv run ruff check
1515

16+
typecheck:
17+
uv run ty check clr_loader
18+
1619
format:
1720
uv run ruff format
1821

19-
check: lint test
22+
check: lint typecheck test
2023
uv run ruff format --check
2124

2225
docs output="doc/html/":
2326
uv run --group doc sphinx-build doc/ doc/html/
2427

25-

‎clr_loader/ffi/__init__.py‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,8 @@ def _path_to_version(path: Path) -> tuple[int, int, int]:
7272
# Handle pre-release versions like "10.0.0-rc.1" by taking only the version part
7373
version_part = name.split("-")[0]
7474
res = list(map(int, version_part.split(".")))
75-
return tuple(res + [0, 0, 0])[:3]
75+
v = (res + [0, 0, 0])[:3]
76+
return (v[0], v[1], v[2])
7677
except Exception: # noqa
7778
return (0, 0, 0)
7879

‎clr_loader/hostfxr.py‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import sys
22
from collections.abc import Generator
33
from pathlib import Path
4+
from typing import Any
45

56
from .ffi import ffi, load_hostfxr
67
from .types import Runtime, RuntimeInfo, StrOrPath
@@ -144,7 +145,7 @@ def info(self):
144145
def _get_handle_for_runtime_config(
145146
dll, dotnet_root: StrOrPath, runtime_config: StrOrPath
146147
):
147-
params = ffi.new("hostfxr_initialize_parameters*")
148+
params: Any = ffi.new("hostfxr_initialize_parameters*")
148149
params.size = ffi.sizeof("hostfxr_initialize_parameters")
149150
# params.host_path = ffi.new("char_t[]", encode(sys.executable))
150151
params.host_path = ffi.NULL
@@ -164,7 +165,7 @@ def _get_handle_for_runtime_config(
164165
def _get_handle_for_dotnet_command_line(
165166
dll, dotnet_root: StrOrPath, entry_dll: StrOrPath
166167
):
167-
params = ffi.new("hostfxr_initialize_parameters*")
168+
params: Any = ffi.new("hostfxr_initialize_parameters*")
168169
params.size = ffi.sizeof("hostfxr_initialize_parameters")
169170
params.host_path = ffi.NULL
170171
dotnet_root_p = ffi.new("char_t[]", encode(str(Path(dotnet_root))))
@@ -209,4 +210,7 @@ def encode(string: str):
209210
return string.encode("utf8")
210211

211212
def decode(char_ptr) -> str:
212-
return ffi.string(char_ptr).decode("utf8")
213+
res = ffi.string(char_ptr)
214+
if isinstance(res, bytes):
215+
return res.decode("utf8")
216+
return str(res)

‎clr_loader/mono.py‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,12 @@ def initialize(
186186

187187
build = _MONO.mono_get_runtime_build_info()
188188
_check_result(build, "Failed to get Mono version")
189-
ver_str = ffi.string(build).decode("utf8") # e.g. '6.12.0.122 (tarball)'
189+
build_bytes = ffi.string(build)
190+
ver_str = (
191+
build_bytes.decode("utf8")
192+
if isinstance(build_bytes, bytes)
193+
else str(build_bytes)
194+
) # e.g. '6.12.0.122 (tarball)'
190195

191196
ver = re.match(r"^(?P<major>\d+)\.(?P<minor>\d+)\.[\d.]+", ver_str)
192197
if ver is not None:

‎clr_loader/netfx.py‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,16 @@
1010

1111
class NetFx(Runtime):
1212
def __init__(self, domain: str | None = None, config_file: Path | None = None):
13-
self._domain: str | None = None
13+
self._domain: Any = None
1414

1515
initialize()
16+
config_file_s: Any
1617
if config_file is not None:
1718
config_file_s = str(config_file).encode("utf8")
1819
else:
1920
config_file_s = ffi.NULL
2021

21-
domain_s = domain.encode("utf8") if domain else ffi.NULL
22+
domain_s: Any = domain.encode("utf8") if domain else ffi.NULL
2223

2324
self._domain_name: str | None = domain
2425
self._config_file: Path | None = config_file

‎clr_loader/util/find.py‎

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -161,31 +161,31 @@ def _find_mono_unix(
161161
if candidate.exists():
162162
return candidate
163163

164-
if res := find_library(unix_name):
165-
return Path(res)
164+
if lib_path := find_library(unix_name):
165+
return Path(lib_path)
166166

167167
if macos:
168-
res = (
168+
candidate = (
169169
Path("/Library/Frameworks/Mono.framework/Versions/Current/lib")
170170
/ lib_filename
171171
)
172-
if res.exists():
173-
return res
172+
if candidate.exists():
173+
return candidate
174174

175175
# Use HOMEBREW_PREFIX environment variable if available
176176
if homebrew_prefix := os.environ.get("HOMEBREW_PREFIX"):
177-
res = Path(homebrew_prefix) / "opt/mono/lib" / lib_filename
178-
if res.exists():
179-
return res
177+
candidate = Path(homebrew_prefix) / "opt/mono/lib" / lib_filename
178+
if candidate.exists():
179+
return candidate
180180

181181
# Check for native Apple Silicon (arm64)
182182
if platform.machine() == "arm64":
183-
res = Path("/opt/homebrew/opt/mono/lib") / lib_filename
184-
if res.exists():
185-
return res
183+
candidate = Path("/opt/homebrew/opt/mono/lib") / lib_filename
184+
if candidate.exists():
185+
return candidate
186186
else:
187-
res = Path("/usr/local/opt/mono/lib") / lib_filename
188-
if res.exists():
189-
return res
187+
candidate = Path("/usr/local/opt/mono/lib") / lib_filename
188+
if candidate.exists():
189+
return candidate
190190

191191
raise RuntimeError("Could not find libmono")

‎pyproject.toml‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,10 @@ Documentation = "https://pythonnet.github.io/clr-loader/"
3333

3434
[dependency-groups]
3535
dev = [
36+
"ty >=0.0.1",
3637
"pytest >=9.0, <10.0",
3738
"ruff >=0.15",
39+
"types-cffi",
3840
]
3941
doc = ["furo >=2025.0", "sphinx >=9.0, <10.0"]
4042

‎uv.lock‎

Lines changed: 52 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)