Skip to content

Commit 502f533

Browse files
committed
handle Optional[sitk.Image]
1 parent 4844349 commit 502f533

4 files changed

Lines changed: 31 additions & 16 deletions

File tree

.pre-commit-config.yaml

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ ci:
99

1010
repos:
1111
- repo: https://github.com/pre-commit/pre-commit-hooks
12-
rev: v4.3.0
12+
rev: v4.4.0
1313
hooks:
1414
- id: end-of-file-fixer
1515
- id: trailing-whitespace
@@ -23,20 +23,20 @@ repos:
2323
- id: detect-private-key
2424

2525
- repo: https://github.com/psf/black
26-
rev: 22.3.0
26+
rev: 23.3.0
2727
hooks:
2828
- id: black
2929
name: format code
3030

3131
- repo: https://github.com/PyCQA/isort
32-
rev: 5.10.1
32+
rev: 5.12.0
3333
hooks:
3434
- id: isort
3535
name: format imports
3636
args: ["--profile", "black"]
3737

3838
- repo: https://github.com/executablebooks/mdformat
39-
rev: 0.7.14
39+
rev: 0.7.16
4040
hooks:
4141
- id: mdformat
4242
name: format markdown
@@ -46,28 +46,28 @@ repos:
4646
exclude: CHANGELOG.md
4747

4848
- repo: https://github.com/PyCQA/flake8
49-
rev: 4.0.1
49+
rev: 6.0.0
5050
hooks:
5151
- id: flake8
5252
name: check PEP8
5353
args: ["--ignore=E501,W503,E203"]
5454

5555
- repo: https://github.com/asottile/pyupgrade
56-
rev: v2.34.0
56+
rev: v3.7.0
5757
hooks:
5858
- id: pyupgrade
5959
name: upgrade code
6060
args: ["--py37-plus"]
6161

6262
- repo: https://github.com/hadialqattan/pycln
63-
rev: v1.2.5
63+
rev: v2.1.5
6464
hooks:
6565
- id: pycln
6666
name: prune imports
6767
args: [--expand-stars]
6868

6969
- repo: https://github.com/nbQA-dev/nbQA
70-
rev: 1.3.1
70+
rev: 1.7.0
7171
hooks:
7272
- id: nbqa-black
7373
additional_dependencies: [black]

examples/pre_processing.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from typing import Optional
1+
from typing import Optional, Union
22

33
import SimpleITK as sitk
44
import typer
@@ -9,7 +9,16 @@
99

1010

1111
@register_command(app)
12-
def median_filter(input: sitk.Image, radius: int) -> sitk.Image:
12+
def add(input1: sitk.Image, input2: Union[sitk.Image, None] = None) -> sitk.Image:
13+
"""Perform foo filtering"""
14+
if input2:
15+
sum: sitk.Image = input1 + input2
16+
return sum
17+
return input1
18+
19+
20+
@register_command(app)
21+
def median_filter(input: sitk.Image, radius: int = 2) -> sitk.Image:
1322
"""Perform median filtering"""
1423
image: sitk.Image = sitk.Median(input, [radius] * input.GetDimension())
1524
return image

setup.cfg

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
[metadata]
22
name = sitk-cli
3-
version = 0.4.0
3+
version = 0.5.0
4+
requires-python = ">=3.8"
45
url = https://github.com/dyollb/sitk-cli
56
description = Wrap SimpleITK functions as command lines
67
long_description = file: README.md

src/sitk_cli/lib.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from inspect import Parameter, isclass, signature
22
from pathlib import Path
3-
from typing import Optional
3+
from typing import Optional, Union, get_args, get_origin
44

55
import SimpleITK as sitk
66
import typer
@@ -14,10 +14,15 @@ def make_cli(func, output_arg_name="output"):
1414

1515
def _translate_param(p: Parameter):
1616
annotation, default = p.annotation, p.default
17-
if isclass(p.annotation) and issubclass(
18-
p.annotation, (sitk.Image, sitk.Transform)
19-
):
20-
if issubclass(p.annotation, sitk.Image):
17+
18+
# handle Optional[A] and Union[A, None]
19+
origin = get_origin(annotation)
20+
args = get_args(annotation)
21+
if origin is Union and args and not isinstance(args[0], type(None)):
22+
annotation = get_args(annotation)[0]
23+
24+
if isclass(annotation) and issubclass(annotation, (sitk.Image, sitk.Transform)):
25+
if issubclass(annotation, sitk.Image):
2126
image_args.append(p.name)
2227
else:
2328
transform_args.append(p.name)

0 commit comments

Comments
 (0)