Skip to content

Commit

Permalink
BF: do not demand --files for all commands, even those which do not c…
Browse files Browse the repository at this point in the history
…are about it

It refactors how we do check and assertion now for mypy to stay quiet.
Closes #707
  • Loading branch information
yarikoptic committed Sep 27, 2023
1 parent 5552416 commit 40f34b9
Showing 1 changed file with 16 additions and 5 deletions.
21 changes: 16 additions & 5 deletions heudiconv/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def _pdb_excepthook(
def process_extra_commands(
outdir: str,
command: str,
files: list[str],
files: Optional[list[str]],
heuristic: Optional[str],
session: Optional[str],
subjs: Optional[list[str]],
Expand All @@ -72,8 +72,8 @@ def process_extra_commands(
Output directory
command : {'treat-json', 'ls', 'populate-templates', 'populate-intended-for'}
Heudiconv command to run
files : list of str
List of files
files : list of str or None
List of files if command needs/expects it
heuristic : str or None
Path to heuristic file or name of builtin heuristic.
session : str or None
Expand All @@ -83,12 +83,21 @@ def process_extra_commands(
grouping : {'studyUID', 'accession_number', 'all', 'custom'}
How to group dicoms.
"""

def ensure_has_files() -> None:
if files is None:
raise ValueError(f"command {command} expects --files being provided")

Check warning on line 89 in heudiconv/main.py

View check run for this annotation

Codecov / codecov/patch

heudiconv/main.py#L89

Added line #L89 was not covered by tests

if command == "treat-jsons":
ensure_has_files()
assert files is not None # for mypy now

Check warning on line 93 in heudiconv/main.py

View check run for this annotation

Codecov / codecov/patch

heudiconv/main.py#L92-L93

Added lines #L92 - L93 were not covered by tests
for fname in files:
treat_infofile(fname)
elif command == "ls":
ensure_heuristic_arg(heuristic)
assert heuristic is not None
ensure_has_files()
assert files is not None # for mypy now
heuristic_mod = load_heuristic(heuristic)
heuristic_ls = getattr(heuristic_mod, "ls", None)
for fname in files:
Expand All @@ -111,10 +120,14 @@ def process_extra_commands(
elif command == "populate-templates":
ensure_heuristic_arg(heuristic)
assert heuristic is not None
ensure_has_files()
assert files is not None # for mypy now
heuristic_mod = load_heuristic(heuristic)
for fname in files:
populate_bids_templates(fname, getattr(heuristic_mod, "DEFAULT_FIELDS", {}))
elif command == "sanitize-jsons":
ensure_has_files()
assert files is not None # for mypy now

Check warning on line 130 in heudiconv/main.py

View check run for this annotation

Codecov / codecov/patch

heudiconv/main.py#L129-L130

Added lines #L129 - L130 were not covered by tests
tuneup_bids_json_files(files)
elif command == "heuristics":
from .utils import get_known_heuristics_with_descriptions
Expand Down Expand Up @@ -357,8 +370,6 @@ def workflow(
)

if command:
if files is None:
raise ValueError("'command' given but 'files' is None")
assert dicom_dir_template is None
process_extra_commands(
outdir,
Expand Down

0 comments on commit 40f34b9

Please sign in to comment.