Skip to content

Commit 7c5793a

Browse files
fepegarCopilot
andauthored
Adopt ty and refactor core typing everywhere (#1414)
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 7517ab9 commit 7c5793a

79 files changed

Lines changed: 1701 additions & 895 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.pre-commit-config.yaml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,18 @@ repos:
3737
- id: pyupgrade
3838
args: ['--py310-plus']
3939

40+
- repo: local
41+
hooks:
42+
- id: ty-check
43+
name: ty check
44+
entry: uv run --group types ty check --output-format concise src
45+
language: system
46+
always_run: true
47+
pass_filenames: false
48+
require_serial: true
49+
types_or: [python, pyi]
50+
4051
ci:
4152
autoupdate_commit_msg: Autoupdate pre-commit hooks
4253
autoupdate_schedule: quarterly
54+
skip: [ty-check]

pyproject.toml

Lines changed: 3 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ dependencies = [
3535
"deprecated>=1.2",
3636
"einops>=0.3",
3737
"humanize>=0.1",
38+
"jaxtyping>=0.2",
3839
"nibabel>=3",
3940
"numpy>=1.20",
4041
"packaging>=20",
@@ -85,7 +86,8 @@ doc = [
8586
maintain = ["bump-my-version"]
8687
quality = ["ruff"]
8788
types = [
88-
"mypy",
89+
"matplotlib",
90+
"ty",
8991
"pandas-stubs",
9092
"pillow",
9193
"pip",
@@ -117,29 +119,6 @@ filename = "pyproject.toml"
117119
search = 'version = "{current_version}"'
118120
replace = 'version = "{new_version}"'
119121

120-
[tool.mypy]
121-
pretty = true
122-
123-
[[tool.mypy.overrides]]
124-
module = "duecredit.*"
125-
ignore_missing_imports = true
126-
127-
[[tool.mypy.overrides]]
128-
module = "huggingface_hub.*"
129-
ignore_missing_imports = true
130-
131-
[[tool.mypy.overrides]]
132-
module = "matplotlib.*"
133-
ignore_missing_imports = true
134-
135-
[[tool.mypy.overrides]]
136-
module = "nibabel.*"
137-
ignore_missing_imports = true
138-
139-
[[tool.mypy.overrides]]
140-
module = "scipy.*"
141-
ignore_missing_imports = true
142-
143122
[tool.pytest.ini_options]
144123
addopts = ["-ra", "--strict-config", "--strict-markers"]
145124
filterwarnings = [

src/torchio/data/dataset.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
from .subject import Subject
1212

1313

14-
class SubjectsDataset(Dataset):
14+
class SubjectsDataset(Dataset[Subject]):
1515
"""Base TorchIO dataset.
1616
1717
Reader of 3D medical images that directly inherits from the PyTorch
@@ -61,12 +61,12 @@ class SubjectsDataset(Dataset):
6161
def __init__(
6262
self,
6363
subjects: Sequence[Subject],
64-
transform: Callable | None = None,
64+
transform: Callable[[Subject], Subject] | None = None,
6565
load_getitem: bool = True,
6666
):
6767
self._parse_subjects_list(subjects)
6868
self._subjects = subjects
69-
self._transform: Callable | None
69+
self._transform: Callable[[Subject], Subject] | None
7070
self.set_transform(transform)
7171
self.load_getitem = load_getitem
7272

@@ -104,7 +104,7 @@ def from_batch(cls, batch: dict) -> SubjectsDataset:
104104
subjects: list[Subject] = get_subjects_from_batch(batch)
105105
return cls(subjects)
106106

107-
def dry_iter(self):
107+
def dry_iter(self) -> Sequence[Subject]:
108108
"""Return the internal list of subjects.
109109
110110
This can be used to iterate over the subjects without loading the data
@@ -115,7 +115,10 @@ def dry_iter(self):
115115
"""
116116
return self._subjects
117117

118-
def set_transform(self, transform: Callable | None) -> None:
118+
def set_transform(
119+
self,
120+
transform: Callable[[Subject], Subject] | None,
121+
) -> None:
119122
"""Set the `transform` attribute.
120123
121124
Args:

0 commit comments

Comments
 (0)