diff --git a/snakebids/paths/_factory.py b/snakebids/paths/_factory.py index 85648a04..f86d1fa8 100644 --- a/snakebids/paths/_factory.py +++ b/snakebids/paths/_factory.py @@ -134,7 +134,7 @@ def bids_factory(spec: BidsPathSpec, *, _implicit: bool = False) -> BidsFunction parse_entities = _get_entity_parser(aliases) - def bids( + def bids( # noqa: PLR0912 root: str | Path | None = None, *, datatype: str | None = None, @@ -228,13 +228,23 @@ def bids( if datatype: path_parts.append(datatype) + tail: list[str] = [] + if suffix is not None: + tail.append(suffix) + if extension is not None: + tail.append(extension) path_parts.append( - "_".join(it.chain(spec_parts[:split], custom_parts, spec_parts[split:])) + "_".join( + it.chain( + spec_parts[:split], + custom_parts, + spec_parts[split:], + ["".join(tail)], + ) + ) ) - tail = f"_{suffix}{extension or ''}" if suffix else extension or "" - - result = os.path.join(*path_parts) + tail + result = os.path.join(*path_parts) if custom_parts and _implicit and not in_interactive_session(): wrn_msg = ( f"Path generated with unrecognized entities, and a snakebids spec has " diff --git a/snakebids/tests/test_paths/test_bids.py b/snakebids/tests/test_paths/test_bids.py index ac905847..e6cc866a 100644 --- a/snakebids/tests/test_paths/test_bids.py +++ b/snakebids/tests/test_paths/test_bids.py @@ -144,6 +144,12 @@ def test_underscore_does_not_precede_extension( def test_no_underscore_at_end_if_no_suffix(self, entities: dict[str, str]): assert bids(**entities)[-1] != "_" + @given(entities=_bids_args(nonstandard=True, entities=None)) + def test_no_underscore_at_beginning_if_only_suffix( + self, entities: dict[str, str] + ): + assert bids(**entities)[0] != "_" + @given(entities=_bids_args(), root=_roots()) def test_beginning_of_path_always_root( self, entities: dict[str, str], root: str @@ -322,4 +328,6 @@ def test_benchmark_bids(benchmark: Benchmark): england="britain", space="cosmos", rome="fell", + suffix="suffix", + extension=".ext", )